File tree 9 files changed +92
-14
lines changed
wildcard_imports_whitelist
9 files changed +92
-14
lines changed Original file line number Diff line number Diff line change @@ -573,9 +573,20 @@ define_Conf! {
573
573
( allow_comparison_to_zero: bool = true ) ,
574
574
/// Lint: WILDCARD_IMPORTS.
575
575
///
576
- /// List of path segments to ignore when checking wildcard imports,
577
- /// could get overrided by `warn_on_all_wildcard_imports`.
578
- ( ignored_wildcard_imports: Vec <String > = Vec :: new( ) ) ,
576
+ /// List of path segments to ignore when checking wildcard imports.
577
+ ///
578
+ /// #### Example
579
+ ///
580
+ /// ```toml
581
+ /// ignored-wildcard-imports = [ "utils", "common" ]
582
+ /// ```
583
+ ///
584
+ /// #### Noteworthy
585
+ ///
586
+ /// 1. This configuration has no effects if used with `warn_on_all_wildcard_imports = true`.
587
+ /// 2. Paths with any segment that containing the word 'prelude'
588
+ /// are already ignored by default.
589
+ ( ignored_wildcard_imports: FxHashSet <String > = FxHashSet :: default ( ) ) ,
579
590
}
580
591
581
592
/// Search for the configuration file.
Original file line number Diff line number Diff line change @@ -101,11 +101,11 @@ declare_clippy_lint! {
101
101
pub struct WildcardImports {
102
102
warn_on_all : bool ,
103
103
test_modules_deep : u32 ,
104
- ignored_segments : Vec < String > ,
104
+ ignored_segments : FxHashSet < String > ,
105
105
}
106
106
107
107
impl WildcardImports {
108
- pub fn new ( warn_on_all : bool , ignored_wildcard_imports : Vec < String > ) -> Self {
108
+ pub fn new ( warn_on_all : bool , ignored_wildcard_imports : FxHashSet < String > ) -> Self {
109
109
Self {
110
110
warn_on_all,
111
111
test_modules_deep : 0 ,
@@ -212,10 +212,8 @@ fn is_super_only_import(segments: &[PathSegment<'_>]) -> bool {
212
212
213
213
// Allow skipping imports containing user configured segments,
214
214
// i.e. "...::utils::...::*" if user put `ignored-wildcard-imports = ["utils"]` in `Clippy.toml`
215
- fn is_ignored_via_config ( segments : & [ PathSegment < ' _ > ] , ignored_segments : & [ String ] ) -> bool {
216
- let segments_set: FxHashSet < & str > = segments. iter ( ) . map ( |s| s. ident . as_str ( ) ) . collect ( ) ;
217
- let ignored_set: FxHashSet < & str > = ignored_segments. iter ( ) . map ( String :: as_str) . collect ( ) ;
215
+ fn is_ignored_via_config ( segments : & [ PathSegment < ' _ > ] , ignored_segments : & FxHashSet < String > ) -> bool {
218
216
// segment matching need to be exact instead of using 'contains', in case user unintentionaly put
219
217
// a single character in the config thus skipping most of the warnings.
220
- segments_set . intersection ( & ignored_set ) . next ( ) . is_some ( )
218
+ segments . iter ( ) . any ( |seg| ignored_segments . contains ( seg . ident . as_str ( ) ) )
221
219
}
Original file line number Diff line number Diff line change 1
1
warn-on-all-wildcard-imports = true
2
+
3
+ # This should be ignored since `warn-on-all-wildcard-imports` has higher precedence
4
+ ignored-wildcard-imports = [" utils" ]
Original file line number Diff line number Diff line change 3
3
mod prelude {
4
4
pub const FOO: u8 = 1;
5
5
}
6
+
7
+ mod utils {
8
+ pub const BAR: u8 = 1;
9
+ pub fn print() {}
10
+ }
11
+
12
+ mod my_crate {
13
+ pub mod utils {
14
+ pub fn my_util_fn() {}
15
+ }
16
+ }
17
+
18
+ use utils::{BAR, print};
19
+ //~^ ERROR: usage of wildcard import
20
+ use my_crate::utils::my_util_fn;
21
+ //~^ ERROR: usage of wildcard import
6
22
use prelude::FOO;
7
23
//~^ ERROR: usage of wildcard import
8
24
9
25
fn main() {
10
26
let _ = FOO;
27
+ let _ = BAR;
28
+ print();
29
+ my_util_fn();
11
30
}
Original file line number Diff line number Diff line change 3
3
mod prelude {
4
4
pub const FOO : u8 = 1 ;
5
5
}
6
+
7
+ mod utils {
8
+ pub const BAR : u8 = 1 ;
9
+ pub fn print ( ) { }
10
+ }
11
+
12
+ mod my_crate {
13
+ pub mod utils {
14
+ pub fn my_util_fn ( ) { }
15
+ }
16
+ }
17
+
18
+ use utils:: * ;
19
+ //~^ ERROR: usage of wildcard import
20
+ use my_crate:: utils:: * ;
21
+ //~^ ERROR: usage of wildcard import
6
22
use prelude:: * ;
7
23
//~^ ERROR: usage of wildcard import
8
24
9
25
fn main ( ) {
10
26
let _ = FOO ;
27
+ let _ = BAR ;
28
+ print ( ) ;
29
+ my_util_fn ( ) ;
11
30
}
Original file line number Diff line number Diff line change 1
1
error: usage of wildcard import
2
- --> $DIR/wildcard_imports.rs:6 :5
2
+ --> $DIR/wildcard_imports.rs:18 :5
3
3
|
4
- LL | use prelude ::*;
5
- | ^^^^^^^^^^ help: try: `prelude::FOO `
4
+ LL | use utils ::*;
5
+ | ^^^^^^^^ help: try: `utils::{BAR, print} `
6
6
|
7
7
= note: `-D clippy::wildcard-imports` implied by `-D warnings`
8
8
= help: to override `-D warnings` add `#[allow(clippy::wildcard_imports)]`
9
9
10
- error: aborting due to 1 previous error
10
+ error: usage of wildcard import
11
+ --> $DIR/wildcard_imports.rs:20:5
12
+ |
13
+ LL | use my_crate::utils::*;
14
+ | ^^^^^^^^^^^^^^^^^^ help: try: `my_crate::utils::my_util_fn`
15
+
16
+ error: usage of wildcard import
17
+ --> $DIR/wildcard_imports.rs:22:5
18
+ |
19
+ LL | use prelude::*;
20
+ | ^^^^^^^^^^ help: try: `prelude::FOO`
21
+
22
+ error: aborting due to 3 previous errors
11
23
Original file line number Diff line number Diff line change @@ -8,11 +8,19 @@ mod utils_plus {
8
8
pub fn do_something() {}
9
9
}
10
10
11
+ mod my_crate {
12
+ pub mod utils {
13
+ pub fn my_util_fn() {}
14
+ }
15
+ }
16
+
17
+ use my_crate::utils::*;
11
18
use utils::*;
12
19
use utils_plus::do_something;
13
20
//~^ ERROR: usage of wildcard import
14
21
15
22
fn main() {
16
23
print();
24
+ my_util_fn();
17
25
do_something();
18
26
}
Original file line number Diff line number Diff line change @@ -8,11 +8,19 @@ mod utils_plus {
8
8
pub fn do_something ( ) { }
9
9
}
10
10
11
+ mod my_crate {
12
+ pub mod utils {
13
+ pub fn my_util_fn ( ) { }
14
+ }
15
+ }
16
+
17
+ use my_crate:: utils:: * ;
11
18
use utils:: * ;
12
19
use utils_plus:: * ;
13
20
//~^ ERROR: usage of wildcard import
14
21
15
22
fn main ( ) {
16
23
print ( ) ;
24
+ my_util_fn ( ) ;
17
25
do_something ( ) ;
18
26
}
Original file line number Diff line number Diff line change 1
1
error: usage of wildcard import
2
- --> $DIR/wildcard_imports.rs:12 :5
2
+ --> $DIR/wildcard_imports.rs:19 :5
3
3
|
4
4
LL | use utils_plus::*;
5
5
| ^^^^^^^^^^^^^ help: try: `utils_plus::do_something`
You can’t perform that action at this time.
0 commit comments