A class overrides equals but not hashCode
What it finds
A class that overrides equals(Object) but not hashCode() breaks the contract that equal objects have equal hash codes: two equal objects land in different buckets of a HashMap or HashSet, which then holds both or finds neither.
Only classes that extend nothing are reported: a subclass usually adds to the equals of a superclass whose hashCode stays consistent with it. Abstract classes are left alone, since their subclasses may define hashCode, and so are equals methods that call super.equals, which compares identity like the inherited hashCode. Records derive hashCode from their components.
From the rule's source, equals_without_hashcode.rs.
class Key { private final int id; Key(int id) { this.id = id; } @Override public boolean equals(Object o) {tx0 reports: Key overrides equals but not hashCode return o instanceof Key k && k.id == id; }} class Complete { @Override public boolean equals(Object o) { return o == this; } @Override public int hashCode() { return 1; }} class Derived extends Complete { @Override public boolean equals(Object o) { return super.equals(o); }} abstract class Base { @Override public boolean equals(Object o) { return false; }} class Overload { public boolean equals(Overload o) { return o == this; }} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:equals-without-hashcode" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/java:equals-without-hashcode] reason="..."tx0 explain tx0/java:equals-without-hashcode shows the same in the terminal. Found a false positive? Report it.