about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJ-ZhengLi <lizheng135@huawei.com>2024-01-29 11:29:15 +0800
committerJ-ZhengLi <lizheng135@huawei.com>2024-01-30 09:38:14 +0800
commit314bddee95f88a405ec4b44db5576e3745ee1699 (patch)
treeeb2f4cb8337b36e0f7a9a0c2ab5d80e1e9e9339f
parentd02df12bd5e9ad2016328786d6a117afe0f1604a (diff)
downloadrust-314bddee95f88a405ec4b44db5576e3745ee1699.tar.gz
rust-314bddee95f88a405ec4b44db5576e3745ee1699.zip
add more test cases & improve docs & replace `Vec` with `FxHashSet` for segments
-rw-r--r--clippy_config/src/conf.rs17
-rw-r--r--clippy_lints/src/wildcard_imports.rs10
-rw-r--r--tests/ui-toml/wildcard_imports/clippy.toml3
-rw-r--r--tests/ui-toml/wildcard_imports/wildcard_imports.fixed19
-rw-r--r--tests/ui-toml/wildcard_imports/wildcard_imports.rs19
-rw-r--r--tests/ui-toml/wildcard_imports/wildcard_imports.stderr20
-rw-r--r--tests/ui-toml/wildcard_imports_whitelist/wildcard_imports.fixed8
-rw-r--r--tests/ui-toml/wildcard_imports_whitelist/wildcard_imports.rs8
-rw-r--r--tests/ui-toml/wildcard_imports_whitelist/wildcard_imports.stderr2
9 files changed, 92 insertions, 14 deletions
diff --git a/clippy_config/src/conf.rs b/clippy_config/src/conf.rs
index e4e512482ac..d03f805e2fc 100644
--- a/clippy_config/src/conf.rs
+++ b/clippy_config/src/conf.rs
@@ -573,9 +573,20 @@ define_Conf! {
     (allow_comparison_to_zero: bool = true),
     /// Lint: WILDCARD_IMPORTS.
     ///
-    /// List of path segments to ignore when checking wildcard imports,
-    /// could get overrided by `warn_on_all_wildcard_imports`.
-    (ignored_wildcard_imports: Vec<String> = Vec::new()),
+    /// List of path segments to ignore when checking wildcard imports.
+    ///
+    /// #### Example
+    ///
+    /// ```toml
+    /// ignored-wildcard-imports = [ "utils", "common" ]
+    /// ```
+    ///
+    /// #### Noteworthy
+    ///
+    /// 1. This configuration has no effects if used with `warn_on_all_wildcard_imports = true`.
+    /// 2. Paths with any segment that containing the word 'prelude'
+    /// are already ignored by default.
+    (ignored_wildcard_imports: FxHashSet<String> = FxHashSet::default()),
 }
 
 /// Search for the configuration file.
