Skip to content

wildcard_imports: lint on pub use if asked to #14182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion book/src/lint_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,8 @@ The maximum allowed size of a bit mask before suggesting to use 'trailing_zeros'


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

**Default Value:** `false`

Expand Down
3 changes: 2 additions & 1 deletion clippy_config/src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,8 @@ define_Conf! {
/// The maximum allowed size of a bit mask before suggesting to use 'trailing_zeros'
#[lints(verbose_bit_mask)]
verbose_bit_mask_threshold: u64 = 1,
/// Whether to allow certain wildcard imports (prelude, super in tests).
/// Whether to emit warnings on all wildcard imports, including those from `prelude`, from `super` in tests,
/// or for `pub use` reexports.
#[lints(wildcard_imports)]
warn_on_all_wildcard_imports: bool = false,
/// Whether to also emit warnings for unsafe blocks with metavariable expansions in **private** macros.
Expand Down
6 changes: 5 additions & 1 deletion clippy_lints/src/wildcard_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ declare_clippy_lint! {
/// (including the standard library) provide modules named "prelude" specifically designed
/// for wildcard import.
///
/// Wildcard imports reexported through `pub use` are also allowed.
///
/// `use super::*` is allowed in test modules. This is defined as any module with "test" in the name.
///
/// These exceptions can be disabled using the `warn-on-all-wildcard-imports` configuration flag.
Expand Down Expand Up @@ -121,7 +123,9 @@ impl LateLintPass<'_> for WildcardImports {
}

let module = cx.tcx.parent_module_from_def_id(item.owner_id.def_id);
if cx.tcx.visibility(item.owner_id.def_id) != ty::Visibility::Restricted(module.to_def_id()) {
if cx.tcx.visibility(item.owner_id.def_id) != ty::Visibility::Restricted(module.to_def_id())
&& !self.warn_on_all
{
return;
}
if let ItemKind::Use(use_path, UseKind::Glob) = &item.kind
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-toml/wildcard_imports/wildcard_imports.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod my_crate {
}
}

use utils::{BAR, print};
pub use utils::{BAR, print};
//~^ ERROR: usage of wildcard import
use my_crate::utils::my_util_fn;
//~^ ERROR: usage of wildcard import
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-toml/wildcard_imports/wildcard_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod my_crate {
}
}

use utils::*;
pub use utils::*;
//~^ ERROR: usage of wildcard import
use my_crate::utils::*;
//~^ ERROR: usage of wildcard import
Expand Down
6 changes: 3 additions & 3 deletions tests/ui-toml/wildcard_imports/wildcard_imports.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: usage of wildcard import
--> tests/ui-toml/wildcard_imports/wildcard_imports.rs:18:5
--> tests/ui-toml/wildcard_imports/wildcard_imports.rs:18:9
|
LL | use utils::*;
| ^^^^^^^^ help: try: `utils::{BAR, print}`
LL | pub use utils::*;
| ^^^^^^^^ help: try: `utils::{BAR, print}`
|
= note: `-D clippy::wildcard-imports` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::wildcard_imports)]`
Expand Down