about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--clippy_lints/src/wildcard_imports.rs19
-rw-r--r--tests/ui/wildcard_imports.fixed16
-rw-r--r--tests/ui/wildcard_imports.rs16
-rw-r--r--tests/ui/wildcard_imports.stderr14
4 files changed, 46 insertions, 19 deletions
diff --git a/clippy_lints/src/wildcard_imports.rs b/clippy_lints/src/wildcard_imports.rs
index e7400642b07..a22d0e6775d 100644
--- a/clippy_lints/src/wildcard_imports.rs
+++ b/clippy_lints/src/wildcard_imports.rs
@@ -84,7 +84,7 @@ impl LateLintPass<'_, '_> for WildcardImports {
             if !in_macro(item.span);
             if let ItemKind::Use(use_path, UseKind::Glob) = &item.kind;
             if !is_prelude_import(use_path.segments);
-            if !is_super_only_import_in_test(use_path.segments);
+            if !(is_super_only_import(use_path.segments) && is_in_test_module(cx, item));
             let used_imports = cx.tcx.names_imported_by_glob_use(item.hir_id.owner);
             if !used_imports.is_empty(); // Already handled by `unused_imports`
             then {
@@ -109,8 +109,7 @@ impl LateLintPass<'_, '_> for WildcardImports {
                         span = use_path.span.with_hi(item.span.hi() - BytePos(1));
                     }
                     (
-                        span,
-                        false,
+                        span, false,
                     )
                 };
 
@@ -164,8 +163,14 @@ fn is_prelude_import(segments: &[PathSegment<'_>]) -> bool {
         .map_or(false, |ps| ps.ident.as_str() == "prelude")
 }
 
-// Allow "super::*" imports.
-// This is intended primarily to ease the process of writing unit tests.
-fn is_super_only_import_in_test(segments: &[PathSegment<'_>]) -> bool {
-    segments.iter().len() == 1 && segments.first().map_or(false, |ps| ps.ident.as_str() == "super")
+// Allow "super::*" imports in tests.
+fn is_super_only_import(segments: &[PathSegment<'_>]) -> bool {
+    segments.len() == 1 && segments[0].ident.as_str() == "super"
+}
+
+fn is_in_test_module(cx: &LateContext<'_, '_>, item: &Item<'_>) -> bool {
+    let parent = cx.tcx.hir().get_parent_node(item.hir_id);
+    let parent_item = cx.tcx.hir().expect_item(parent);
+    let parent_name = parent_item.ident.name.as_str();
+    parent_name.contains("test")
 }
diff --git a/tests/ui/wildcard_imports.fixed b/tests/ui/wildcard_imports.fixed
index 003f11009a0..1c5c01f65d1 100644
--- a/tests/ui/wildcard_imports.fixed
+++ b/tests/ui/wildcard_imports.fixed
@@ -159,7 +159,15 @@ fn test_weird_formatting() {
 mod test_super_imports {
     fn foofoo() {}
 
-    mod use_super {
+    mod use_super_should_be_replaced {
+        use super::foofoo;
+
+        fn with_super() {
+            let _ = foofoo();
+        }
+    }
+
+    mod use_super_in_test_should_pass {
         use super::*;
 
         fn with_super() {
@@ -167,7 +175,7 @@ mod test_super_imports {
         }
     }
 
-    mod use_explicit {
+    mod use_explicit_should_be_replaced {
         use test_super_imports::foofoo;
 
         fn with_explicit() {
@@ -175,7 +183,7 @@ mod test_super_imports {
         }
     }
 
-    mod use_double_super {
+    mod use_double_super_should_be_replaced {
         mod inner {
             use super::super::foofoo;
 
@@ -185,7 +193,7 @@ mod test_super_imports {
         }
     }
 
-    mod use_super_explicit {
+    mod use_super_explicit_should_be_replaced {
         use super::super::test_super_imports::foofoo;
 
         fn with_super_explicit() {
diff --git a/tests/ui/wildcard_imports.rs b/tests/ui/wildcard_imports.rs
index 7bd57c7965a..f783149ef93 100644
--- a/tests/ui/wildcard_imports.rs
+++ b/tests/ui/wildcard_imports.rs
@@ -160,7 +160,7 @@ fn test_weird_formatting() {
 mod test_super_imports {
     fn foofoo() {}
 
-    mod use_super {
+    mod use_super_should_be_replaced {
         use super::*;
 
         fn with_super() {
@@ -168,7 +168,15 @@ mod test_super_imports {
         }
     }
 
-    mod use_explicit {
+    mod use_super_in_test_should_pass {
+        use super::*;
+
+        fn with_super() {
+            let _ = foofoo();
+        }
+    }
+
+    mod use_explicit_should_be_replaced {
         use test_super_imports::*;
 
         fn with_explicit() {
@@ -176,7 +184,7 @@ mod test_super_imports {
         }
     }
 
-    mod use_double_super {
+    mod use_double_super_should_be_replaced {
         mod inner {
             use super::super::*;
 
@@ -186,7 +194,7 @@ mod test_super_imports {
         }
     }
 
-    mod use_super_explicit {
+    mod use_super_explicit_should_be_replaced {
         use super::super::test_super_imports::*;
 
         fn with_super_explicit() {
diff --git a/tests/ui/wildcard_imports.stderr b/tests/ui/wildcard_imports.stderr
index 858dc28797f..649d550a88d 100644
--- a/tests/ui/wildcard_imports.stderr
+++ b/tests/ui/wildcard_imports.stderr
@@ -93,22 +93,28 @@ LL | |         *;
    | |_________^ help: try: `crate:: fn_mod::foo`
 
 error: usage of wildcard import
-  --> $DIR/wildcard_imports.rs:172:13
+  --> $DIR/wildcard_imports.rs:164:13
+   |
+LL |         use super::*;
+   |             ^^^^^^^^ help: try: `super::foofoo`
+
+error: usage of wildcard import
+  --> $DIR/wildcard_imports.rs:180:13
    |
 LL |         use test_super_imports::*;
    |             ^^^^^^^^^^^^^^^^^^^^^ help: try: `test_super_imports::foofoo`
 
 error: usage of wildcard import
-  --> $DIR/wildcard_imports.rs:181:17
+  --> $DIR/wildcard_imports.rs:189:17
    |
 LL |             use super::super::*;
    |                 ^^^^^^^^^^^^^^^ help: try: `super::super::foofoo`
 
 error: usage of wildcard import
-  --> $DIR/wildcard_imports.rs:190:13
+  --> $DIR/wildcard_imports.rs:198:13
    |
 LL |         use super::super::test_super_imports::*;
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `super::super::test_super_imports::foofoo`
 
-error: aborting due to 18 previous errors
+error: aborting due to 19 previous errors