Open kchibisov opened 1 month ago
In the general case, there is no way to determine if a impl Hash
is stable. Consider fn hash(&self, state: &mut Hasher) { if const { riemann_hypothesis() } { self.value.hash(state) } }
. Taking into account "accidental mutability", imho this is non-goal for this lint.
Yeah, it's really hard to figure this stuff out if you have a hash, though, general AST matching can detect if you use value the at all inside the hash
implementation.
Maybe for the rest it's fine to force the user to annotate or just not throw if you have custom hash impl...
By "force the user to annotate," is there a way to annotate the type as being safe for use as a hash key (because e.g. the interior mutable field is explicitly excluded for eq/hash), or are you suggesting that every user of the type for the rest of time needs to add a Clippy annotation to suppress this false positive?
I mean to suppress the warning on this particular struct, etc, so to suppress the false positive. The implementation is edgy, so probably it's not a bad idea, it's just the issue is that you can only see the issue once you try to hash
it from what I've seen.
Summary
With the custom
Hash
implementation for thestruct
the said lint often fails because it sees themutable
field inside the struct. However the way how this field is used could be very different and such field could be not used at all.Lint Name
mutable_key_type
Reproducer
I tried this code:
I saw this happen:
I expected to see this happen:
It shouldn't trigger because the unmutable part of the key is used, as in the pointer to the
Arc
which can't change.Also, if you completely remove the
self.alive
part of theHash
implementation it'll still warn, even though the object won't be a part of thehash
meaning that it's mutability can not affect anything.Version
Additional Labels
No response