Skip to content

Commit

Permalink
Auto merge of #10848 - charmitro:wildcard-prelude-detection, r=llogiq
Browse files Browse the repository at this point in the history
[`wildcard_imports`] Modules that contain `prelude` are also allowed

This commit fixes #10846 by checking if the path segment contains the word "prelude", allowing us
`use module_prelude::*`.

changelog: [`wildcard_imports`]: Modules that contain `prelude` are also allowed
  • Loading branch information
bors committed May 30, 2023
2 parents 423f081 + 2883124 commit 8793c2a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 18 deletions.
9 changes: 6 additions & 3 deletions clippy_lints/src/wildcard_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ declare_clippy_lint! {
/// This can lead to confusing error messages at best and to unexpected behavior at worst.
///
/// ### Exceptions
/// Wildcard imports are allowed from modules named `prelude`. Many crates (including the standard library)
/// provide modules named "prelude" specifically designed for wildcard import.
/// Wildcard imports are allowed from modules that their name contains `prelude`. Many crates
/// (including the standard library) provide modules named "prelude" specifically designed
/// for wildcard import.
///
/// `use super::*` is allowed in test modules. This is defined as any module with "test" in the name.
///
Expand Down Expand Up @@ -212,7 +213,9 @@ impl WildcardImports {
// 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().any(|ps| ps.ident.name == sym::prelude)
segments
.iter()
.any(|ps| ps.ident.name.as_str().contains(sym::prelude.as_str()))
}

// 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 @@ -25,3 +25,9 @@ pub mod prelude {
pub struct PreludeModAnywhere;
}
}

pub mod extern_prelude {
pub mod v1 {
pub struct ExternPreludeModAnywhere;
}
}
2 changes: 2 additions & 0 deletions tests/ui/wildcard_imports.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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::extern_prelude::v1::*;
use wildcard_imports_helper::prelude::v1::*;

struct ReadFoo;
Expand Down Expand Up @@ -81,6 +82,7 @@ fn main() {
let _ = inner_struct_mod::C;
let _ = ExternA;
let _ = PreludeModAnywhere;
let _ = ExternPreludeModAnywhere;

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 @@ -24,6 +24,7 @@ use wildcard_imports_helper::inner::inner_for_self_import::*;
use wildcard_imports_helper::*;

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

struct ReadFoo;
Expand Down Expand Up @@ -81,6 +82,7 @@ fn main() {
let _ = inner_struct_mod::C;
let _ = ExternA;
let _ = PreludeModAnywhere;
let _ = ExternPreludeModAnywhere;

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

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

error: usage of wildcard import
--> $DIR/wildcard_imports.rs:101:75
--> $DIR/wildcard_imports.rs:103: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:102:13
--> $DIR/wildcard_imports.rs:104:13
|
LL | use wildcard_imports_helper::*;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{ExternA, extern_foo}`

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

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

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

error: usage of wildcard import
--> $DIR/wildcard_imports.rs:149:9
--> $DIR/wildcard_imports.rs:151: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:158:9
--> $DIR/wildcard_imports.rs:160:9
|
LL | use crate:: in_fn_test:: * ;
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `crate:: in_fn_test::exported`

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

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

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

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

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

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

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

0 comments on commit 8793c2a

Please sign in to comment.