New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lint: Warn if only one of {Hash, PartialEq} is derived, the other manually implemented #1499
Comments
I think I have seen lots of code that derives PartialEq without any impl of Hash (derived or not) I wouldn't want warnings on all those cases |
The proposal in bold doesn't concern those types. I've now updated the title as well. To be clear:
|
This seems like something that can be handled in e.g. clippy. |
One direction is in clippy: https://github.com/Manishearth/rust-clippy/wiki#derive_hash_not_eq |
This is awesome! On Mon, Feb 15, 2016 at 01:39:17AM -0800, Oliver Schneider wrote:
|
Clippy now requires either both derived or both not derived: https://github.com/Manishearth/rust-clippy/pull/676 |
Let's close this issue for now and see how the lint in Clippy works out. If it works well enough I'd still like to see it in rustc itself, but it's probably too early to tell. |
This is a continuation of rust-lang/rust#29758
The derived
Hash
andPartialEq
implementations for structs rely on the struct fields' implementations of those traits for comparison and hashing. If I manually implement one of those traits by myself, the following rule might be broken:(found in the docs of
Hash
)See the previous issue on top for an example.
This seems like a potential pitfall. In my particular case, I had a struct with
derive(Eq, PartialEq, Hash)
, and wanted to change the way equality works. So I manually implementedPartialEq
, but forgot to do the same forHash
as well. Luckily @eefriedman reminded me of the incorrectHash
impl during code review, but the code compiled perfectly fine.I think
rustc
could detect this kind of situation. IMO the compiler should warn if one of{Hash, PartialEq}
is derived, and the other trait manually implemented, because:derive
The text was updated successfully, but these errors were encountered: