hash-and-equals

Same as hash_and_equals in Dart lints

Path
lint/correctness/hash-and-equals
Default
On (recommended)
Origin
Dart lints
Rule
242 of 512
recommended

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

example.dartdart
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 `hashCode` if you override the `==` operator.
2 @override
3 bool operator ==(Object other) => identical(this, other);
Override the `==` operator if you override `hashCode`.
7 @override
8 int get hashCode => 42;
Override the `==` operator if you override `hashCode`.
12 @override
13 final int hashCode = 7;
Override `hashCode` if you override the `==` operator.
16class M {
17 @override bool operator ==(Object other) => true;
Override the `==` operator if you override `hashCode`.
20mixin class MC {
21 @override int get hashCode => 1;
Override `hashCode` if you override the `==` operator.
25 final int value = 0;
26 @override bool operator ==(Object other) => other is D;
Override `hashCode` if you override the `==` operator.
30class SetterHash {
31 @override bool operator ==(Object other) => true;
Override `hashCode` if you override the `==` operator.
35class EqualsBase {
36 bool operator ==(Object other) => true;
Override the `==` operator if you override `hashCode`.
39class InheritsEquals extends EqualsBase {
40 int get hashCode => 0;
Override the `==` operator if you override `hashCode`.
43class HashBase {
44 int get hashCode => 0;
Override `hashCode` if you override the `==` operator.
47class InheritsHash extends HashBase {
48 bool operator ==(Object other) => true;

Valid

example.dartdart
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.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "hash-and-equals": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/correctness/hash-and-equals: <reason>