Hashing a unit value doesn’t accomplish anything because the implementation of Hash
for ()
is a no-op. This can lead to
confusion or misleading code, as it implies some hashing operation is actually taking place, when in reality it does nothing.
Code examples
Noncompliant code example
enum Foo { Empty, WithValue(u8) }
use Foo::*;
let mut state = DefaultHasher::new();
let my_enum = Foo::Empty;
match my_enum {
Empty => ().hash(&mut state), // Noncompliant
WithValue(x) => x.hash(&mut state),
}
Compliant solution
enum Foo { Empty, WithValue(u8) }
use Foo::*;
let mut state = DefaultHasher::new();
let my_enum = Foo::Empty;
match my_enum {
Empty => 0_u8.hash(&mut state), // Compliant
WithValue(x) => x.hash(&mut state),
}