Email me

Clippy lints / Pedantic

Clippy lint · Pedantic · checked with Clippy 0.1.99

clippy::wildcard_imports: Checks for wildcard imports use _::*

What it meansChecks for wildcard imports use _::*. It is in the pedantic group and off by default.

Does it matter?

wildcard imports can pollute the namespace. This is especially bad if you try to import something through a wildcard, that already has been imported by name from a different source: use crate1::foo; // Imports a function named foo use crate2::*; // Has a function named foo foo(); // Calls crate1::foo This can lead to confusing error messages at best and to unexpected behavior at worst.

Stricter lints with some false positives. Off by default; opt in with clippy::pedantic.

How to fix it

Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.

Flagged

use crate1::foo; // Imports a function named foo
use crate2::*; // Has a function named foo

foo(); // Calls crate1::foo

Write this instead

use crate1::*;

foo();

Turn it off, or on

It is off by default. To use it, set it to warn or deny:

Whole crate (Cargo.toml)

[lints.clippy]
wildcard_imports = "warn"

In code

#![warn(clippy::wildcard_imports)]

One run

cargo clippy -- -W clippy::wildcard-imports

The [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.

Settings in clippy.toml

  • warn-on-all-wildcard-imports: Whether to emit warnings on all wildcard imports, including those from prelude, from super in tests, or for pub use reexports.
Pedanticgroup
allowdefault level
yesfixed by cargo clippy --fix
1.43.0added in Rust

Clippy's own documentation

What it does

Checks for wildcard imports use _::*.

Why is this bad?

wildcard imports can pollute the namespace. This is especially bad if you try to import something through a wildcard, that already has been imported by name from a different source:

use crate1::foo; // Imports a function named foo
use crate2::*; // Has a function named foo

foo(); // Calls crate1::foo

This can lead to confusing error messages at best and to unexpected behavior at worst.

Exceptions

Wildcard imports are allowed from modules that their name contains prelude. Many crates (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.

Known problems

If macros are imported through the wildcard, this macro is not included by the suggestion and has to be added by hand.

Applying the suggestion when explicit imports of the things imported with a glob import exist, may result in unused_imports warnings.

Example

use crate1::*;

foo();

Use instead:

use crate1::foo;

foo();

Configuration

  • allowed-wildcard-imports: List of path segments allowed to have wildcard imports.

Example

allowed-wildcard-imports = [ "utils", "common" ]

Noteworthy

  1. This configuration has no effects if used with warn_on_all_wildcard_imports = true.
  2. Paths with any segment that containing the word ‘prelude’ are already allowed by default.

(default: [])

  • warn-on-all-wildcard-imports: Whether to emit warnings on all wildcard imports, including those from prelude, from super in tests, or for pub use reexports.

    (default: false)

From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.

Related lints

All 847 Clippy lints · Rust compiler errors · Cargo.toml checker

Up next

Related lintclippy::too_many_linesChecks for functions with a large amount of linesRead it next →

Did this answer your question?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