-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Lint against shadowing the prelude #8439
Comments
I believe this is the same issue and this can produce incredibly surprising behavior. #![deny(clippy::all, clippy::pedantic)]
fn format() {
println!("my format function");
}
use format as renamed;
fn main() {
renamed();
let x = 42_i32;
let y = renamed!("foo{}", x);
println!("{y}");
} outputs:
|
I hadn't even thought of different namespaces, but that's a good point — even without any Speaking precisely, that's not shadowing (since both items stay accessible), but it seems closely enough related to the goals of this lint to include. |
What about |
@hellow554 Yes, that is exactly what would be flagged by this lint. It is a tradeoff between different features the code could have, just like many other clippy lints — by disabling the lint, you can have concise conventional names, and by enabling the lint, the reader can know that all prelude names mean what they usually do. |
Maybe a "subsection" (it actually isn't, but it's very similar) that could be warn-by-default would be primitive types, i.e. |
I'd love to see this lint as well, specifically to catch cases like |
…n Rust 1.80 `std::mem::{size,align}_of{,_val}` was added to `std::prelude` in Rust 1.80; see [`rust`#123168](rust-lang/rust#123168). However, we don't have an MSRV at 1.80 or higher yet. So, let's work around it by importing these items fully. Since neither `clippy` nor `rustc` lint against shadowed `prelude` items yet (see also a [proposed `clippy` lint] for such), that lets us remove the explicit `std::mem::*` imports later at our leisure. [proposed `clippy` lint]: rust-lang/rust-clippy#8439
…n Rust 1.80 `std::mem::{size,align}_of{,_val}` was added to `std::prelude` in Rust 1.80; see [`rust`#123168](rust-lang/rust#123168). However, we don't have an MSRV at 1.80 or higher yet. So, let's work around it by importing these items fully. Since neither `clippy` nor `rustc` lint against shadowed `prelude` items yet (see also a [proposed `clippy` lint] for such), that lets us remove the explicit `std::mem::*` imports later at our leisure. [proposed `clippy` lint]: rust-lang/rust-clippy#8439
It's possible to make accidental import of
It would be nice if this fail-safe form was excluded from the lint. |
@kornelski I'd expect the lint to be allow-by-default, and if explicitly changed to warn I would expect to get a warning for shadowing |
As the author of this proposal, one of the things I would like it to be good for helping people avoid confusion, or writing confusing code, by unintentionally shadowing a prelude item that is not like Perhaps a way to resolve this tension would be to make it warn-by-default with a configurable exclusion list, set by default to |
Having a more conservative variant that's warn-by-default and a lint for all shadowing that's allow-by-default sounds reasonable. |
What it does
Lints cases where a
use
, item declaration, or local binding shadows an item in the Rust prelude.In the case of a
use
, it suggests instead using the containing module, i.e. replacinguse std::io::Result;
withuse std::io;
Lint Name
shadow_prelude
Category
style, pedantic, restriction
Advantage
Code which redefines well-known names can be confusing to read, especially for beginners, or people who are trying to help a beginner but have only a code excerpt rather than a complete file with
use
s visible. (“This return type needs to beResult<(), MyError>
.” “I did that, but it says thatResult
only takes 1 generic argument.” “Oh, you must have imported another Result; you need to remove that and change all your usages.”)If an author intends to avoid shadowing, they may benefit from the lint catching when e.g. their IDE helpfully inserts an unwanted
use
.It would also catch when a library author accidentally chooses a name for an item that conflicts with the standard prelude, allowing them to decide whether to reuse the name or choose a new one.
Drawbacks
It increases the verbosity of code obeying the restriction.
Shadowing
Result
andError
names is common enough practice that it might be objectionable to enable this lint by default, which limits its utility in the primary use case of helping beginners.Example
Could be written as:
The text was updated successfully, but these errors were encountered: