rust-lang / rust-lang/rust-clippy

A lint for potential name clashes with the Rust Prelude

Open
#13,301 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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;

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the examples and stated advantages and drawbacks in this issue; no source file or test entry point is named. Done means the lint detects the shown Drop clash, does not trigger for DropEvent, and supports the shown #[expect(clippy::rust_prelude_name_clash)] suppression behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.