S2141: False positive when interface declares equals()

The rule S2141 triggers an issue, if an interface where equals() is declared is used in a HashSet. There is no guarantee that any implementor of the interface reimplements equals() because it is already implemented in Object. Even if the intention is to make interface implementors re-implement equals() (which is not enforcable), there is already a rule checking that equals() and hashCode() are implemented in tandem. Thus, the rule should not raise an issue.
If the interface defines a default implementation for equals(), however, then an issue is justified.
SonarJava version: 6.3

class NotOverridenEquals {

  private Set<Equality> set = new HashSet<>();
  //                          ^^^^^^^^^^^^^^^
  //                          Add a hashCode() method to Equality
}

interface Equality {

  @Override
  boolean equals(Object other);
}

Hey Oliver,

Thanks a lot for the feedback and the very useful reproducer. This is indeed a FP. I created the following ticket to handle it: SONARJAVA-3449. The ticket is currently planned for release 6.6 of the Java analyzer, to be released in around 2 weeks.

Cheers,
Michael

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.