rust-lang / rust-lang/rust-clippy

Suggest removing explicit PartialEq implementation that could have been derived

Open
#6,484 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint E-medium L-complexity T-middle
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Suggest that when the explicit implementation of PartialEq matches the one that would have been produced by #[derive(PartialEq)] the user should use the derive. A similar hint could be written for PartialOrd, Hash and perhaps a few others.

Categories (optional)
  • Kind: clippy::style

What is the advantage of the recommended code over the original code

  • Less code.
  • Correctly adjusts as fields are added/renamed.
Drawbacks

Should a user explicitly desire an implementation identical to what would be generated, but not that evolves with the data type in future, the user would have to ignore the lint. I believe that such instances deserve a comment as to why (e.g. that additional fields should not participate in the equality), and attaching that comment to a lint ignore makes a lot of sense.

Example
struct Foo(u64);

impl PartialEq for Foo {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}

Could be written as:

#[derive(PartialEq)]
struct Foo(u64);

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

The issue provides no file, test, or entry point; begin by locating existing Clippy style lints that compare explicit trait implementations with derives. Define the matching behavior for PartialEq first, then consider whether the same approach applies to PartialOrd and Hash, with completion requiring the requested lint behavior and coverage for the shown example.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
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.