Skip to content

Commit d02df12

Browse files
committedJan 30, 2024
add configuration for [wildcard_imports] to ignore certain imports
1 parent 455c07b commit d02df12

File tree

8 files changed

+78
-3
lines changed

8 files changed

+78
-3
lines changed
 

Diff for: ‎clippy_config/src/conf.rs

+5
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,11 @@ define_Conf! {
571571
///
572572
/// Don't lint when comparing the result of a modulo operation to zero.
573573
(allow_comparison_to_zero: bool = true),
574+
/// Lint: WILDCARD_IMPORTS.
575+
///
576+
/// List of path segments to ignore when checking wildcard imports,
577+
/// could get overrided by `warn_on_all_wildcard_imports`.
578+
(ignored_wildcard_imports: Vec<String> = Vec::new()),
574579
}
575580

576581
/// Search for the configuration file.

Diff for: ‎clippy_lints/src/lib.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
545545
excessive_nesting_threshold,
546546
future_size_threshold,
547547
ref ignore_interior_mutability,
548+
ref ignored_wildcard_imports,
548549
large_error_threshold,
549550
literal_representation_threshold,
550551
matches_for_let_else,
@@ -876,7 +877,12 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
876877
))
877878
});
878879
store.register_early_pass(|| Box::new(option_env_unwrap::OptionEnvUnwrap));
879-
store.register_late_pass(move |_| Box::new(wildcard_imports::WildcardImports::new(warn_on_all_wildcard_imports)));
880+
store.register_late_pass(move |_| {
881+
Box::new(wildcard_imports::WildcardImports::new(
882+
warn_on_all_wildcard_imports,
883+
ignored_wildcard_imports.clone(),
884+
))
885+
});
880886
store.register_late_pass(|_| Box::<redundant_pub_crate::RedundantPubCrate>::default());
881887
store.register_late_pass(|_| Box::new(unnamed_address::UnnamedAddress));
882888
store.register_late_pass(|_| Box::<dereference::Dereferencing<'_>>::default());

Diff for: ‎clippy_lints/src/wildcard_imports.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::is_test_module_or_function;
33
use clippy_utils::source::{snippet, snippet_with_applicability};
4+
use rustc_data_structures::fx::FxHashSet;
45
use rustc_errors::Applicability;
56
use rustc_hir::def::{DefKind, Res};
67
use rustc_hir::{Item, ItemKind, PathSegment, UseKind};
@@ -100,13 +101,15 @@ declare_clippy_lint! {
100101
pub struct WildcardImports {
101102
warn_on_all: bool,
102103
test_modules_deep: u32,
104+
ignored_segments: Vec<String>,
103105
}
104106

105107
impl WildcardImports {
106-
pub fn new(warn_on_all: bool) -> Self {
108+
pub fn new(warn_on_all: bool, ignored_wildcard_imports: Vec<String>) -> Self {
107109
Self {
108110
warn_on_all,
109111
test_modules_deep: 0,
112+
ignored_segments: ignored_wildcard_imports,
110113
}
111114
}
112115
}
@@ -190,6 +193,7 @@ impl WildcardImports {
190193
item.span.from_expansion()
191194
|| is_prelude_import(segments)
192195
|| (is_super_only_import(segments) && self.test_modules_deep > 0)
196+
|| is_ignored_via_config(segments, &self.ignored_segments)
193197
}
194198
}
195199

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

204208
// Allow "super::*" imports in tests.
205209
fn is_super_only_import(segments: &[PathSegment<'_>]) -> bool {
206210
segments.len() == 1 && segments[0].ident.name == kw::Super
207211
}
212+
213+
// Allow skipping imports containing user configured segments,
214+
// i.e. "...::utils::...::*" if user put `ignored-wildcard-imports = ["utils"]` in `Clippy.toml`
215+
fn is_ignored_via_config(segments: &[PathSegment<'_>], ignored_segments: &[String]) -> bool {
216+
let segments_set: FxHashSet<&str> = segments.iter().map(|s| s.ident.as_str()).collect();
217+
let ignored_set: FxHashSet<&str> = ignored_segments.iter().map(String::as_str).collect();
218+
// segment matching need to be exact instead of using 'contains', in case user unintentionaly put
219+
// a single character in the config thus skipping most of the warnings.
220+
segments_set.intersection(&ignored_set).next().is_some()
221+
}

Diff for: ‎tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
3939
excessive-nesting-threshold
4040
future-size-threshold
4141
ignore-interior-mutability
42+
ignored-wildcard-imports
4243
large-error-threshold
4344
literal-representation-threshold
4445
matches-for-let-else
@@ -117,6 +118,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
117118
excessive-nesting-threshold
118119
future-size-threshold
119120
ignore-interior-mutability
121+
ignored-wildcard-imports
120122
large-error-threshold
121123
literal-representation-threshold
122124
matches-for-let-else

Diff for: ‎tests/ui-toml/wildcard_imports_whitelist/clippy.toml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ignored-wildcard-imports = ["utils"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![warn(clippy::wildcard_imports)]
2+
3+
mod utils {
4+
pub fn print() {}
5+
}
6+
7+
mod utils_plus {
8+
pub fn do_something() {}
9+
}
10+
11+
use utils::*;
12+
use utils_plus::do_something;
13+
//~^ ERROR: usage of wildcard import
14+
15+
fn main() {
16+
print();
17+
do_something();
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![warn(clippy::wildcard_imports)]
2+
3+
mod utils {
4+
pub fn print() {}
5+
}
6+
7+
mod utils_plus {
8+
pub fn do_something() {}
9+
}
10+
11+
use utils::*;
12+
use utils_plus::*;
13+
//~^ ERROR: usage of wildcard import
14+
15+
fn main() {
16+
print();
17+
do_something();
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: usage of wildcard import
2+
--> $DIR/wildcard_imports.rs:12:5
3+
|
4+
LL | use utils_plus::*;
5+
| ^^^^^^^^^^^^^ help: try: `utils_plus::do_something`
6+
|
7+
= note: `-D clippy::wildcard-imports` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::wildcard_imports)]`
9+
10+
error: aborting due to 1 previous error
11+

0 commit comments

Comments
 (0)