Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 809c931

Browse files
committed
wildcard_imports: lint on pub use if asked to
`warn_on_all_wildcard_imports` should warn on all wildcard imports, including the reexported ones.
1 parent a8b1782 commit 809c931

File tree

6 files changed

+14
-8
lines changed

6 files changed

+14
-8
lines changed

book/src/lint_configuration.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,8 @@ The maximum allowed size of a bit mask before suggesting to use 'trailing_zeros'
10301030

10311031

10321032
## `warn-on-all-wildcard-imports`
1033-
Whether to allow certain wildcard imports (prelude, super in tests).
1033+
Whether to emit warnings on all wildcard imports, including those from `prelude`, from `super` in tests,
1034+
or for `pub use` reexports.
10341035

10351036
**Default Value:** `false`
10361037

clippy_config/src/conf.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,8 @@ define_Conf! {
737737
/// The maximum allowed size of a bit mask before suggesting to use 'trailing_zeros'
738738
#[lints(verbose_bit_mask)]
739739
verbose_bit_mask_threshold: u64 = 1,
740-
/// Whether to allow certain wildcard imports (prelude, super in tests).
740+
/// Whether to emit warnings on all wildcard imports, including those from `prelude`, from `super` in tests,
741+
/// or for `pub use` reexports.
741742
#[lints(wildcard_imports)]
742743
warn_on_all_wildcard_imports: bool = false,
743744
/// Whether to also emit warnings for unsafe blocks with metavariable expansions in **private** macros.

clippy_lints/src/wildcard_imports.rs

Lines changed: 5 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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)