Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 56f4e1c

Browse files
committed
Check if the parent module name contains "test"
1 parent bdc75db commit 56f4e1c

File tree

4 files changed

+46
-19
lines changed

4 files changed

+46
-19
lines changed

clippy_lints/src/wildcard_imports.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl LateLintPass<'_, '_> for WildcardImports {
8484
if !in_macro(item.span);
8585
if let ItemKind::Use(use_path, UseKind::Glob) = &item.kind;
8686
if !is_prelude_import(use_path.segments);
87-
if !is_super_only_import_in_test(use_path.segments);
87+
if !(is_super_only_import(use_path.segments) && is_in_test_module(cx, item));
8888
let used_imports = cx.tcx.names_imported_by_glob_use(item.hir_id.owner);
8989
if !used_imports.is_empty(); // Already handled by `unused_imports`
9090
then {
@@ -109,8 +109,7 @@ impl LateLintPass<'_, '_> for WildcardImports {
109109
span = use_path.span.with_hi(item.span.hi() - BytePos(1));
110110
}
111111
(
112-
span,
113-
false,
112+
span, false,
114113
)
115114
};
116115

@@ -164,8 +163,14 @@ fn is_prelude_import(segments: &[PathSegment<'_>]) -> bool {
164163
.map_or(false, |ps| ps.ident.as_str() == "prelude")
165164
}
166165

167-
// Allow "super::*" imports.
168-
// This is intended primarily to ease the process of writing unit tests.
169-
fn is_super_only_import_in_test(segments: &[PathSegment<'_>]) -> bool {
170-
segments.iter().len() == 1 && segments.first().map_or(false, |ps| ps.ident.as_str() == "super")
166+
// Allow "super::*" imports in tests.
167+
fn is_super_only_import(segments: &[PathSegment<'_>]) -> bool {
168+
segments.len() == 1 && segments[0].ident.as_str() == "super"
169+
}
170+
171+
fn is_in_test_module(cx: &LateContext<'_, '_>, item: &Item<'_>) -> bool {
172+
let parent = cx.tcx.hir().get_parent_node(item.hir_id);
173+
let parent_item = cx.tcx.hir().expect_item(parent);
174+
let parent_name = parent_item.ident.name.as_str();
175+
parent_name.contains("test")
171176
}

tests/ui/wildcard_imports.fixed

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,23 +159,31 @@ fn test_weird_formatting() {
159159
mod test_super_imports {
160160
fn foofoo() {}
161161

162-
mod use_super {
162+
mod use_super_should_be_replaced {
163+
use super::foofoo;
164+
165+
fn with_super() {
166+
let _ = foofoo();
167+
}
168+
}
169+
170+
mod use_super_in_test_should_pass {
163171
use super::*;
164172

165173
fn with_super() {
166174
let _ = foofoo();
167175
}
168176
}
169177

170-
mod use_explicit {
178+
mod use_explicit_should_be_replaced {
171179
use test_super_imports::foofoo;
172180

173181
fn with_explicit() {
174182
let _ = foofoo();
175183
}
176184
}
177185

178-
mod use_double_super {
186+
mod use_double_super_should_be_replaced {
179187
mod inner {
180188
use super::super::foofoo;
181189

@@ -185,7 +193,7 @@ mod test_super_imports {
185193
}
186194
}
187195

188-
mod use_super_explicit {
196+
mod use_super_explicit_should_be_replaced {
189197
use super::super::test_super_imports::foofoo;
190198

191199
fn with_super_explicit() {

tests/ui/wildcard_imports.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,23 +160,31 @@ fn test_weird_formatting() {
160160
mod test_super_imports {
161161
fn foofoo() {}
162162

163-
mod use_super {
163+
mod use_super_should_be_replaced {
164164
use super::*;
165165

166166
fn with_super() {
167167
let _ = foofoo();
168168
}
169169
}
170170

171-
mod use_explicit {
171+
mod use_super_in_test_should_pass {
172+
use super::*;
173+
174+
fn with_super() {
175+
let _ = foofoo();
176+
}
177+
}
178+
179+
mod use_explicit_should_be_replaced {
172180
use test_super_imports::*;
173181

174182
fn with_explicit() {
175183
let _ = foofoo();
176184
}
177185
}
178186

179-
mod use_double_super {
187+
mod use_double_super_should_be_replaced {
180188
mod inner {
181189
use super::super::*;
182190

@@ -186,7 +194,7 @@ mod test_super_imports {
186194
}
187195
}
188196

189-
mod use_super_explicit {
197+
mod use_super_explicit_should_be_replaced {
190198
use super::super::test_super_imports::*;
191199

192200
fn with_super_explicit() {

tests/ui/wildcard_imports.stderr

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,28 @@ LL | | *;
9393
| |_________^ help: try: `crate:: fn_mod::foo`
9494

9595
error: usage of wildcard import
96-
--> $DIR/wildcard_imports.rs:172:13
96+
--> $DIR/wildcard_imports.rs:164:13
97+
|
98+
LL | use super::*;
99+
| ^^^^^^^^ help: try: `super::foofoo`
100+
101+
error: usage of wildcard import
102+
--> $DIR/wildcard_imports.rs:180:13
97103
|
98104
LL | use test_super_imports::*;
99105
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `test_super_imports::foofoo`
100106

101107
error: usage of wildcard import
102-
--> $DIR/wildcard_imports.rs:181:17
108+
--> $DIR/wildcard_imports.rs:189:17
103109
|
104110
LL | use super::super::*;
105111
| ^^^^^^^^^^^^^^^ help: try: `super::super::foofoo`
106112

107113
error: usage of wildcard import
108-
--> $DIR/wildcard_imports.rs:190:13
114+
--> $DIR/wildcard_imports.rs:198:13
109115
|
110116
LL | use super::super::test_super_imports::*;
111117
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `super::super::test_super_imports::foofoo`
112118

113-
error: aborting due to 18 previous errors
119+
error: aborting due to 19 previous errors
114120

0 commit comments

Comments
 (0)