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

Commit 0c14ea8

Browse files
committed
Allow 'use super::*;' imports
1 parent 43d9cdc commit 0c14ea8

File tree

4 files changed

+111
-4
lines changed

4 files changed

+111
-4
lines changed

clippy_lints/src/wildcard_imports.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use if_chain::if_chain;
33
use rustc_errors::Applicability;
44
use rustc_hir::{
55
def::{DefKind, Res},
6-
Item, ItemKind, UseKind,
6+
Item, ItemKind, PathSegment, UseKind,
77
};
88
use rustc_lint::{LateContext, LateLintPass};
99
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -83,8 +83,8 @@ impl LateLintPass<'_, '_> for WildcardImports {
8383
if_chain! {
8484
if !in_macro(item.span);
8585
if let ItemKind::Use(use_path, UseKind::Glob) = &item.kind;
86-
// don't lint prelude glob imports
87-
if !use_path.segments.iter().last().map_or(false, |ps| ps.ident.as_str() == "prelude");
86+
if !is_prelude_import(use_path.segments);
87+
if !is_super_only_import_in_test(use_path.segments);
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 {
@@ -154,3 +154,16 @@ impl LateLintPass<'_, '_> for WildcardImports {
154154
}
155155
}
156156
}
157+
158+
// Allow "...prelude::*" imports.
159+
// Many crates have a prelude, and it is imported as a glob by design.
160+
fn is_prelude_import(segments: &[PathSegment<'_>]) -> bool {
161+
segments.iter().last().map_or(false, |ps| ps.ident.as_str() == "prelude")
162+
}
163+
164+
// Allow "super::*" imports.
165+
// This is intended primarily to ease the process of writing unit tests.
166+
fn is_super_only_import_in_test(segments: &[PathSegment<'_>]) -> bool {
167+
segments.iter().len() == 1 &&
168+
segments.first().map_or(false, |ps| ps.ident.as_str() == "super")
169+
}

tests/ui/wildcard_imports.fixed

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,41 @@ fn test_weird_formatting() {
155155
exported();
156156
foo();
157157
}
158+
159+
mod test_super_imports {
160+
fn foofoo() {}
161+
162+
mod use_super {
163+
use super::*;
164+
165+
fn with_super() {
166+
let _ = foofoo();
167+
}
168+
}
169+
170+
mod use_explicit {
171+
use test_super_imports::foofoo;
172+
173+
fn with_explicit() {
174+
let _ = foofoo();
175+
}
176+
}
177+
178+
mod use_double_super {
179+
mod inner {
180+
use super::super::foofoo;
181+
182+
fn with_double_super() {
183+
let _ = foofoo();
184+
}
185+
}
186+
}
187+
188+
mod use_super_explicit {
189+
use super::super::test_super_imports::foofoo;
190+
191+
fn with_super_explicit() {
192+
let _ = foofoo();
193+
}
194+
}
195+
}

tests/ui/wildcard_imports.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,41 @@ fn test_weird_formatting() {
156156
exported();
157157
foo();
158158
}
159+
160+
mod test_super_imports {
161+
fn foofoo() {}
162+
163+
mod use_super {
164+
use super::*;
165+
166+
fn with_super() {
167+
let _ = foofoo();
168+
}
169+
}
170+
171+
mod use_explicit {
172+
use test_super_imports::*;
173+
174+
fn with_explicit() {
175+
let _ = foofoo();
176+
}
177+
}
178+
179+
mod use_double_super {
180+
mod inner {
181+
use super::super::*;
182+
183+
fn with_double_super() {
184+
let _ = foofoo();
185+
}
186+
}
187+
}
188+
189+
mod use_super_explicit {
190+
use super::super::test_super_imports::*;
191+
192+
fn with_super_explicit() {
193+
let _ = foofoo();
194+
}
195+
}
196+
}

tests/ui/wildcard_imports.stderr

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,23 @@ LL | use crate:: fn_mod::
9292
LL | | *;
9393
| |_________^ help: try: `crate:: fn_mod::foo`
9494

95-
error: aborting due to 15 previous errors
95+
error: usage of wildcard import
96+
--> $DIR/wildcard_imports.rs:172:13
97+
|
98+
LL | use test_super_imports::*;
99+
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `test_super_imports::foofoo`
100+
101+
error: usage of wildcard import
102+
--> $DIR/wildcard_imports.rs:181:17
103+
|
104+
LL | use super::super::*;
105+
| ^^^^^^^^^^^^^^^ help: try: `super::super::foofoo`
106+
107+
error: usage of wildcard import
108+
--> $DIR/wildcard_imports.rs:190:13
109+
|
110+
LL | use super::super::test_super_imports::*;
111+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `super::super::test_super_imports::foofoo`
112+
113+
error: aborting due to 18 previous errors
96114

0 commit comments

Comments
 (0)