Skip to content

Commit 2883124

Browse files
committed
[wildcard_imports] Modules that contain prelude are also allowed
This commit fixes #10846 by checking if the path segment contains the word "prelude". Signed-off-by: Charalampos Mitrodimas <[email protected]>
1 parent 423f081 commit 2883124

File tree

5 files changed

+31
-18
lines changed

5 files changed

+31
-18
lines changed

clippy_lints/src/wildcard_imports.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ declare_clippy_lint! {
6565
/// This can lead to confusing error messages at best and to unexpected behavior at worst.
6666
///
6767
/// ### Exceptions
68-
/// Wildcard imports are allowed from modules named `prelude`. Many crates (including the standard library)
69-
/// provide modules named "prelude" specifically designed for wildcard import.
68+
/// Wildcard imports are allowed from modules that their name contains `prelude`. Many crates
69+
/// (including the standard library) provide modules named "prelude" specifically designed
70+
/// for wildcard import.
7071
///
7172
/// `use super::*` is allowed in test modules. This is defined as any module with "test" in the name.
7273
///
@@ -212,7 +213,9 @@ impl WildcardImports {
212213
// Allow "...prelude::..::*" imports.
213214
// Many crates have a prelude, and it is imported as a glob by design.
214215
fn is_prelude_import(segments: &[PathSegment<'_>]) -> bool {
215-
segments.iter().any(|ps| ps.ident.name == sym::prelude)
216+
segments
217+
.iter()
218+
.any(|ps| ps.ident.name.as_str().contains(sym::prelude.as_str()))
216219
}
217220

218221
// Allow "super::*" imports in tests.

tests/ui/auxiliary/wildcard_imports_helper.rs

+6
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@ pub mod prelude {
2525
pub struct PreludeModAnywhere;
2626
}
2727
}
28+
29+
pub mod extern_prelude {
30+
pub mod v1 {
31+
pub struct ExternPreludeModAnywhere;
32+
}
33+
}

tests/ui/wildcard_imports.fixed

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use wildcard_imports_helper::inner::inner_for_self_import::inner_extern_bar;
2424
use wildcard_imports_helper::{ExternA, extern_foo};
2525

2626
use std::io::prelude::*;
27+
use wildcard_imports_helper::extern_prelude::v1::*;
2728
use wildcard_imports_helper::prelude::v1::*;
2829

2930
struct ReadFoo;
@@ -81,6 +82,7 @@ fn main() {
8182
let _ = inner_struct_mod::C;
8283
let _ = ExternA;
8384
let _ = PreludeModAnywhere;
85+
let _ = ExternPreludeModAnywhere;
8486

8587
double_struct_import_test!();
8688
double_struct_import_test!();

tests/ui/wildcard_imports.rs

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use wildcard_imports_helper::inner::inner_for_self_import::*;
2424
use wildcard_imports_helper::*;
2525

2626
use std::io::prelude::*;
27+
use wildcard_imports_helper::extern_prelude::v1::*;
2728
use wildcard_imports_helper::prelude::v1::*;
2829

2930
struct ReadFoo;
@@ -81,6 +82,7 @@ fn main() {
8182
let _ = inner_struct_mod::C;
8283
let _ = ExternA;
8384
let _ = PreludeModAnywhere;
85+
let _ = ExternPreludeModAnywhere;
8486

8587
double_struct_import_test!();
8688
double_struct_import_test!();

tests/ui/wildcard_imports.stderr

+15-15
Original file line numberDiff line numberDiff line change
@@ -37,93 +37,93 @@ LL | use wildcard_imports_helper::*;
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{ExternA, extern_foo}`
3838

3939
error: usage of wildcard import
40-
--> $DIR/wildcard_imports.rs:95:13
40+
--> $DIR/wildcard_imports.rs:97:13
4141
|
4242
LL | use crate::fn_mod::*;
4343
| ^^^^^^^^^^^^^^^^ help: try: `crate::fn_mod::foo`
4444

4545
error: usage of wildcard import
46-
--> $DIR/wildcard_imports.rs:101:75
46+
--> $DIR/wildcard_imports.rs:103:75
4747
|
4848
LL | use wildcard_imports_helper::inner::inner_for_self_import::{self, *};
4949
| ^ help: try: `inner_extern_foo`
5050

5151
error: usage of wildcard import
52-
--> $DIR/wildcard_imports.rs:102:13
52+
--> $DIR/wildcard_imports.rs:104:13
5353
|
5454
LL | use wildcard_imports_helper::*;
5555
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{ExternA, extern_foo}`
5656

5757
error: usage of wildcard import
58-
--> $DIR/wildcard_imports.rs:113:20
58+
--> $DIR/wildcard_imports.rs:115:20
5959
|
6060
LL | use self::{inner::*, inner2::*};
6161
| ^^^^^^^^ help: try: `inner::inner_foo`
6262

6363
error: usage of wildcard import
64-
--> $DIR/wildcard_imports.rs:113:30
64+
--> $DIR/wildcard_imports.rs:115:30
6565
|
6666
LL | use self::{inner::*, inner2::*};
6767
| ^^^^^^^^^ help: try: `inner2::inner_bar`
6868

6969
error: usage of wildcard import
70-
--> $DIR/wildcard_imports.rs:120:13
70+
--> $DIR/wildcard_imports.rs:122:13
7171
|
7272
LL | use wildcard_imports_helper::*;
7373
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{ExternExportedEnum, ExternExportedStruct, extern_exported}`
7474

7575
error: usage of wildcard import
76-
--> $DIR/wildcard_imports.rs:149:9
76+
--> $DIR/wildcard_imports.rs:151:9
7777
|
7878
LL | use crate::in_fn_test::*;
7979
| ^^^^^^^^^^^^^^^^^^^^ help: try: `crate::in_fn_test::{ExportedEnum, ExportedStruct, exported}`
8080

8181
error: usage of wildcard import
82-
--> $DIR/wildcard_imports.rs:158:9
82+
--> $DIR/wildcard_imports.rs:160:9
8383
|
8484
LL | use crate:: in_fn_test:: * ;
8585
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `crate:: in_fn_test::exported`
8686

8787
error: usage of wildcard import
88-
--> $DIR/wildcard_imports.rs:159:9
88+
--> $DIR/wildcard_imports.rs:161:9
8989
|
9090
LL | use crate:: fn_mod::
9191
| _________^
9292
LL | | *;
9393
| |_________^ help: try: `crate:: fn_mod::foo`
9494

9595
error: usage of wildcard import
96-
--> $DIR/wildcard_imports.rs:170:13
96+
--> $DIR/wildcard_imports.rs:172:13
9797
|
9898
LL | use super::*;
9999
| ^^^^^^^^ help: try: `super::foofoo`
100100

101101
error: usage of wildcard import
102-
--> $DIR/wildcard_imports.rs:205:17
102+
--> $DIR/wildcard_imports.rs:207:17
103103
|
104104
LL | use super::*;
105105
| ^^^^^^^^ help: try: `super::insidefoo`
106106

107107
error: usage of wildcard import
108-
--> $DIR/wildcard_imports.rs:213:13
108+
--> $DIR/wildcard_imports.rs:215:13
109109
|
110110
LL | use super_imports::*;
111111
| ^^^^^^^^^^^^^^^^ help: try: `super_imports::foofoo`
112112

113113
error: usage of wildcard import
114-
--> $DIR/wildcard_imports.rs:222:17
114+
--> $DIR/wildcard_imports.rs:224:17
115115
|
116116
LL | use super::super::*;
117117
| ^^^^^^^^^^^^^^^ help: try: `super::super::foofoo`
118118

119119
error: usage of wildcard import
120-
--> $DIR/wildcard_imports.rs:231:13
120+
--> $DIR/wildcard_imports.rs:233:13
121121
|
122122
LL | use super::super::super_imports::*;
123123
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `super::super::super_imports::foofoo`
124124

125125
error: usage of wildcard import
126-
--> $DIR/wildcard_imports.rs:239:13
126+
--> $DIR/wildcard_imports.rs:241:13
127127
|
128128
LL | use super::*;
129129
| ^^^^^^^^ help: try: `super::foofoo`

0 commit comments

Comments
 (0)