From 54912410c71bd2547d008c0a5ba3c8c296f1ae74 Mon Sep 17 00:00:00 2001 From: blyxyas Date: Sun, 2 Apr 2023 01:51:44 +0200 Subject: [PATCH 1/4] `Wildcard_imports` ignore `test.rs` files --- clippy_lints/src/wildcard_imports.rs | 22 ++++++++++++++++++- tests/ui/wildcard_imports/another_file.fixed | 18 +++++++++++++++ tests/ui/wildcard_imports/another_file.rs | 18 +++++++++++++++ tests/ui/wildcard_imports/another_file.stderr | 10 +++++++++ tests/ui/wildcard_imports/test.rs | 17 ++++++++++++++ tests/ui/wildcard_imports/tests.rs | 17 ++++++++++++++ 6 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 tests/ui/wildcard_imports/another_file.fixed create mode 100644 tests/ui/wildcard_imports/another_file.rs create mode 100644 tests/ui/wildcard_imports/another_file.stderr create mode 100644 tests/ui/wildcard_imports/test.rs create mode 100644 tests/ui/wildcard_imports/tests.rs diff --git a/clippy_lints/src/wildcard_imports.rs b/clippy_lints/src/wildcard_imports.rs index 36f910c983f6..f062cafdd4fa 100644 --- a/clippy_lints/src/wildcard_imports.rs +++ b/clippy_lints/src/wildcard_imports.rs @@ -7,7 +7,8 @@ use rustc_hir::{ def::{DefKind, Res}, Item, ItemKind, PathSegment, UseKind, }; -use rustc_lint::{LateContext, LateLintPass}; +use rustc_hir::{HirId, Mod}; +use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::ty; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::symbol::kw; @@ -102,6 +103,7 @@ declare_clippy_lint! { pub struct WildcardImports { warn_on_all: bool, test_modules_deep: u32, + ignore: bool, } impl WildcardImports { @@ -109,6 +111,7 @@ impl WildcardImports { Self { warn_on_all, test_modules_deep: 0, + ignore: false, } } } @@ -116,7 +119,24 @@ impl WildcardImports { impl_lint_pass!(WildcardImports => [ENUM_GLOB_USE, WILDCARD_IMPORTS]); impl LateLintPass<'_> for WildcardImports { + fn check_mod(&mut self, cx: &LateContext<'_>, module: &Mod<'_>, _: HirId) { + let filename = cx + .sess() + .source_map() + .span_to_filename(module.spans.inner_span) + .display(rustc_span::FileNameDisplayPreference::Local) + .to_string(); + + if filename.ends_with("test.rs") || filename.ends_with("tests.rs") { + self.ignore = true; + } + } + fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { + if self.ignore { + return; + } + if is_test_module_or_function(cx.tcx, item) { self.test_modules_deep = self.test_modules_deep.saturating_add(1); } diff --git a/tests/ui/wildcard_imports/another_file.fixed b/tests/ui/wildcard_imports/another_file.fixed new file mode 100644 index 000000000000..726808e598f4 --- /dev/null +++ b/tests/ui/wildcard_imports/another_file.fixed @@ -0,0 +1,18 @@ +// run-rustfix +#![warn(clippy::wildcard_imports)] +#![allow(unused, clippy::unnecessary_wraps, clippy::let_unit_value)] + +// Test for #10580, the lint should **not** ignore it. + +fn foofoo() {} + +mod outer { + mod inner { + use super::super::foofoo; + fn barbar() { + let _ = foofoo(); + } + } +} + +fn main() {} diff --git a/tests/ui/wildcard_imports/another_file.rs b/tests/ui/wildcard_imports/another_file.rs new file mode 100644 index 000000000000..057332ef7060 --- /dev/null +++ b/tests/ui/wildcard_imports/another_file.rs @@ -0,0 +1,18 @@ +// run-rustfix +#![warn(clippy::wildcard_imports)] +#![allow(unused, clippy::unnecessary_wraps, clippy::let_unit_value)] + +// Test for #10580, the lint should **not** ignore it. + +fn foofoo() {} + +mod outer { + mod inner { + use super::super::*; + fn barbar() { + let _ = foofoo(); + } + } +} + +fn main() {} diff --git a/tests/ui/wildcard_imports/another_file.stderr b/tests/ui/wildcard_imports/another_file.stderr new file mode 100644 index 000000000000..56923eff58b2 --- /dev/null +++ b/tests/ui/wildcard_imports/another_file.stderr @@ -0,0 +1,10 @@ +error: usage of wildcard import + --> $DIR/another_file.rs:11:13 + | +LL | use super::super::*; + | ^^^^^^^^^^^^^^^ help: try: `super::super::foofoo` + | + = note: `-D clippy::wildcard-imports` implied by `-D warnings` + +error: aborting due to previous error + diff --git a/tests/ui/wildcard_imports/test.rs b/tests/ui/wildcard_imports/test.rs new file mode 100644 index 000000000000..9029e5ba503a --- /dev/null +++ b/tests/ui/wildcard_imports/test.rs @@ -0,0 +1,17 @@ +#![warn(clippy::wildcard_imports)] +#![allow(unused, clippy::unnecessary_wraps, clippy::let_unit_value)] + +// Test for #10580, the lint **should** not ignore it. + +fn foofoo() {} + +mod outer { + mod inner { + use super::super::*; + fn barbar() { + let _ = foofoo(); + } + } +} + +fn main() {} diff --git a/tests/ui/wildcard_imports/tests.rs b/tests/ui/wildcard_imports/tests.rs new file mode 100644 index 000000000000..b7483853388e --- /dev/null +++ b/tests/ui/wildcard_imports/tests.rs @@ -0,0 +1,17 @@ +#![warn(clippy::wildcard_imports)] +#![allow(unused, clippy::unnecessary_wraps, clippy::let_unit_value)] + +// Test for #10580, the lint **should** ignore it. + +fn foofoo() {} + +mod outer { + mod inner { + use super::super::*; + fn barbar() { + let _ = foofoo(); + } + } +} + +fn main() {} From 5acc2993e74506780e0a20331f58e78e125aad92 Mon Sep 17 00:00:00 2001 From: blyxyas Date: Mon, 3 Apr 2023 14:16:46 +0200 Subject: [PATCH 2/4] Really mini minor irrelevant change for formatting --- clippy_lints/src/wildcard_imports.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/clippy_lints/src/wildcard_imports.rs b/clippy_lints/src/wildcard_imports.rs index f062cafdd4fa..53ab9e946316 100644 --- a/clippy_lints/src/wildcard_imports.rs +++ b/clippy_lints/src/wildcard_imports.rs @@ -127,9 +127,7 @@ impl LateLintPass<'_> for WildcardImports { .display(rustc_span::FileNameDisplayPreference::Local) .to_string(); - if filename.ends_with("test.rs") || filename.ends_with("tests.rs") { - self.ignore = true; - } + self.ignore = filename.ends_with("test.rs") || filename.ends_with("tests.rs"); } fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { From ba0e7e88cb69587bc1e639509999d3f39ee0dae2 Mon Sep 17 00:00:00 2001 From: blyxyas Date: Wed, 5 Apr 2023 20:38:44 +0200 Subject: [PATCH 3/4] Now the lint ignores any crates with `--cfg test` --- clippy_lints/src/wildcard_imports.rs | 14 ++++---------- tests/ui/wildcard_imports/another_file.fixed | 18 ------------------ tests/ui/wildcard_imports/another_file.stderr | 10 ---------- tests/ui/wildcard_imports/test.rs | 17 ----------------- tests/ui/wildcard_imports/tests.rs | 17 ----------------- ...her_file.rs => wildcard_imports_cfgtest.rs} | 5 +++-- 6 files changed, 7 insertions(+), 74 deletions(-) delete mode 100644 tests/ui/wildcard_imports/another_file.fixed delete mode 100644 tests/ui/wildcard_imports/another_file.stderr delete mode 100644 tests/ui/wildcard_imports/test.rs delete mode 100644 tests/ui/wildcard_imports/tests.rs rename tests/ui/{wildcard_imports/another_file.rs => wildcard_imports_cfgtest.rs} (70%) diff --git a/clippy_lints/src/wildcard_imports.rs b/clippy_lints/src/wildcard_imports.rs index 53ab9e946316..0cae6e00a95b 100644 --- a/clippy_lints/src/wildcard_imports.rs +++ b/clippy_lints/src/wildcard_imports.rs @@ -7,7 +7,6 @@ use rustc_hir::{ def::{DefKind, Res}, Item, ItemKind, PathSegment, UseKind, }; -use rustc_hir::{HirId, Mod}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::ty; use rustc_session::{declare_tool_lint, impl_lint_pass}; @@ -119,15 +118,10 @@ impl WildcardImports { impl_lint_pass!(WildcardImports => [ENUM_GLOB_USE, WILDCARD_IMPORTS]); impl LateLintPass<'_> for WildcardImports { - fn check_mod(&mut self, cx: &LateContext<'_>, module: &Mod<'_>, _: HirId) { - let filename = cx - .sess() - .source_map() - .span_to_filename(module.spans.inner_span) - .display(rustc_span::FileNameDisplayPreference::Local) - .to_string(); - - self.ignore = filename.ends_with("test.rs") || filename.ends_with("tests.rs"); + fn check_crate(&mut self, cx: &LateContext<'_>) { + if cx.sess().opts.test { + self.ignore = true; + } } fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { diff --git a/tests/ui/wildcard_imports/another_file.fixed b/tests/ui/wildcard_imports/another_file.fixed deleted file mode 100644 index 726808e598f4..000000000000 --- a/tests/ui/wildcard_imports/another_file.fixed +++ /dev/null @@ -1,18 +0,0 @@ -// run-rustfix -#![warn(clippy::wildcard_imports)] -#![allow(unused, clippy::unnecessary_wraps, clippy::let_unit_value)] - -// Test for #10580, the lint should **not** ignore it. - -fn foofoo() {} - -mod outer { - mod inner { - use super::super::foofoo; - fn barbar() { - let _ = foofoo(); - } - } -} - -fn main() {} diff --git a/tests/ui/wildcard_imports/another_file.stderr b/tests/ui/wildcard_imports/another_file.stderr deleted file mode 100644 index 56923eff58b2..000000000000 --- a/tests/ui/wildcard_imports/another_file.stderr +++ /dev/null @@ -1,10 +0,0 @@ -error: usage of wildcard import - --> $DIR/another_file.rs:11:13 - | -LL | use super::super::*; - | ^^^^^^^^^^^^^^^ help: try: `super::super::foofoo` - | - = note: `-D clippy::wildcard-imports` implied by `-D warnings` - -error: aborting due to previous error - diff --git a/tests/ui/wildcard_imports/test.rs b/tests/ui/wildcard_imports/test.rs deleted file mode 100644 index 9029e5ba503a..000000000000 --- a/tests/ui/wildcard_imports/test.rs +++ /dev/null @@ -1,17 +0,0 @@ -#![warn(clippy::wildcard_imports)] -#![allow(unused, clippy::unnecessary_wraps, clippy::let_unit_value)] - -// Test for #10580, the lint **should** not ignore it. - -fn foofoo() {} - -mod outer { - mod inner { - use super::super::*; - fn barbar() { - let _ = foofoo(); - } - } -} - -fn main() {} diff --git a/tests/ui/wildcard_imports/tests.rs b/tests/ui/wildcard_imports/tests.rs deleted file mode 100644 index b7483853388e..000000000000 --- a/tests/ui/wildcard_imports/tests.rs +++ /dev/null @@ -1,17 +0,0 @@ -#![warn(clippy::wildcard_imports)] -#![allow(unused, clippy::unnecessary_wraps, clippy::let_unit_value)] - -// Test for #10580, the lint **should** ignore it. - -fn foofoo() {} - -mod outer { - mod inner { - use super::super::*; - fn barbar() { - let _ = foofoo(); - } - } -} - -fn main() {} diff --git a/tests/ui/wildcard_imports/another_file.rs b/tests/ui/wildcard_imports_cfgtest.rs similarity index 70% rename from tests/ui/wildcard_imports/another_file.rs rename to tests/ui/wildcard_imports_cfgtest.rs index 057332ef7060..a5e4e92b32c0 100644 --- a/tests/ui/wildcard_imports/another_file.rs +++ b/tests/ui/wildcard_imports_cfgtest.rs @@ -1,8 +1,9 @@ -// run-rustfix +// compile-flags: --test + #![warn(clippy::wildcard_imports)] #![allow(unused, clippy::unnecessary_wraps, clippy::let_unit_value)] -// Test for #10580, the lint should **not** ignore it. +// Test for #10580, the lint should ignore it because of the crate's cfg test flag. fn foofoo() {} From 4c3e2ff2a40a54bc37669bbdb0861a50ec418ff3 Mon Sep 17 00:00:00 2001 From: blyxyas Date: Mon, 8 May 2023 18:38:41 +0200 Subject: [PATCH 4/4] Fix header --- clippy_lints/src/wildcard_imports.rs | 10 +--------- tests/ui/wildcard_imports_cfgtest.rs | 2 +- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/clippy_lints/src/wildcard_imports.rs b/clippy_lints/src/wildcard_imports.rs index 0cae6e00a95b..a9089fba3c53 100644 --- a/clippy_lints/src/wildcard_imports.rs +++ b/clippy_lints/src/wildcard_imports.rs @@ -102,7 +102,6 @@ declare_clippy_lint! { pub struct WildcardImports { warn_on_all: bool, test_modules_deep: u32, - ignore: bool, } impl WildcardImports { @@ -110,7 +109,6 @@ impl WildcardImports { Self { warn_on_all, test_modules_deep: 0, - ignore: false, } } } @@ -118,14 +116,8 @@ impl WildcardImports { impl_lint_pass!(WildcardImports => [ENUM_GLOB_USE, WILDCARD_IMPORTS]); impl LateLintPass<'_> for WildcardImports { - fn check_crate(&mut self, cx: &LateContext<'_>) { - if cx.sess().opts.test { - self.ignore = true; - } - } - fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { - if self.ignore { + if cx.sess().is_test_crate() { return; } diff --git a/tests/ui/wildcard_imports_cfgtest.rs b/tests/ui/wildcard_imports_cfgtest.rs index a5e4e92b32c0..203c4e15b50c 100644 --- a/tests/ui/wildcard_imports_cfgtest.rs +++ b/tests/ui/wildcard_imports_cfgtest.rs @@ -1,4 +1,4 @@ -// compile-flags: --test +//@compile-flags: --test #![warn(clippy::wildcard_imports)] #![allow(unused, clippy::unnecessary_wraps, clippy::let_unit_value)]