Skip to content

Commit

Permalink
Widen understanding of prelude import
Browse files Browse the repository at this point in the history
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
  • Loading branch information
stchris committed Aug 22, 2020
1 parent a8520b0 commit 5b07b9e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 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().filter(|ps| ps.ident.as_str() == "prelude").count() > 0
}

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

0 comments on commit 5b07b9e

Please sign in to comment.