Skip to content

Commit

Permalink
Auto merge of #5929 - stchris:issues/5917, r=ebroto
Browse files Browse the repository at this point in the history
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`]
  • Loading branch information
bors committed Aug 22, 2020
2 parents a8520b0 + 422c07c commit 57892b2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 19 deletions.
7 changes: 2 additions & 5 deletions clippy_lints/src/wildcard_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,10 @@ impl WildcardImports {
}
}

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

// Allow "super::*" imports in tests.
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/auxiliary/wildcard_imports_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ mod extern_exports {
A,
}
}

pub mod prelude {
pub mod v1 {
pub struct PreludeModAnywhere;
}
}
2 changes: 2 additions & 0 deletions tests/ui/wildcard_imports.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use wildcard_imports_helper::inner::inner_for_self_import::inner_extern_bar;
use wildcard_imports_helper::{ExternA, extern_foo};

use std::io::prelude::*;
use wildcard_imports_helper::prelude::v1::*;

struct ReadFoo;

Expand Down Expand Up @@ -75,6 +76,7 @@ fn main() {
let _ = A;
let _ = inner_struct_mod::C;
let _ = ExternA;
let _ = PreludeModAnywhere;

double_struct_import_test!();
double_struct_import_test!();
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/wildcard_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use wildcard_imports_helper::inner::inner_for_self_import::*;
use wildcard_imports_helper::*;

use std::io::prelude::*;
use wildcard_imports_helper::prelude::v1::*;

struct ReadFoo;

Expand Down Expand Up @@ -75,6 +76,7 @@ fn main() {
let _ = A;
let _ = inner_struct_mod::C;
let _ = ExternA;
let _ = PreludeModAnywhere;

double_struct_import_test!();
double_struct_import_test!();
Expand Down
28 changes: 14 additions & 14 deletions tests/ui/wildcard_imports.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -37,87 +37,87 @@ LL | use wildcard_imports_helper::*;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{ExternA, extern_foo}`

error: usage of wildcard import
--> $DIR/wildcard_imports.rs:89:13
--> $DIR/wildcard_imports.rs:91:13
|
LL | use crate::fn_mod::*;
| ^^^^^^^^^^^^^^^^ help: try: `crate::fn_mod::foo`

error: usage of wildcard import
--> $DIR/wildcard_imports.rs:95:75
--> $DIR/wildcard_imports.rs:97:75
|
LL | use wildcard_imports_helper::inner::inner_for_self_import::{self, *};
| ^ help: try: `inner_extern_foo`

error: usage of wildcard import
--> $DIR/wildcard_imports.rs:96:13
--> $DIR/wildcard_imports.rs:98:13
|
LL | use wildcard_imports_helper::*;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{ExternA, extern_foo}`

error: usage of wildcard import
--> $DIR/wildcard_imports.rs:107:20
--> $DIR/wildcard_imports.rs:109:20
|
LL | use self::{inner::*, inner2::*};
| ^^^^^^^^ help: try: `inner::inner_foo`

error: usage of wildcard import
--> $DIR/wildcard_imports.rs:107:30
--> $DIR/wildcard_imports.rs:109:30
|
LL | use self::{inner::*, inner2::*};
| ^^^^^^^^^ help: try: `inner2::inner_bar`

error: usage of wildcard import
--> $DIR/wildcard_imports.rs:114:13
--> $DIR/wildcard_imports.rs:116:13
|
LL | use wildcard_imports_helper::*;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{ExternExportedEnum, ExternExportedStruct, extern_exported}`

error: usage of wildcard import
--> $DIR/wildcard_imports.rs:143:9
--> $DIR/wildcard_imports.rs:145:9
|
LL | use crate::in_fn_test::*;
| ^^^^^^^^^^^^^^^^^^^^ help: try: `crate::in_fn_test::{ExportedEnum, ExportedStruct, exported}`

error: usage of wildcard import
--> $DIR/wildcard_imports.rs:152:9
--> $DIR/wildcard_imports.rs:154:9
|
LL | use crate:: in_fn_test:: * ;
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `crate:: in_fn_test::exported`

error: usage of wildcard import
--> $DIR/wildcard_imports.rs:153:9
--> $DIR/wildcard_imports.rs:155:9
|
LL | use crate:: fn_mod::
| _________^
LL | | *;
| |_________^ help: try: `crate:: fn_mod::foo`

error: usage of wildcard import
--> $DIR/wildcard_imports.rs:164:13
--> $DIR/wildcard_imports.rs:166:13
|
LL | use super::*;
| ^^^^^^^^ help: try: `super::foofoo`

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

error: usage of wildcard import
--> $DIR/wildcard_imports.rs:207:13
--> $DIR/wildcard_imports.rs:209:13
|
LL | use super_imports::*;
| ^^^^^^^^^^^^^^^^ help: try: `super_imports::foofoo`

error: usage of wildcard import
--> $DIR/wildcard_imports.rs:216:17
--> $DIR/wildcard_imports.rs:218:17
|
LL | use super::super::*;
| ^^^^^^^^^^^^^^^ help: try: `super::super::foofoo`

error: usage of wildcard import
--> $DIR/wildcard_imports.rs:225:13
--> $DIR/wildcard_imports.rs:227:13
|
LL | use super::super::super_imports::*;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `super::super::super_imports::foofoo`
Expand Down

0 comments on commit 57892b2

Please sign in to comment.