Open
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 i32
s, 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.