Skip to content

Commit

Permalink
add more test cases & improve docs & replace Vec with FxHashSet f…
Browse files Browse the repository at this point in the history
…or segments
  • Loading branch information
J-ZhengLi committed Jan 30, 2024
1 parent d02df12 commit 314bdde
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 14 deletions.
17 changes: 14 additions & 3 deletions clippy_config/src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 4 additions & 6 deletions clippy_lints/src/wildcard_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()))
}
3 changes: 3 additions & 0 deletions tests/ui-toml/wildcard_imports/clippy.toml
Original file line number Diff line number Diff line change
@@ -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"]
19 changes: 19 additions & 0 deletions tests/ui-toml/wildcard_imports/wildcard_imports.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
19 changes: 19 additions & 0 deletions tests/ui-toml/wildcard_imports/wildcard_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
20 changes: 16 additions & 4 deletions tests/ui-toml/wildcard_imports/wildcard_imports.stderr
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
8 changes: 8 additions & 0 deletions tests/ui-toml/wildcard_imports_whitelist/wildcard_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
@@ -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`
Expand Down

0 comments on commit 314bdde

Please sign in to comment.