Error Prone version
Error Prone 2.49.0 (javac plugin) — javac 21.0.8, source/target 21.
Check name
EqualsUsingHashCode — https://errorprone.info/bugpattern/EqualsUsingHashCode
Description
EqualsUsingHashCode flags equals() implementations that decide equality by comparing hash codes (fragile, since hashes collide). The matcher only recognizes the direct shape hashCode() == o.hashCode(). Extracting the two hashCode() calls into locals before the == comparison is a runtime-equivalent refactor, but the rule no longer fires.
Reproducer
// BEFORE — 1 finding
public class C {
@Override
public boolean equals(Object o) {
return hashCode() == o.hashCode();
}
}
// AFTER — equivalent rewrite, 0 findings
public class C {
@Override
public boolean equals(Object o) {
int a = hashCode(); int b = o.hashCode(); return a == b;
}
}
Expected behavior
EqualsUsingHashCode should report the same finding on BEFORE and AFTER. The AFTER equals() still decides equality solely by comparing the two hash codes.
Actual behavior
- BEFORE: 1 finding (
Implementing #equals by just comparing hashCodes is fragile ...).
- AFTER: 0 findings.
The matcher only recognizes the direct o.hashCode() == hashCode() comparison and misses the trivially-equivalent extracted-local form.
Error Prone version
Error Prone 2.49.0 (javac plugin) — javac 21.0.8, source/target 21.
Check name
EqualsUsingHashCode— https://errorprone.info/bugpattern/EqualsUsingHashCodeDescription
EqualsUsingHashCodeflagsequals()implementations that decide equality by comparing hash codes (fragile, since hashes collide). The matcher only recognizes the direct shapehashCode() == o.hashCode(). Extracting the twohashCode()calls into locals before the==comparison is a runtime-equivalent refactor, but the rule no longer fires.Reproducer
Expected behavior
EqualsUsingHashCodeshould report the same finding on BEFORE and AFTER. The AFTERequals()still decides equality solely by comparing the two hash codes.Actual behavior
Implementing #equals by just comparing hashCodes is fragile ...).The matcher only recognizes the direct
o.hashCode() == hashCode()comparison and misses the trivially-equivalent extracted-local form.