about summary refs log tree commit diff
diff options
context:
space:
mode:
authorblyxyas <blyxyas@gmail.com>2023-04-02 01:51:44 +0200
committerblyxyas <blyxyas@gmail.com>2023-05-08 18:24:58 +0200
commit54912410c71bd2547d008c0a5ba3c8c296f1ae74 (patch)
tree3347b27fc9fe885711105a61338db44febd15cc2
parente66488a66917d2741ff7a862fb2cdbb38ab4015e (diff)
downloadrust-54912410c71bd2547d008c0a5ba3c8c296f1ae74.tar.gz
rust-54912410c71bd2547d008c0a5ba3c8c296f1ae74.zip
`Wildcard_imports` ignore `test.rs` files
-rw-r--r--clippy_lints/src/wildcard_imports.rs22
-rw-r--r--tests/ui/wildcard_imports/another_file.fixed18
-rw-r--r--tests/ui/wildcard_imports/another_file.rs18
-rw-r--r--tests/ui/wildcard_imports/another_file.stderr10
-rw-r--r--tests/ui/wildcard_imports/test.rs17
-rw-r--r--tests/ui/wildcard_imports/tests.rs17
6 files changed, 101 insertions, 1 deletions
diff --git a/clippy_lints/src/wildcard_imports.rs b/clippy_lints/src/wildcard_imports.rs
index 36f910c983f..f062cafdd4f 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 00000000000..726808e598f
--- /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 00000000000..057332ef706
--- /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 00000000000..56923eff58b
--- /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 00000000000..9029e5ba503
--- /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 00000000000..b7483853388
--- /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() {}