Skip to content

Commit

Permalink
add configuration for [wildcard_imports] to ignore certain imports
Browse files Browse the repository at this point in the history
  • Loading branch information
J-ZhengLi committed Jan 30, 2024
1 parent 455c07b commit d02df12
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 3 deletions.
5 changes: 5 additions & 0 deletions clippy_config/src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,11 @@ define_Conf! {
///
/// Don't lint when comparing the result of a modulo operation to zero.
(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()),
}

/// Search for the configuration file.
Expand Down
8 changes: 7 additions & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
excessive_nesting_threshold,
future_size_threshold,
ref ignore_interior_mutability,
ref ignored_wildcard_imports,
large_error_threshold,
literal_representation_threshold,
matches_for_let_else,
Expand Down Expand Up @@ -876,7 +877,12 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
))
});
store.register_early_pass(|| Box::new(option_env_unwrap::OptionEnvUnwrap));
store.register_late_pass(move |_| Box::new(wildcard_imports::WildcardImports::new(warn_on_all_wildcard_imports)));
store.register_late_pass(move |_| {
Box::new(wildcard_imports::WildcardImports::new(
warn_on_all_wildcard_imports,
ignored_wildcard_imports.clone(),
))
});
store.register_late_pass(|_| Box::<redundant_pub_crate::RedundantPubCrate>::default());
store.register_late_pass(|_| Box::new(unnamed_address::UnnamedAddress));
store.register_late_pass(|_| Box::<dereference::Dereferencing<'_>>::default());
Expand Down
18 changes: 16 additions & 2 deletions clippy_lints/src/wildcard_imports.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_test_module_or_function;
use clippy_utils::source::{snippet, snippet_with_applicability};
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::Applicability;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::{Item, ItemKind, PathSegment, UseKind};
Expand Down Expand Up @@ -100,13 +101,15 @@ declare_clippy_lint! {
pub struct WildcardImports {
warn_on_all: bool,
test_modules_deep: u32,
ignored_segments: Vec<String>,
}

impl WildcardImports {
pub fn new(warn_on_all: bool) -> Self {
pub fn new(warn_on_all: bool, ignored_wildcard_imports: Vec<String>) -> Self {
Self {
warn_on_all,
test_modules_deep: 0,
ignored_segments: ignored_wildcard_imports,
}
}
}
Expand Down Expand Up @@ -190,6 +193,7 @@ impl WildcardImports {
item.span.from_expansion()
|| is_prelude_import(segments)
|| (is_super_only_import(segments) && self.test_modules_deep > 0)
|| is_ignored_via_config(segments, &self.ignored_segments)
}
}

Expand All @@ -198,10 +202,20 @@ impl WildcardImports {
fn is_prelude_import(segments: &[PathSegment<'_>]) -> bool {
segments
.iter()
.any(|ps| ps.ident.name.as_str().contains(sym::prelude.as_str()))
.any(|ps| ps.ident.as_str().contains(sym::prelude.as_str()))
}

// Allow "super::*" imports in tests.
fn is_super_only_import(segments: &[PathSegment<'_>]) -> bool {
segments.len() == 1 && segments[0].ident.name == kw::Super
}

// 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();
// 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()
}
2 changes: 2 additions & 0 deletions tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
excessive-nesting-threshold
future-size-threshold
ignore-interior-mutability
ignored-wildcard-imports
large-error-threshold
literal-representation-threshold
matches-for-let-else
Expand Down Expand Up @@ -117,6 +118,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
excessive-nesting-threshold
future-size-threshold
ignore-interior-mutability
ignored-wildcard-imports
large-error-threshold
literal-representation-threshold
matches-for-let-else
Expand Down
1 change: 1 addition & 0 deletions tests/ui-toml/wildcard_imports_whitelist/clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ignored-wildcard-imports = ["utils"]
18 changes: 18 additions & 0 deletions tests/ui-toml/wildcard_imports_whitelist/wildcard_imports.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![warn(clippy::wildcard_imports)]

mod utils {
pub fn print() {}
}

mod utils_plus {
pub fn do_something() {}
}

use utils::*;
use utils_plus::do_something;
//~^ ERROR: usage of wildcard import

fn main() {
print();
do_something();
}
18 changes: 18 additions & 0 deletions tests/ui-toml/wildcard_imports_whitelist/wildcard_imports.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![warn(clippy::wildcard_imports)]

mod utils {
pub fn print() {}
}

mod utils_plus {
pub fn do_something() {}
}

use utils::*;
use utils_plus::*;
//~^ ERROR: usage of wildcard import

fn main() {
print();
do_something();
}
11 changes: 11 additions & 0 deletions tests/ui-toml/wildcard_imports_whitelist/wildcard_imports.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: usage of wildcard import
--> $DIR/wildcard_imports.rs:12:5
|
LL | use utils_plus::*;
| ^^^^^^^^^^^^^ help: try: `utils_plus::do_something`
|
= 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

0 comments on commit d02df12

Please sign in to comment.