rust-lang / rust-lang/rust-clippy
lint `Hash` impls with `match`es on enums but no `mem::discriminant` call
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Implementing Hash by hashing just the fields of enum variants may cause hash collisions. E.g.
enum Foo {
A(i32),
B(i32),
}
impl Hash for Foo {
fn hash<H: Hasher>(&self, hasher: &mut H) {
match self {
Foo::A(i) => i.hash(hasher),
Foo::B(i) => i.hash(hasher),
}
}
}
would only hash the i32s, which makes the hash of Foo::A(42) equal to the hash of Foo::B(42). What's missing here is a call to mem::discriminant(self).hash(hasher); to differentiate between the variants.
This will lead to false positives if the user is doing some sort of manual scheme to differentiate between the variants, but in general the discriminant scheme should be preferred.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue names no source file or test. Start from the Rust enum Hash example and locate the relevant Clippy lint entry point and test conventions; done means detecting matches that hash variant fields without hashing mem::discriminant, while accounting for the noted false positives.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100