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`]
This commit is contained in:
bors 2020-08-22 12:50:22 +00:00
commit f21d10b970
5 changed files with 26 additions and 19 deletions

View File

@ -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.

View File

@ -19,3 +19,9 @@ mod extern_exports {
A,
}
}
pub mod prelude {
pub mod v1 {
pub struct PreludeModAnywhere;
}
}

View File

@ -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;
@ -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!();

View File

@ -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;
@ -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!();

View File

@ -37,55 +37,55 @@ 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::
| _________^
@ -93,31 +93,31 @@ 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`