diff --git a/clippy_lints/src/wildcard_imports.rs b/clippy_lints/src/wildcard_imports.rs
index 4eb45e6f8da..7318ffc9587 100644
--- a/clippy_lints/src/wildcard_imports.rs
+++ b/clippy_lints/src/wildcard_imports.rs
@@ -101,11 +101,11 @@ declare_clippy_lint! {
 pub struct WildcardImports {
     warn_on_all: bool,
     test_modules_deep: u32,
-    ignored_segments: Vec<String>,
+    ignored_segments: FxHashSet<String>,
 }
 
 impl WildcardImports {
-    pub fn new(warn_on_all: bool, ignored_wildcard_imports: Vec<String>) -> Self {
+    pub fn new(warn_on_all: bool, ignored_wildcard_imports: FxHashSet<String>) -> Self {
         Self {
             warn_on_all,
             test_modules_deep: 0,
@@ -212,10 +212,8 @@ fn is_super_only_import(segments: &[PathSegment<'_>]) -> bool {
 
 // Allow skipping imports containing user configured segments,
 // i.e. "...::utils::...::*" if user put `ignored-wildcard-imports = ["utils"]` in `Clippy.toml`
-fn is_ignored_via_config(segments: &[PathSegment<'_>], ignored_segments: &[String]) -> bool {
-    let segments_set: FxHashSet<&str> = segments.iter().map(|s| s.ident.as_str()).collect();
-    let ignored_set: FxHashSet<&str> = ignored_segments.iter().map(String::as_str).collect();
+fn is_ignored_via_config(segments: &[PathSegment<'_>], ignored_segments: &FxHashSet<String>) -> bool {
     // segment matching need to be exact instead of using 'contains', in case user unintentionaly put
     // a single character in the config thus skipping most of the warnings.
-    segments_set.intersection(&ignored_set).next().is_some()
+    segments.iter().any(|seg| ignored_segments.contains(seg.ident.as_str()))
 }
diff --git a/tests/ui-toml/wildcard_imports/clippy.toml b/tests/ui-toml/wildcard_imports/clippy.toml
index 875aaeef6c9..56a945e48bb 100644
--- a/tests/ui-toml/wildcard_imports/clippy.toml
+++ b/tests/ui-toml/wildcard_imports/clippy.toml
@@ -1 +1,4 @@
 warn-on-all-wildcard-imports = true
+
+# This should be ignored since `warn-on-all-wildcard-imports` has higher precedence
+ignored-wildcard-imports = ["utils"]
diff --git a/tests/ui-toml/wildcard_imports/wildcard_imports.fixed b/tests/ui-toml/wildcard_imports/wildcard_imports.fixed
index 1752f48856c..af72d6be0e0 100644
--- a/tests/ui-toml/wildcard_imports/wildcard_imports.fixed
+++ b/tests/ui-toml/wildcard_imports/wildcard_imports.fixed
@@ -3,9 +3,28 @@
 mod prelude {
     pub const FOO: u8 = 1;
 }
+
+mod utils {
+    pub const BAR: u8 = 1;
+    pub fn print() {}
+}
+
+mod my_crate {
+    pub mod utils {
+        pub fn my_util_fn() {}
+    }
+}
+
+use utils::{BAR, print};
+//~^ ERROR: usage of wildcard import
+use my_crate::utils::my_util_fn;
+//~^ ERROR: usage of wildcard import
 use prelude::FOO;
 //~^ ERROR: usage of wildcard import
 
 fn main() {
     let _ = FOO;
+    let _ = BAR;
+    print();
+    my_util_fn();
 }
diff --git a/tests/ui-toml/wildcard_imports/wildcard_imports.rs b/tests/ui-toml/wildcard_imports/wildcard_imports.rs
index 331c2c59c22..91009dd8835 100644
--- a/tests/ui-toml/wildcard_imports/wildcard_imports.rs
+++ b/tests/ui-toml/wildcard_imports/wildcard_imports.rs
@@ -3,9 +3,28 @@
 mod prelude {
     pub const FOO: u8 = 1;
 }
+
+mod utils {
+    pub const BAR: u8 = 1;
+    pub fn print() {}
+}
+
+mod my_crate {
+    pub mod utils {
+        pub fn my_util_fn() {}
+    }
+}
+
+use utils::*;
+//~^ ERROR: usage of wildcard import
+use my_crate::utils::*;
+//~^ ERROR: usage of wildcard import
 use prelude::*;
 //~^ ERROR: usage of wildcard import
 
 fn main() {
     let _ = FOO;
+    let _ = BAR;
+    print();
+    my_util_fn();
 }
diff --git a/tests/ui-toml/wildcard_imports/wildcard_imports.stderr b/tests/ui-toml/wildcard_imports/wildcard_imports.stderr
index f11fda6a0c6..a733d786d0e 100644
--- a/tests/ui-toml/wildcard_imports/wildcard_imports.stderr
+++ b/tests/ui-toml/wildcard_imports/wildcard_imports.stderr
@@ -1,11 +1,23 @@
 error: usage of wildcard import
-  --> $DIR/wildcard_imports.rs:6:5
+  --> $DIR/wildcard_imports.rs:18:5
    |
-LL | use prelude::*;
-   |     ^^^^^^^^^^ help: try: `prelude::FOO`
+LL | use utils::*;
+   |     ^^^^^^^^ help: try: `utils::{BAR, print}`
    |
    = note: `-D clippy::wildcard-imports` implied by `-D warnings`
    = help: to override `-D warnings` add `#[allow(clippy::wildcard_imports)]`
 
-error: aborting due to 1 previous error
+error: usage of wildcard import
+  --> $DIR/wildcard_imports.rs:20:5
+   |
+LL | use my_crate::utils::*;
+   |     ^^^^^^^^^^^^^^^^^^ help: try: `my_crate::utils::my_util_fn`
+
+error: usage of wildcard import
+  --> $DIR/wildcard_imports.rs:22:5
+   |
+LL | use prelude::*;
+   |     ^^^^^^^^^^ help: try: `prelude::FOO`
+
+error: aborting due to 3 previous errors
 
diff --git a/tests/ui-toml/wildcard_imports_whitelist/wildcard_imports.fixed b/tests/ui-toml/wildcard_imports_whitelist/wildcard_imports.fixed
index 5ef30b054b3..d539525c45e 100644
--- a/tests/ui-toml/wildcard_imports_whitelist/wildcard_imports.fixed
+++ b/tests/ui-toml/wildcard_imports_whitelist/wildcard_imports.fixed
@@ -8,11 +8,19 @@ mod utils_plus {
     pub fn do_something() {}
 }
 
+mod my_crate {
+    pub mod utils {
+        pub fn my_util_fn() {}
+    }
+}
+
+use my_crate::utils::*;
 use utils::*;
 use utils_plus::do_something;
 //~^ ERROR: usage of wildcard import
 
 fn main() {
     print();
+    my_util_fn();
     do_something();
 }
diff --git a/tests/ui-toml/wildcard_imports_whitelist/wildcard_imports.rs b/tests/ui-toml/wildcard_imports_whitelist/wildcard_imports.rs
index 5ea91a8d70a..9169d16a08b 100644
--- a/tests/ui-toml/wildcard_imports_whitelist/wildcard_imports.rs
+++ b/tests/ui-toml/wildcard_imports_whitelist/wildcard_imports.rs
@@ -8,11 +8,19 @@ mod utils_plus {
     pub fn do_something() {}
 }
 
+mod my_crate {
+    pub mod utils {
+        pub fn my_util_fn() {}
+    }
+}
+
+use my_crate::utils::*;
 use utils::*;
 use utils_plus::*;
 //~^ ERROR: usage of wildcard import
 
 fn main() {
     print();
+    my_util_fn();
     do_something();
 }
diff --git a/tests/ui-toml/wildcard_imports_whitelist/wildcard_imports.stderr b/tests/ui-toml/wildcard_imports_whitelist/wildcard_imports.stderr
index 15f522cb5af..12c2f9cbada 100644
--- a/tests/ui-toml/wildcard_imports_whitelist/wildcard_imports.stderr
+++ b/tests/ui-toml/wildcard_imports_whitelist/wildcard_imports.stderr
@@ -1,5 +1,5 @@
 error: usage of wildcard import
-  --> $DIR/wildcard_imports.rs:12:5
+  --> $DIR/wildcard_imports.rs:19:5
    |
 LL | use utils_plus::*;
    |     ^^^^^^^^^^^^^ help: try: `utils_plus::do_something`