Skip to content

Widen understanding of prelude import #5929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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