add configuration for [wildcard_imports] to ignore certain imports

This commit is contained in:
J-ZhengLi 2023-12-18 17:57:22 +08:00
parent 455c07b7cc
commit d02df12bd5
8 changed files with 78 additions and 3 deletions

View File

@ -571,6 +571,11 @@ pub fn get_configuration_metadata() -> Vec<ClippyConfiguration> {
///
/// 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.

View File

@ -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,
@ -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());

View File

@ -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};
@ -100,13 +101,15 @@
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,
}
}
}
@ -190,6 +193,7 @@ fn check_exceptions(&self, item: &Item<'_>, segments: &[PathSegment<'_>]) -> boo
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)
}
}
@ -198,10 +202,20 @@ fn check_exceptions(&self, item: &Item<'_>, segments: &[PathSegment<'_>]) -> boo
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()
}

View File

@ -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
@ -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

View File

@ -0,0 +1 @@
ignored-wildcard-imports = ["utils"]

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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