rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

A lint for potential name clashes with the Rust Prelude

Open LikeLakers2 opened this issue 1 year ago • 3 comments

What it does

This lint would check for potential name clashes in a crate with the Rust Prelude.

Name clashes with the Rust Standard Library are not too big of a problem - in those cases, you simply rename one import or the other. However, the Rust Prelude is special in that it is included everywhere - and its imports can be overridden.

This allows for name clashes to happen - and because Rust allows the rust prelude's imports to be overridden, it's not immediately obvious that it will cause problems.

As a real world example of this happening, see https://github.com/bevyengine/bevy/issues/14902, where the Bevy Game Engine accidentally introduced a name clash with std::ops::Drop - and nobody noticed until they tried to impl Drop for SomeStruct in the same file as importing the Bevy Prelude.

Advantage

Drawbacks

  • If a user is overriding a item from the Rust Prelude that they don't need, this may result in unwanted lint output.

Example

I do not imagine this lint could be automatically fixed, so this will be an example of when this lint will show, and how a user might fix it.

I imagine it should lint on code such as this:

pub struct Drop;

The lint would suggest renaming the offending struct. Afterwards, it might look like this, which should not trigger this lint:

pub struct DropEvent;

Alternatively, if a user is okay with the name clash (i.e. they don't use std::ops::Drop), they can edit their code to show that they're okay with that:

#[expect(clippy::rust_prelude_name_clash)]
pub struct Drop;

LikeLakers2 avatar Aug 24 '24 17:08 LikeLakers2

#[expect(clippy::rust_prelude_name_clash)]
pub struct Drop;

Is #[expect(...)] stable Rust? I didn't think it got out of nightly yet.

BD103 avatar Aug 24 '24 19:08 BD103

@BD103 I don't remember. If it's not, just pretend expect is replaced with allow. The point of that code block was to say "Here's what they can do to show that they acknowledge the clashing name."

LikeLakers2 avatar Aug 24 '24 20:08 LikeLakers2

Ok! I was hoping to use it in my own projects, which is why I was asking.

BD103 avatar Aug 24 '24 21:08 BD103