Skip to content

Commit 314bdde

Browse files
committedJan 30, 2024
add more test cases & improve docs & replace Vec with FxHashSet for segments
1 parent d02df12 commit 314bdde

File tree

9 files changed

+92
-14
lines changed

9 files changed

+92
-14
lines changed
 

Diff for: ‎clippy_config/src/conf.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,20 @@ define_Conf! {
573573
(allow_comparison_to_zero: bool = true),
574574
/// Lint: WILDCARD_IMPORTS.
575575
///
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()),
579590
}
580591

581592
/// Search for the configuration file.

Diff for: ‎clippy_lints/src/wildcard_imports.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ declare_clippy_lint! {
101101
pub struct WildcardImports {
102102
warn_on_all: bool,
103103
test_modules_deep: u32,
104-
ignored_segments: Vec<String>,
104+
ignored_segments: FxHashSet<String>,
105105
}
106106

107107
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 {
109109
Self {
110110
warn_on_all,
111111
test_modules_deep: 0,
@@ -212,10 +212,8 @@ fn is_super_only_import(segments: &[PathSegment<'_>]) -> bool {
212212

213213
// Allow skipping imports containing user configured segments,
214214
// 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 {
218216
// segment matching need to be exact instead of using 'contains', in case user unintentionaly put
219217
// 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()))
221219
}

Diff for: ‎tests/ui-toml/wildcard_imports/clippy.toml

+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
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"]

Diff for: ‎tests/ui-toml/wildcard_imports/wildcard_imports.fixed

+19
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,28 @@
33
mod prelude {
44
pub const FOO: u8 = 1;
55
}
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
622
use prelude::FOO;
723
//~^ ERROR: usage of wildcard import
824

925
fn main() {
1026
let _ = FOO;
27+
let _ = BAR;
28+
print();
29+
my_util_fn();
1130
}

Diff for: ‎tests/ui-toml/wildcard_imports/wildcard_imports.rs

+19
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,28 @@
33
mod prelude {
44
pub const FOO: u8 = 1;
55
}
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
622
use prelude::*;
723
//~^ ERROR: usage of wildcard import
824

925
fn main() {
1026
let _ = FOO;
27+
let _ = BAR;
28+
print();
29+
my_util_fn();
1130
}
+16-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
error: usage of wildcard import
2-
--> $DIR/wildcard_imports.rs:6:5
2+
--> $DIR/wildcard_imports.rs:18:5
33
|
4-
LL | use prelude::*;
5-
| ^^^^^^^^^^ help: try: `prelude::FOO`
4+
LL | use utils::*;
5+
| ^^^^^^^^ help: try: `utils::{BAR, print}`
66
|
77
= note: `-D clippy::wildcard-imports` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::wildcard_imports)]`
99

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
1123

Diff for: ‎tests/ui-toml/wildcard_imports_whitelist/wildcard_imports.fixed

+8
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@ mod utils_plus {
88
pub fn do_something() {}
99
}
1010

11+
mod my_crate {
12+
pub mod utils {
13+
pub fn my_util_fn() {}
14+
}
15+
}
16+
17+
use my_crate::utils::*;
1118
use utils::*;
1219
use utils_plus::do_something;
1320
//~^ ERROR: usage of wildcard import
1421

1522
fn main() {
1623
print();
24+
my_util_fn();
1725
do_something();
1826
}

Diff for: ‎tests/ui-toml/wildcard_imports_whitelist/wildcard_imports.rs

+8
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@ mod utils_plus {
88
pub fn do_something() {}
99
}
1010

11+
mod my_crate {
12+
pub mod utils {
13+
pub fn my_util_fn() {}
14+
}
15+
}
16+
17+
use my_crate::utils::*;
1118
use utils::*;
1219
use utils_plus::*;
1320
//~^ ERROR: usage of wildcard import
1421

1522
fn main() {
1623
print();
24+
my_util_fn();
1725
do_something();
1826
}

Diff for: ‎tests/ui-toml/wildcard_imports_whitelist/wildcard_imports.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: usage of wildcard import
2-
--> $DIR/wildcard_imports.rs:12:5
2+
--> $DIR/wildcard_imports.rs:19:5
33
|
44
LL | use utils_plus::*;
55
| ^^^^^^^^^^^^^ help: try: `utils_plus::do_something`

0 commit comments

Comments
 (0)