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);
}