rust-lang / rust-lang/rust-clippy

derived_hash_with_manual_eq not firing on an enum

Open
#12,092 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-negative
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

I have an enum with a custom implementation of PartialEq and a derived Hash (and a bug because x == y wasn't always producing the same result as hash(x) == hash(y) ☠️ ), and I expected Clippy to call this out but it doesn't. My suspicion is that the lint is only looking at structs, not enums? That's only a guess though.

Lint Name

derived_hash_with_manual_eq

Reproducer

I tried this code:

use std::ops::Deref;

#[derive(Debug, Clone, Eq, Hash)]
pub enum EscapedStr<'a> {
    SingleSlice(&'a str),
    CopiedValue(String),
}

impl<'a> EscapedStr<'a> {
    pub fn as_str(&self) -> &str {
        self
    }
}

impl<'a> PartialEq for EscapedStr<'a> {
    fn eq(&self, other: &Self) -> bool {
        self.as_str() == other.as_str()
    }
}

impl<'a> Deref for EscapedStr<'a> {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        match &self {
            EscapedStr::SingleSlice(s) => s,
            EscapedStr::CopiedValue(s) => s,
        }
    }
}

I expected to see this happen: Clippy giving me a warning like

error: you are deriving `Hash` but have implemented `PartialEq` explicitly
 --> src/lib.rs:2:28
  |
2 | #[derive(Debug, Clone, Eq, Hash)]
  |                            ^^^^
  |
note: `PartialEq` implemented here
 --> src/lib.rs:8:1
  |
8 | impl PartialEq for EscapedStr {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#derived_hash_with_manual_eq
  = note: `#[deny(clippy::derived_hash_with_manual_eq)]` on by default
  = note: this error originates in the derive macro `Hash` (in Nightly builds, run with -Z macro-backtrace for more info)

Instead, this happened: Clippy was silent and said this code was fine

Version
rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: aarch64-apple-darwin
release: 1.75.0
LLVM version: 17.0.6

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 at the derived_hash_with_manual_eq lint entry point and run the Rust reproducer from the issue, comparing enum handling with the expected diagnostic. Done means the enum example produces the warning shown in the report, with coverage for this behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.