Skip to content

Commit f21d10b

Browse files
committed
Auto merge of #5929 - stchris:issues/5917, r=ebroto
Widen understanding of prelude import Prelude imports are exempt from wildcard import warnings. Until now only imports of the form ``` use ...::prelude::*; ``` were considered. This change makes it so that the segment `prelude` can show up anywhere, for instance: ``` use ...::prelude::v1::*; ``` Fixes #5917 changelog: Allow `prelude` to appear in any segment of the import path in [`wildcard_imports`]
2 parents a8520b0 + e615a26 commit f21d10b

File tree

5 files changed

+26
-19
lines changed

5 files changed

+26
-19
lines changed

clippy_lints/src/wildcard_imports.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,10 @@ impl WildcardImports {
195195
}
196196
}
197197

198-
// Allow "...prelude::*" imports.
198+
// Allow "...prelude::..::*" imports.
199199
// Many crates have a prelude, and it is imported as a glob by design.
200200
fn is_prelude_import(segments: &[PathSegment<'_>]) -> bool {
201-
segments
202-
.iter()
203-
.last()
204-
.map_or(false, |ps| ps.ident.as_str() == "prelude")
201+
segments.iter().any(|ps| ps.ident.as_str() == "prelude")
205202
}
206203

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

tests/ui/auxiliary/wildcard_imports_helper.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ mod extern_exports {
1919
A,
2020
}
2121
}
22+
23+
pub mod prelude {
24+
pub mod v1 {
25+
pub struct PreludeModAnywhere;
26+
}
27+
}

tests/ui/wildcard_imports.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use wildcard_imports_helper::inner::inner_for_self_import::inner_extern_bar;
2020
use wildcard_imports_helper::{ExternA, extern_foo};
2121

2222
use std::io::prelude::*;
23+
use wildcard_imports_helper::prelude::v1::*;
2324

2425
struct ReadFoo;
2526

@@ -75,6 +76,7 @@ fn main() {
7576
let _ = A;
7677
let _ = inner_struct_mod::C;
7778
let _ = ExternA;
79+
let _ = PreludeModAnywhere;
7880

7981
double_struct_import_test!();
8082
double_struct_import_test!();

tests/ui/wildcard_imports.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use wildcard_imports_helper::inner::inner_for_self_import::*;
2020
use wildcard_imports_helper::*;
2121

2222
use std::io::prelude::*;
23+
use wildcard_imports_helper::prelude::v1::*;
2324

2425
struct ReadFoo;
2526

@@ -75,6 +76,7 @@ fn main() {
7576
let _ = A;
7677
let _ = inner_struct_mod::C;
7778
let _ = ExternA;
79+
let _ = PreludeModAnywhere;
7880

7981
double_struct_import_test!();
8082
double_struct_import_test!();

tests/ui/wildcard_imports.stderr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,87 +37,87 @@ 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:89:13
40+
--> $DIR/wildcard_imports.rs:91: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:95:75
46+
--> $DIR/wildcard_imports.rs:97: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:96:13
52+
--> $DIR/wildcard_imports.rs:98: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:107:20
58+
--> $DIR/wildcard_imports.rs:109: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:107:30
64+
--> $DIR/wildcard_imports.rs:109: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:114:13
70+
--> $DIR/wildcard_imports.rs:116: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:143:9
76+
--> $DIR/wildcard_imports.rs:145: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:152:9
82+
--> $DIR/wildcard_imports.rs:154: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:153:9
88+
--> $DIR/wildcard_imports.rs:155: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:164:13
96+
--> $DIR/wildcard_imports.rs:166:13
9797
|
9898
LL | use super::*;
9999
| ^^^^^^^^ help: try: `super::foofoo`
100100

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

107107
error: usage of wildcard import
108-
--> $DIR/wildcard_imports.rs:207:13
108+
--> $DIR/wildcard_imports.rs:209: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:216:17
114+
--> $DIR/wildcard_imports.rs:218: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:225:13
120+
--> $DIR/wildcard_imports.rs:227:13
121121
|
122122
LL | use super::super::super_imports::*;
123123
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `super::super::super_imports::foofoo`

0 commit comments

Comments
 (0)