Skip to content
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

std_wildcard_imports to lint against use std::mod::* #13961

Open
tgross35 opened this issue Jan 7, 2025 · 2 comments
Open

std_wildcard_imports to lint against use std::mod::* #13961

tgross35 opened this issue Jan 7, 2025 · 2 comments
Labels
A-lint Area: New lints

Comments

@tgross35
Copy link
Contributor

tgross35 commented Jan 7, 2025

What it does

This is a version of wildcard_imports that affects only imports from the standard crates std, core, proc_macro, alloc, and test.

Advantage

wildcard_imports is in pedantic, which seems reasonable for most crates. However, using glob imports from the standard library means that upgrading Rust version can break the build. Recent examples:

I am proposing std_wildcard_imports as a separate lint from wildcard_imports so it can get enabled by default, likely in clippy::style (or possibly uplifted rust-lang/rust#135672 (comment))

Drawbacks

Confusion between wildcard_imports and std_wildcard_imports, noise on existing code, sometimes use proc_macro::* or similar is fine for testing.

Example

Lint on glob imports:

use std::mem::*; // error
use core::sync::*; // error
use proc_macro::*; // error

However, using glob imports from prelude modules should not raise this warning:

use std::io::prelude::*; // ok
use std::prelude::rust_2021:* // unlikely, but still ok

Non-std crates don't raise this lint

use regex::*; // ok
@tgross35 tgross35 added the A-lint Area: New lints label Jan 7, 2025
@tgross35
Copy link
Contributor Author

tgross35 commented Jan 7, 2025

Quick usage search https://github.com/search?q=lang%3Arust+%2Fuse+%28std%7Ccore%7Cproc_macro%29.*%3A%3A%5C*%2F&type=code. Most of those are io::prelude, but there are at least a handful of results on the first few pages that would be linted.

@jieyouxu
Copy link
Member

jieyouxu commented Jan 22, 2025

There's a caveat that the prelude modules like std::io::prelude::* can still participate in breakage, in that a user may glob import another external crate in addition to glob imports of one of these std preludes, then if that external crate adds something that collides with the (practically frozen) std preludes, you can still end up with glob-vs-glob ambiguous. Less likely of course, agreed with not linting on std preludes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

2 participants