hash-and-equals
Same as hash_and_equals in Dart lints
- Group
- Correctness
- Path
lint/correctness/hash-and-equals- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 242 of 512
Require hashCode and == to be overridden together.
Flags a class declaration that overrides == without also defining
hashCode, or defines hashCode without overriding ==. The
two are bound by a contract: any two objects that compare equal must return
the same hash code. Overriding only one breaks that invariant, so the type
misbehaves as a Map key or Set element — lookups miss and duplicates slip
through. Override both and derive them from the same fields.
Invalid
class A {
@override
bool operator ==(Object other) => identical(this, other);
}
class B {
@override
int get hashCode => 42;
}
class C {
@override
final int hashCode = 7;
}
class M {
@override bool operator ==(Object other) => true;
}
mixin class MC {
@override int get hashCode => 1;
}
class D {
final int value = 0;
@override bool operator ==(Object other) => other is D;
}
// A `hashCode` setter does not override `Object.hashCode`, so `==` is still unpaired.
class SetterHash {
@override bool operator ==(Object other) => true;
set hashCode(int value) {}
}
class EqualsBase {
bool operator ==(Object other) => true;
}
class InheritsEquals extends EqualsBase {
int get hashCode => 0;
}
class HashBase {
int get hashCode => 0;
}
class InheritsHash extends HashBase {
bool operator ==(Object other) => true;
} @override bool operator ==(Object other) => identical(this, other); @override int get hashCode => 42; @override final int hashCode = 7;class M { @override bool operator ==(Object other) => true;mixin class MC { @override int get hashCode => 1; final int value = 0; @override bool operator ==(Object other) => other is D;class SetterHash { @override bool operator ==(Object other) => true;class EqualsBase { bool operator ==(Object other) => true;class InheritsEquals extends EqualsBase { int get hashCode => 0;class HashBase { int get hashCode => 0;class InheritsHash extends HashBase { bool operator ==(Object other) => true;Valid
class A {
@override
bool operator ==(Object other) => identical(this, other);
@override
int get hashCode => 0;
}
class B {
void method() {}
}
mixin M {
@override
bool operator ==(Object other) => true;
@override
int get hashCode => 1;
}
class C {
final int value = 0;
int get other => 0;
}
enum E { a, b }Configure hash-and-equals
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"hash-and-equals": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/hash-and-equals: <reason>More correctness rules
- implementation-imports
implementation_importsrecommendedDisallow importing another package's private
src/libraries. - implicit-reopen
implicit_reopenFlags classes that implicitly reopen a supertype declared in the same library.
- invalid-case-patterns
invalid_case_patternsrecommendedFlags pre-3.0 switch case expressions that change meaning or break under Dart 3 patterns.
- invalid-runtime-check-with-js-interop-types
invalid_runtime_check_with_js_interop_typesrecommendedReject platform-dependent runtime checks involving JavaScript interop types.
- library-private-types-in-public-api
library_private_types_in_public_apirecommendedAvoid using library-private types in public APIs.
- list-all-equatable-fieldsrecommendedequatable
Flags declared instance fields omitted from a statically understood Equatable props list.
- literal-only-boolean-expressions
literal_only_boolean_expressionsrecommendedFlags conditions built only from literals, whose value never changes.
- match-getter-setter-field-names
Detect likely copy-paste errors in trivial accessors when a different field matching the accessor name is declared.