From 97e5a70f472db51ea319367b5b424a55a77a2187 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Thu, 20 Jan 2022 23:00:50 +0100 Subject: [PATCH 1/3] warn if we find multiple clippy configs Fixes #8323 --- clippy_lints/src/utils/conf.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index c9d99617c1e..36a7e67af6f 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -322,6 +322,9 @@ pub fn lookup_conf_file() -> io::Result> { let mut current = env::var_os("CLIPPY_CONF_DIR") .or_else(|| env::var_os("CARGO_MANIFEST_DIR")) .map_or_else(|| PathBuf::from("."), PathBuf::from); + + let mut found_config: Option = None; + loop { for config_file_name in &CONFIG_FILE_NAMES { if let Ok(config_file) = current.join(config_file_name).canonicalize() { @@ -329,11 +332,26 @@ pub fn lookup_conf_file() -> io::Result> { Err(e) if e.kind() == io::ErrorKind::NotFound => {}, Err(e) => return Err(e), Ok(md) if md.is_dir() => {}, - Ok(_) => return Ok(Some(config_file)), + Ok(_) => { + // warn if we happen to find two config files + if let Some(ref found_config_) = found_config { + eprintln!( + "Warning: found two config files: {} and {}.\nUsing the first one!", + found_config_.display(), + config_file.display(), + ); + } else { + found_config = Some(config_file); + } + }, } } } + if found_config.is_some() { + return Ok(found_config); + } + // If the current directory has no parent, we're done searching. if !current.pop() { return Ok(None); From 1193abe4c85ecd57f1a5634629874fb153a180ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Sun, 30 Jan 2022 13:32:35 +0100 Subject: [PATCH 2/3] multiple configs: add tests --- clippy_lints/src/utils/conf.rs | 4 ++-- tests/ui-cargo/multiple_config_files/no_warn/Cargo.toml | 8 ++++++++ tests/ui-cargo/multiple_config_files/no_warn/clippy.toml | 1 + tests/ui-cargo/multiple_config_files/no_warn/src/main.rs | 3 +++ tests/ui-cargo/multiple_config_files/warn/.clippy.toml | 1 + tests/ui-cargo/multiple_config_files/warn/Cargo.toml | 8 ++++++++ tests/ui-cargo/multiple_config_files/warn/clippy.toml | 1 + tests/ui-cargo/multiple_config_files/warn/src/main.rs | 3 +++ tests/ui-cargo/multiple_config_files/warn/src/main.stderr | 2 ++ 9 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 tests/ui-cargo/multiple_config_files/no_warn/Cargo.toml create mode 100644 tests/ui-cargo/multiple_config_files/no_warn/clippy.toml create mode 100644 tests/ui-cargo/multiple_config_files/no_warn/src/main.rs create mode 100644 tests/ui-cargo/multiple_config_files/warn/.clippy.toml create mode 100644 tests/ui-cargo/multiple_config_files/warn/Cargo.toml create mode 100644 tests/ui-cargo/multiple_config_files/warn/clippy.toml create mode 100644 tests/ui-cargo/multiple_config_files/warn/src/main.rs create mode 100644 tests/ui-cargo/multiple_config_files/warn/src/main.stderr diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index 36a7e67af6f..680b2eb1da7 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -333,10 +333,10 @@ pub fn lookup_conf_file() -> io::Result> { Err(e) => return Err(e), Ok(md) if md.is_dir() => {}, Ok(_) => { - // warn if we happen to find two config files + // warn if we happen to find two config files #8323 if let Some(ref found_config_) = found_config { eprintln!( - "Warning: found two config files: {} and {}.\nUsing the first one!", + "Using config file `{}`\nWarning: `{}` will be ignored.", found_config_.display(), config_file.display(), ); diff --git a/tests/ui-cargo/multiple_config_files/no_warn/Cargo.toml b/tests/ui-cargo/multiple_config_files/no_warn/Cargo.toml new file mode 100644 index 00000000000..79c973cbfd2 --- /dev/null +++ b/tests/ui-cargo/multiple_config_files/no_warn/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "no_warn" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/tests/ui-cargo/multiple_config_files/no_warn/clippy.toml b/tests/ui-cargo/multiple_config_files/no_warn/clippy.toml new file mode 100644 index 00000000000..cda8d17eed4 --- /dev/null +++ b/tests/ui-cargo/multiple_config_files/no_warn/clippy.toml @@ -0,0 +1 @@ +avoid-breaking-exported-api = false diff --git a/tests/ui-cargo/multiple_config_files/no_warn/src/main.rs b/tests/ui-cargo/multiple_config_files/no_warn/src/main.rs new file mode 100644 index 00000000000..e7a11a969c0 --- /dev/null +++ b/tests/ui-cargo/multiple_config_files/no_warn/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/tests/ui-cargo/multiple_config_files/warn/.clippy.toml b/tests/ui-cargo/multiple_config_files/warn/.clippy.toml new file mode 100644 index 00000000000..cda8d17eed4 --- /dev/null +++ b/tests/ui-cargo/multiple_config_files/warn/.clippy.toml @@ -0,0 +1 @@ +avoid-breaking-exported-api = false diff --git a/tests/ui-cargo/multiple_config_files/warn/Cargo.toml b/tests/ui-cargo/multiple_config_files/warn/Cargo.toml new file mode 100644 index 00000000000..3d5c707579b --- /dev/null +++ b/tests/ui-cargo/multiple_config_files/warn/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "warn" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/tests/ui-cargo/multiple_config_files/warn/clippy.toml b/tests/ui-cargo/multiple_config_files/warn/clippy.toml new file mode 100644 index 00000000000..cda8d17eed4 --- /dev/null +++ b/tests/ui-cargo/multiple_config_files/warn/clippy.toml @@ -0,0 +1 @@ +avoid-breaking-exported-api = false diff --git a/tests/ui-cargo/multiple_config_files/warn/src/main.rs b/tests/ui-cargo/multiple_config_files/warn/src/main.rs new file mode 100644 index 00000000000..e7a11a969c0 --- /dev/null +++ b/tests/ui-cargo/multiple_config_files/warn/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/tests/ui-cargo/multiple_config_files/warn/src/main.stderr b/tests/ui-cargo/multiple_config_files/warn/src/main.stderr new file mode 100644 index 00000000000..2abb4e3e06e --- /dev/null +++ b/tests/ui-cargo/multiple_config_files/warn/src/main.stderr @@ -0,0 +1,2 @@ +Using config file `$SRC_DIR/tests/ui-cargo/multiple_config_files/warn/.clippy.toml` +Warning: `$SRC_DIR/tests/ui-cargo/multiple_config_files/warn/clippy.toml` will be ignored. From aae64e93b7ad9de1da74623cb961322f65f8600f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Wed, 2 Feb 2022 21:21:20 +0100 Subject: [PATCH 3/3] ignore test on windows since I don't know why compiletest does not hand paths properly there --- tests/ui-cargo/multiple_config_files/warn/src/main.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/ui-cargo/multiple_config_files/warn/src/main.rs b/tests/ui-cargo/multiple_config_files/warn/src/main.rs index e7a11a969c0..2d0b4a7948c 100644 --- a/tests/ui-cargo/multiple_config_files/warn/src/main.rs +++ b/tests/ui-cargo/multiple_config_files/warn/src/main.rs @@ -1,3 +1,5 @@ +// ignore-windows + fn main() { println!("Hello, world!"); }