add more test cases & improve docs & replace Vec
with FxHashSet
for segments
This commit is contained in:
parent
d02df12bd5
commit
314bddee95
@ -573,9 +573,20 @@ pub fn get_configuration_metadata() -> Vec<ClippyConfiguration> {
|
||||
(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.
|
||||
|
@ -101,11 +101,11 @@
|
||||
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()))
|
||||
}
|
||||
|
@ -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"]
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
@ -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,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();
|
||||
}
|
||||
|
@ -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`
|
||||
|
Loading…
Reference in New Issue
Block a user