Skip to content

Commit c82162e

Browse files
committed
Auto merge of #11979 - J-ZhengLi:issue11428, r=Alexendoo
add configuration for [`wildcard_imports`] to ignore certain imports fixes: #11428 changelog: add configuration `ignored-wildcard-imports` for lint [`wildcard_imports`]
2 parents 9b6f866 + 46dd826 commit c82162e

File tree

14 files changed

+184
-7
lines changed

14 files changed

+184
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5824,4 +5824,5 @@ Released 2018-09-13
58245824
[`check-private-items`]: https://doc.rust-lang.org/clippy/lint_configuration.html#check-private-items
58255825
[`pub-underscore-fields-behavior`]: https://doc.rust-lang.org/clippy/lint_configuration.html#pub-underscore-fields-behavior
58265826
[`allow-comparison-to-zero`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-comparison-to-zero
5827+
[`allowed-wildcard-imports`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allowed-wildcard-imports
58275828
<!-- end autogenerated links to configuration documentation -->

book/src/lint_configuration.md

+22
Original file line numberDiff line numberDiff line change
@@ -838,3 +838,25 @@ Don't lint when comparing the result of a modulo operation to zero.
838838
* [`modulo_arithmetic`](https://rust-lang.github.io/rust-clippy/master/index.html#modulo_arithmetic)
839839

840840

841+
## `allowed-wildcard-imports`
842+
List of path segments allowed to have wildcard imports.
843+
844+
#### Example
845+
846+
```toml
847+
allowed-wildcard-imports = [ "utils", "common" ]
848+
```
849+
850+
#### Noteworthy
851+
852+
1. This configuration has no effects if used with `warn_on_all_wildcard_imports = true`.
853+
2. Paths with any segment that containing the word 'prelude'
854+
are already allowed by default.
855+
856+
**Default Value:** `[]`
857+
858+
---
859+
**Affected lints:**
860+
* [`wildcard_imports`](https://rust-lang.github.io/rust-clippy/master/index.html#wildcard_imports)
861+
862+

clippy_config/src/conf.rs

+16
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,22 @@ 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 allowed to have wildcard imports.
577+
///
578+
/// #### Example
579+
///
580+
/// ```toml
581+
/// allowed-wildcard-imports = [ "utils", "common" ]
582+
/// ```
583+
///
584+
/// #### Noteworthy
585+
///
586+
/// 1. This configuration has no effects if used with `warn_on_all_wildcard_imports = true`.
587+
/// 2. Paths with any segment that containing the word 'prelude'
588+
/// are already allowed by default.
589+
(allowed_wildcard_imports: FxHashSet<String> = FxHashSet::default()),
574590
}
575591

576592
/// Search for the configuration file.

clippy_lints/src/lib.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
524524
ref allowed_dotfiles,
525525
ref allowed_idents_below_min_chars,
526526
ref allowed_scripts,
527+
ref allowed_wildcard_imports,
527528
ref arithmetic_side_effects_allowed_binary,
528529
ref arithmetic_side_effects_allowed_unary,
529530
ref arithmetic_side_effects_allowed,
@@ -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+
allowed_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());

clippy_lints/src/wildcard_imports.rs

+14-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+
allowed_segments: FxHashSet<String>,
103105
}
104106

105107
impl WildcardImports {
106-
pub fn new(warn_on_all: bool) -> Self {
108+
pub fn new(warn_on_all: bool, allowed_wildcard_imports: FxHashSet<String>) -> Self {
107109
Self {
108110
warn_on_all,
109111
test_modules_deep: 0,
112+
allowed_segments: allowed_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_allowed_via_config(segments, &self.allowed_segments)
193197
}
194198
}
195199

@@ -198,10 +202,18 @@ 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 `allowed-wildcard-imports = ["utils"]` in `Clippy.toml`
215+
fn is_allowed_via_config(segments: &[PathSegment<'_>], allowed_segments: &FxHashSet<String>) -> bool {
216+
// segment matching need to be exact instead of using 'contains', in case user unintentionaly put
217+
// a single character in the config thus skipping most of the warnings.
218+
segments.iter().any(|seg| allowed_segments.contains(seg.ident.as_str()))
219+
}

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
1515
allowed-duplicate-crates
1616
allowed-idents-below-min-chars
1717
allowed-scripts
18+
allowed-wildcard-imports
1819
arithmetic-side-effects-allowed
1920
arithmetic-side-effects-allowed-binary
2021
arithmetic-side-effects-allowed-unary
@@ -93,6 +94,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
9394
allowed-duplicate-crates
9495
allowed-idents-below-min-chars
9596
allowed-scripts
97+
allowed-wildcard-imports
9698
arithmetic-side-effects-allowed
9799
arithmetic-side-effects-allowed-binary
98100
arithmetic-side-effects-allowed-unary
@@ -171,6 +173,7 @@ error: error reading Clippy's configuration file: unknown field `allow_mixed_uni
171173
allowed-duplicate-crates
172174
allowed-idents-below-min-chars
173175
allowed-scripts
176+
allowed-wildcard-imports
174177
arithmetic-side-effects-allowed
175178
arithmetic-side-effects-allowed-binary
176179
arithmetic-side-effects-allowed-unary
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
warn-on-all-wildcard-imports = true
2+
3+
# This should be ignored since `warn-on-all-wildcard-imports` has higher precedence
4+
allowed-wildcard-imports = ["utils"]

tests/ui-toml/wildcard_imports/wildcard_imports.fixed

+19
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,28 @@
33
mod prelude {
44
pub const FOO: u8 = 1;
55
}
6+
7+
mod utils {
8+
pub const BAR: u8 = 1;
9+
pub fn print() {}
10+
}
11+
12+
mod my_crate {
13+
pub mod utils {
14+
pub fn my_util_fn() {}
15+
}
16+
}
17+
18+
use utils::{BAR, print};
19+
//~^ ERROR: usage of wildcard import
20+
use my_crate::utils::my_util_fn;
21+
//~^ ERROR: usage of wildcard import
622
use prelude::FOO;
723
//~^ ERROR: usage of wildcard import
824

925
fn main() {
1026
let _ = FOO;
27+
let _ = BAR;
28+
print();
29+
my_util_fn();
1130
}

tests/ui-toml/wildcard_imports/wildcard_imports.rs

+19
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,28 @@
33
mod prelude {
44
pub const FOO: u8 = 1;
55
}
6+
7+
mod utils {
8+
pub const BAR: u8 = 1;
9+
pub fn print() {}
10+
}
11+
12+
mod my_crate {
13+
pub mod utils {
14+
pub fn my_util_fn() {}
15+
}
16+
}
17+
18+
use utils::*;
19+
//~^ ERROR: usage of wildcard import
20+
use my_crate::utils::*;
21+
//~^ ERROR: usage of wildcard import
622
use prelude::*;
723
//~^ ERROR: usage of wildcard import
824

925
fn main() {
1026
let _ = FOO;
27+
let _ = BAR;
28+
print();
29+
my_util_fn();
1130
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
error: usage of wildcard import
2-
--> $DIR/wildcard_imports.rs:6:5
2+
--> $DIR/wildcard_imports.rs:18:5
33
|
4-
LL | use prelude::*;
5-
| ^^^^^^^^^^ help: try: `prelude::FOO`
4+
LL | use utils::*;
5+
| ^^^^^^^^ help: try: `utils::{BAR, print}`
66
|
77
= note: `-D clippy::wildcard-imports` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::wildcard_imports)]`
99

10-
error: aborting due to 1 previous error
10+
error: usage of wildcard import
11+
--> $DIR/wildcard_imports.rs:20:5
12+
|
13+
LL | use my_crate::utils::*;
14+
| ^^^^^^^^^^^^^^^^^^ help: try: `my_crate::utils::my_util_fn`
15+
16+
error: usage of wildcard import
17+
--> $DIR/wildcard_imports.rs:22:5
18+
|
19+
LL | use prelude::*;
20+
| ^^^^^^^^^^ help: try: `prelude::FOO`
21+
22+
error: aborting due to 3 previous errors
1123

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
allowed-wildcard-imports = ["utils"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
mod my_crate {
12+
pub mod utils {
13+
pub fn my_util_fn() {}
14+
}
15+
}
16+
17+
use my_crate::utils::*;
18+
use utils::*;
19+
use utils_plus::do_something;
20+
//~^ ERROR: usage of wildcard import
21+
22+
fn main() {
23+
print();
24+
my_util_fn();
25+
do_something();
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
mod my_crate {
12+
pub mod utils {
13+
pub fn my_util_fn() {}
14+
}
15+
}
16+
17+
use my_crate::utils::*;
18+
use utils::*;
19+
use utils_plus::*;
20+
//~^ ERROR: usage of wildcard import
21+
22+
fn main() {
23+
print();
24+
my_util_fn();
25+
do_something();
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: usage of wildcard import
2+
--> $DIR/wildcard_imports.rs:19: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)