A class overrides hashCode but not equals

tx0/java:hashcode-without-equalsrecommendedwarningbug

What it finds

A class that overrides hashCode() but not equals(Object) keeps the identity comparison of Object: the hash code then says nothing that equals agrees with, and a second object with the same value is not found in a HashSet. The two methods are written together or not at all.

Only classes that extend nothing are reported, and abstract classes are left alone.

From the rule's source, hashcode_without_equals.rs.

Example from the rule's tests
class Key {  int id;   @Override  public int hashCode() {tx0 reports: Key overrides hashCode but not equals    return id;  }} class Pair {  int a;   @Override  public int hashCode() {    return a;  }   @Override  public boolean equals(Object o) {    return o instanceof Pair && ((Pair) o).a == a;  }} class Child extends Pair {  @Override  public int hashCode() {    return 1;  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

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:

tx0.toml
[rules]
"tx0/java:hashcode-without-equals" = "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:hashcode-without-equals] reason="..."

tx0 explain tx0/java:hashcode-without-equals shows the same in the terminal. Found a false positive? Report it.