google / google/error-prone

FN: `EqualsUsingHashCode` misses equals() that extracts hashCode() into locals

Open
#5,830 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
7.2k
Forks
820
Avg merge
5h 9m
Merged PRs (30d)
50

Description

### 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

```java
// BEFORE — 1 finding
public class C {
@Override
public boolean equals(Object o) {
return hashCode() == o.hashCode();
}
}
```

```java
// 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.