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() {}