Skip to content

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

Description

@Chordrain

Error Prone version

Error Prone 2.49.0 (javac plugin) — javac 21.0.8, source/target 21.

Check name

EqualsUsingHashCodehttps://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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions