Skip to content

Add new lint: std_wildcard_imports #14868

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

alanbondarun
Copy link

@alanbondarun alanbondarun commented May 22, 2025

changelog: [std_wildcard_imports]: Initial implementation

This fixes #13961.

Please let me know if there any missing test cases and/or changes!

TODO

  • Fix failed tests from other lints
  • Add more test cases

@alanbondarun alanbondarun force-pushed the std_wildcard_imports branch 4 times, most recently from 3a0a721 to 5f8b8e3 Compare May 27, 2025 09:40
@alanbondarun alanbondarun marked this pull request as ready for review May 27, 2025 09:43
@rustbot
Copy link
Collaborator

rustbot commented May 27, 2025

r? @llogiq

rustbot has assigned @llogiq.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label May 27, 2025
@alanbondarun alanbondarun changed the title [WIP] Add new lint: std_wildcard_imports Add new lint: std_wildcard_imports May 27, 2025
@rustbot

This comment has been minimized.

@alanbondarun alanbondarun force-pushed the std_wildcard_imports branch from 5f8b8e3 to 80ac147 Compare June 2, 2025 12:36
Copy link
Contributor

@llogiq llogiq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be some duplication with the wildcard_imports lint. It probably makes sense to pull those lint passes together into one pass that implements both lints.

Otherwise I'm dubious about making this a warn-by-default lint, but we can discuss this during the final comment period.

/// Wildcard imports can pollute the namespace. This is especially bad when importing from the
/// standard library through wildcards:
///
/// ```no_run
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This belongs into the Example section.

/// ```
#[clippy::version = "1.89.0"]
pub STD_WILDCARD_IMPORTS,
style,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the lint group. Yes, namespace pollution is bad. But there are four stdlib namespaces (of which three might be imported from in somewhat normal code), so this could lead to serious churn.

I also don't follow the argument that names imported from std are somehow worse than others. Why? The standard library moves slower than other crates if anything.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Sorry for late reply) Then how about setting the lint group to pedantic, and denying this lint in Cargo.toml of standard libraries? I think it would solve the issue without editing a bunch of existing codes outside of stdlib.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some context, the libs team expressed interest in having some form of this builtin at some point rust-lang/rust#135672 (comment). But we likely need a smarter version for that - some discussion at rust-lang/rust#142448.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the discussions in the linked issues, I suggest we close this PR, clearly define what this lint should do, and then implement it accordingly. What do you think of this approach?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I still think this is useful in its current state, just with some updated docs per @jieyouxu's comment. We probably eventually want some version of this as a rustc builtin lint and that will be a bit smarter, but having something in Clippy (regardless of whatever level the maintainers feel comfortable placing it at) definitely helps get us off the ground.

cx,
STD_WILDCARD_IMPORTS,
span,
"usage of wildcard import from `std` crates",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be shorter and clearer to replace the "std crates" with the actual crate name.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jun 2, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 4, 2025

☔ The latest upstream changes (possibly 84ef7fb) made this pull request unmergeable. Please resolve the merge conflicts.

Comment on lines 16 to 17
/// Wildcard imports can pollute the namespace. This is especially bad when importing from the
/// standard library through wildcards:
Copy link
Member

@jieyouxu jieyouxu Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: as far as I understand it, the motivation of #13961 is mostly not about namespace pollution, but about name resolution breakage (such as introduction of new ambiguities) from people glob-importing std modules, and then std introducing a new item whose name collides with that of an item defined locally. The very bad part about this situation is that even unstable std API editions can introduce name resolution ambiguities.

That is, the strongest motivation for this lint is the name resolution ambiguity related breakages, and not namespace pollution.

@alanbondarun alanbondarun force-pushed the std_wildcard_imports branch from 80ac147 to de87094 Compare June 24, 2025 11:54
@alanbondarun alanbondarun force-pushed the std_wildcard_imports branch from de87094 to 4c4c928 Compare June 24, 2025 12:06
@alanbondarun
Copy link
Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jun 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

std_wildcard_imports to lint against use std::mod::*
5 participants