mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-10 07:04:18 +00:00
Fix false positive of [self_named_module_files] and [mod_module_files]
This commit is contained in:
parent
8c8ff5f31d
commit
65b93a5b43
22 changed files with 138 additions and 1 deletions
|
@ -2,6 +2,7 @@ use rustc_ast::ast;
|
||||||
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
||||||
use rustc_lint::{EarlyContext, EarlyLintPass, Level, LintContext};
|
use rustc_lint::{EarlyContext, EarlyLintPass, Level, LintContext};
|
||||||
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
||||||
|
use rustc_span::def_id::LOCAL_CRATE;
|
||||||
use rustc_span::{FileName, SourceFile, Span, SyntaxContext};
|
use rustc_span::{FileName, SourceFile, Span, SyntaxContext};
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::path::{Component, Path};
|
use std::path::{Component, Path};
|
||||||
|
@ -91,6 +92,13 @@ impl EarlyLintPass for ModStyle {
|
||||||
let mut file_map = FxHashMap::default();
|
let mut file_map = FxHashMap::default();
|
||||||
for file in files.iter() {
|
for file in files.iter() {
|
||||||
if let FileName::Real(name) = &file.name && let Some(lp) = name.local_path() {
|
if let FileName::Real(name) = &file.name && let Some(lp) = name.local_path() {
|
||||||
|
if file.cnum != LOCAL_CRATE {
|
||||||
|
// [#8887](https://github.com/rust-lang/rust-clippy/issues/8887)
|
||||||
|
// Only check files in the current crate.
|
||||||
|
// Fix false positive that crate dependency in workspace sub directory
|
||||||
|
// is checked unintentionally.
|
||||||
|
continue;
|
||||||
|
}
|
||||||
let path = if lp.is_relative() {
|
let path = if lp.is_relative() {
|
||||||
lp
|
lp
|
||||||
} else if let Ok(relative) = lp.strip_prefix(trim_to_src) {
|
} else if let Ok(relative) = lp.strip_prefix(trim_to_src) {
|
||||||
|
|
|
@ -6,6 +6,46 @@ use test_utils::{CARGO_CLIPPY_PATH, IS_RUSTC_TEST_SUITE};
|
||||||
|
|
||||||
mod test_utils;
|
mod test_utils;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_module_style_with_dep_in_subdir() {
|
||||||
|
if IS_RUSTC_TEST_SUITE {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||||
|
let target_dir = root.join("target").join("workspace_test");
|
||||||
|
let cwd = root.join("tests/workspace_test");
|
||||||
|
|
||||||
|
// Make sure we start with a clean state
|
||||||
|
Command::new("cargo")
|
||||||
|
.current_dir(&cwd)
|
||||||
|
.env("CARGO_TARGET_DIR", &target_dir)
|
||||||
|
.arg("clean")
|
||||||
|
.args(["-p", "pass-no-mod-with-dep-in-subdir"])
|
||||||
|
.args(["-p", "pass-mod-with-dep-in-subdir"])
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// [#8887](https://github.com/rust-lang/rust-clippy/issues/8887)
|
||||||
|
// `mod.rs` checks should not be applied to crate dependencies
|
||||||
|
// located in the subdirectory of workspace
|
||||||
|
let output = Command::new(&*CARGO_CLIPPY_PATH)
|
||||||
|
.current_dir(&cwd)
|
||||||
|
.env("CARGO_INCREMENTAL", "0")
|
||||||
|
.env("CARGO_TARGET_DIR", &target_dir)
|
||||||
|
.arg("clippy")
|
||||||
|
.args(["-p", "pass-no-mod-with-dep-in-subdir"])
|
||||||
|
.args(["-p", "pass-mod-with-dep-in-subdir"])
|
||||||
|
.arg("--")
|
||||||
|
.arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
println!("status: {}", output.status);
|
||||||
|
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
|
||||||
|
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
|
||||||
|
assert!(output.status.success());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_no_deps_ignores_path_deps_in_workspaces() {
|
fn test_no_deps_ignores_path_deps_in_workspaces() {
|
||||||
if IS_RUSTC_TEST_SUITE {
|
if IS_RUSTC_TEST_SUITE {
|
||||||
|
|
|
@ -4,4 +4,4 @@ version = "0.1.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
members = ["subcrate"]
|
members = ["subcrate", "module_style/pass_no_mod_with_dep_in_subdir", "module_style/pass_mod_with_dep_in_subdir"]
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
[package]
|
||||||
|
name = "pass-mod-with-dep-in-subdir"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2018"
|
||||||
|
publish = false
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
dep-no-mod = { path = "dep_no_mod"}
|
|
@ -0,0 +1,9 @@
|
||||||
|
[package]
|
||||||
|
name = "dep-no-mod"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2018"
|
||||||
|
publish = false
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1,2 @@
|
||||||
|
pub mod hello;
|
||||||
|
pub struct Thing;
|
|
@ -0,0 +1 @@
|
||||||
|
pub struct Hello;
|
|
@ -0,0 +1,5 @@
|
||||||
|
pub mod foo;
|
||||||
|
|
||||||
|
pub fn foo() {
|
||||||
|
let _ = foo::Thing;
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
pub struct Thing;
|
|
@ -0,0 +1,13 @@
|
||||||
|
#![deny(clippy::self_named_module_files)]
|
||||||
|
|
||||||
|
mod bad;
|
||||||
|
mod more;
|
||||||
|
extern crate dep_no_mod;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let _ = bad::Thing;
|
||||||
|
let _ = more::foo::Foo;
|
||||||
|
let _ = more::inner::Inner;
|
||||||
|
let _ = dep_no_mod::foo::Thing;
|
||||||
|
let _ = dep_no_mod::foo::hello::Hello;
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
pub struct Foo;
|
|
@ -0,0 +1 @@
|
||||||
|
pub struct Inner;
|
|
@ -0,0 +1,2 @@
|
||||||
|
pub mod foo;
|
||||||
|
pub mod inner;
|
|
@ -0,0 +1,10 @@
|
||||||
|
[package]
|
||||||
|
name = "pass-no-mod-with-dep-in-subdir"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2018"
|
||||||
|
publish = false
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
dep-with-mod = { path = "dep_with_mod"}
|
|
@ -0,0 +1,9 @@
|
||||||
|
[package]
|
||||||
|
name = "dep-with-mod"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2018"
|
||||||
|
publish = false
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1,7 @@
|
||||||
|
pub mod with_mod;
|
||||||
|
|
||||||
|
pub fn foo() {
|
||||||
|
let _ = with_mod::Thing;
|
||||||
|
let _ = with_mod::inner::stuff::Inner;
|
||||||
|
let _ = with_mod::inner::stuff::most::Snarks;
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
pub mod stuff;
|
|
@ -0,0 +1,3 @@
|
||||||
|
pub mod most;
|
||||||
|
|
||||||
|
pub struct Inner;
|
|
@ -0,0 +1 @@
|
||||||
|
pub struct Snarks;
|
|
@ -0,0 +1,3 @@
|
||||||
|
pub mod inner;
|
||||||
|
|
||||||
|
pub struct Thing;
|
|
@ -0,0 +1 @@
|
||||||
|
pub struct Thing;
|
|
@ -0,0 +1,9 @@
|
||||||
|
#![deny(clippy::mod_module_files)]
|
||||||
|
|
||||||
|
mod good;
|
||||||
|
pub use dep_with_mod::with_mod::Thing;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let _ = good::Thing;
|
||||||
|
let _ = dep_with_mod::with_mod::Thing;
|
||||||
|
}
|
Loading…
Reference in a new issue