Skip to content

Commit f396004

Browse files
committed
Auto merge of #10975 - hehaoqian:fix_self_named_module_files, r=Centri3,xFrednet
Fix false positive of [self_named_module_files] and [mod_module_files] changelog: [self_named_module_files] [mod_module_files]: No longer lints dependencies located in subdirectory of workspace fixes #8887 --- First time contributor here, just read contribution guide today. I have several questions: 1. ~Is it the correct way to use environment variable `CARGO_HOME` to get the location of cargo home directory?~ (Edit: Code no longer uses CARGO_HOME) 2. How to setup test for this PR? This involves multiple files and `CARGO_HOME` setup. ~Not sure how to do this.~ ~Edit: Working on tests right now~ A workspace_test has been added
2 parents c8c03ea + e11ebbd commit f396004

File tree

22 files changed

+139
-2
lines changed

22 files changed

+139
-2
lines changed

clippy_lints/src/module_style.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use rustc_ast::ast;
22
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
33
use rustc_lint::{EarlyContext, EarlyLintPass, Level, LintContext};
44
use rustc_session::{declare_tool_lint, impl_lint_pass};
5+
use rustc_span::def_id::LOCAL_CRATE;
56
use rustc_span::{FileName, SourceFile, Span, SyntaxContext};
67
use std::ffi::OsStr;
78
use std::path::{Component, Path};
@@ -90,7 +91,14 @@ impl EarlyLintPass for ModStyle {
9091
// `{ foo => path/to/foo.rs, .. }
9192
let mut file_map = FxHashMap::default();
9293
for file in files.iter() {
93-
if let FileName::Real(name) = &file.name && let Some(lp) = name.local_path() {
94+
if let FileName::Real(name) = &file.name
95+
&& let Some(lp) = name.local_path()
96+
&& file.cnum == LOCAL_CRATE
97+
{
98+
// [#8887](https://github.com/rust-lang/rust-clippy/issues/8887)
99+
// Only check files in the current crate.
100+
// Fix false positive that crate dependency in workspace sub directory
101+
// is checked unintentionally.
94102
let path = if lp.is_relative() {
95103
lp
96104
} else if let Ok(relative) = lp.strip_prefix(trim_to_src) {

tests/workspace.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,46 @@ use test_utils::{CARGO_CLIPPY_PATH, IS_RUSTC_TEST_SUITE};
66

77
mod test_utils;
88

9+
#[test]
10+
fn test_module_style_with_dep_in_subdir() {
11+
if IS_RUSTC_TEST_SUITE {
12+
return;
13+
}
14+
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
15+
let target_dir = root.join("target").join("workspace_test");
16+
let cwd = root.join("tests/workspace_test");
17+
18+
// Make sure we start with a clean state
19+
Command::new("cargo")
20+
.current_dir(&cwd)
21+
.env("CARGO_TARGET_DIR", &target_dir)
22+
.arg("clean")
23+
.args(["-p", "pass-no-mod-with-dep-in-subdir"])
24+
.args(["-p", "pass-mod-with-dep-in-subdir"])
25+
.output()
26+
.unwrap();
27+
28+
// [#8887](https://github.com/rust-lang/rust-clippy/issues/8887)
29+
// `mod.rs` checks should not be applied to crate dependencies
30+
// located in the subdirectory of workspace
31+
let output = Command::new(&*CARGO_CLIPPY_PATH)
32+
.current_dir(&cwd)
33+
.env("CARGO_INCREMENTAL", "0")
34+
.env("CARGO_TARGET_DIR", &target_dir)
35+
.arg("clippy")
36+
.args(["-p", "pass-no-mod-with-dep-in-subdir"])
37+
.args(["-p", "pass-mod-with-dep-in-subdir"])
38+
.arg("--")
39+
.arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
40+
.output()
41+
.unwrap();
42+
43+
println!("status: {}", output.status);
44+
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
45+
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
46+
assert!(output.status.success());
47+
}
48+
949
#[test]
1050
fn test_no_deps_ignores_path_deps_in_workspaces() {
1151
if IS_RUSTC_TEST_SUITE {

tests/workspace_test/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ version = "0.1.0"
44
edition = "2018"
55

66
[workspace]
7-
members = ["subcrate"]
7+
members = ["subcrate", "module_style/pass_no_mod_with_dep_in_subdir", "module_style/pass_mod_with_dep_in_subdir"]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "pass-mod-with-dep-in-subdir"
3+
version = "0.1.0"
4+
edition = "2018"
5+
publish = false
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
dep-no-mod = { path = "dep_no_mod"}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "dep-no-mod"
3+
version = "0.1.0"
4+
edition = "2018"
5+
publish = false
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod hello;
2+
pub struct Thing;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub struct Hello;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub mod foo;
2+
3+
pub fn foo() {
4+
let _ = foo::Thing;
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub struct Thing;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![deny(clippy::self_named_module_files)]
2+
3+
mod bad;
4+
mod more;
5+
extern crate dep_no_mod;
6+
7+
fn main() {
8+
let _ = bad::Thing;
9+
let _ = more::foo::Foo;
10+
let _ = more::inner::Inner;
11+
let _ = dep_no_mod::foo::Thing;
12+
let _ = dep_no_mod::foo::hello::Hello;
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub struct Foo;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub struct Inner;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod foo;
2+
pub mod inner;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "pass-no-mod-with-dep-in-subdir"
3+
version = "0.1.0"
4+
edition = "2018"
5+
publish = false
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
dep-with-mod = { path = "dep_with_mod"}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "dep-with-mod"
3+
version = "0.1.0"
4+
edition = "2018"
5+
publish = false
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pub mod with_mod;
2+
3+
pub fn foo() {
4+
let _ = with_mod::Thing;
5+
let _ = with_mod::inner::stuff::Inner;
6+
let _ = with_mod::inner::stuff::most::Snarks;
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod stuff;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod most;
2+
3+
pub struct Inner;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub struct Snarks;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod inner;
2+
3+
pub struct Thing;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub struct Thing;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![deny(clippy::mod_module_files)]
2+
3+
mod good;
4+
pub use dep_with_mod::with_mod::Thing;
5+
6+
fn main() {
7+
let _ = good::Thing;
8+
let _ = dep_with_mod::with_mod::Thing;
9+
}

0 commit comments

Comments
 (0)