Skip to content

Commit 9c6cb51

Browse files
authored
wildcard_imports: lint on pub use if asked to (#14182)
`warn_on_all_wildcard_imports` should warn on all wildcard imports, including the reexported ones. Fix #13660 changelog: [`warn_on_all_wildcard_imports`]: when asked to warn on all wildcard imports, include the reexported ones
2 parents b27a2bb + 809c931 commit 9c6cb51

File tree

6 files changed

+14
-8
lines changed

6 files changed

+14
-8
lines changed

book/src/lint_configuration.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,8 @@ The maximum allowed size of a bit mask before suggesting to use 'trailing_zeros'
10701070

10711071

10721072
## `warn-on-all-wildcard-imports`
1073-
Whether to allow certain wildcard imports (prelude, super in tests).
1073+
Whether to emit warnings on all wildcard imports, including those from `prelude`, from `super` in tests,
1074+
or for `pub use` reexports.
10741075

10751076
**Default Value:** `false`
10761077

clippy_config/src/conf.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,8 @@ define_Conf! {
764764
/// The maximum allowed size of a bit mask before suggesting to use 'trailing_zeros'
765765
#[lints(verbose_bit_mask)]
766766
verbose_bit_mask_threshold: u64 = 1,
767-
/// Whether to allow certain wildcard imports (prelude, super in tests).
767+
/// Whether to emit warnings on all wildcard imports, including those from `prelude`, from `super` in tests,
768+
/// or for `pub use` reexports.
768769
#[lints(wildcard_imports)]
769770
warn_on_all_wildcard_imports: bool = false,
770771
/// Whether to also emit warnings for unsafe blocks with metavariable expansions in **private** macros.

clippy_lints/src/wildcard_imports.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ declare_clippy_lint! {
6868
/// (including the standard library) provide modules named "prelude" specifically designed
6969
/// for wildcard import.
7070
///
71+
/// Wildcard imports reexported through `pub use` are also allowed.
72+
///
7173
/// `use super::*` is allowed in test modules. This is defined as any module with "test" in the name.
7274
///
7375
/// These exceptions can be disabled using the `warn-on-all-wildcard-imports` configuration flag.
@@ -121,7 +123,9 @@ impl LateLintPass<'_> for WildcardImports {
121123
}
122124

123125
let module = cx.tcx.parent_module_from_def_id(item.owner_id.def_id);
124-
if cx.tcx.visibility(item.owner_id.def_id) != ty::Visibility::Restricted(module.to_def_id()) {
126+
if cx.tcx.visibility(item.owner_id.def_id) != ty::Visibility::Restricted(module.to_def_id())
127+
&& !self.warn_on_all
128+
{
125129
return;
126130
}
127131
if let ItemKind::Use(use_path, UseKind::Glob) = &item.kind

tests/ui-toml/wildcard_imports/wildcard_imports.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mod my_crate {
1515
}
1616
}
1717

18-
use utils::{BAR, print};
18+
pub use utils::{BAR, print};
1919
//~^ ERROR: usage of wildcard import
2020
use my_crate::utils::my_util_fn;
2121
//~^ ERROR: usage of wildcard import

tests/ui-toml/wildcard_imports/wildcard_imports.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mod my_crate {
1515
}
1616
}
1717

18-
use utils::*;
18+
pub use utils::*;
1919
//~^ ERROR: usage of wildcard import
2020
use my_crate::utils::*;
2121
//~^ ERROR: usage of wildcard import

tests/ui-toml/wildcard_imports/wildcard_imports.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: usage of wildcard import
2-
--> tests/ui-toml/wildcard_imports/wildcard_imports.rs:18:5
2+
--> tests/ui-toml/wildcard_imports/wildcard_imports.rs:18:9
33
|
4-
LL | use utils::*;
5-
| ^^^^^^^^ help: try: `utils::{BAR, print}`
4+
LL | pub 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)]`

0 commit comments

Comments
 (0)