prefer-overriding-parent-equality
preset: recommended
Warns when a parent class implements hashCode
and ==
, but the child class does not implement them.
Not providing an implementation in the child class can lead to tricky bugs where equality checks don't work as expected.
Example
❌ Bad:
class SomeClass {
int get hashCode => 1;
bool operator ==(Object other) {
return super == other;
}
}
// LINT: Parent class has a custom '==' implementation. Try providing a separate implementation for this class to avoid potential mistakes.
class AnotherClass extends SomeClass {
final String value;
AnotherClass(this.value);
}
✅ Good:
class SomeClass {
int get hashCode => 1;
bool operator ==(Object other) {
return super == other;
}
}
class CorrectClass extends SomeClass {
final String value;
CorrectClass(this.value);
int get hashCode => Object.hash(this);
bool operator ==(Object other) {
return other is CorrectClass && value == other.value;
}
}