avoid-null-checks-in-equality-operators

Same as avoid_null_checks_in_equality_operators in Dart lints

Group
Style
Path
lint/style/avoid-null-checks-in-equality-operators
Default
On (recommended)
Origin
Dart lints
Rule
107 of 512
recommended

Flags null checks of the parameter inside a custom operator ==.

Dart never calls a user-defined == with null: a == null is answered by the language before the operator runs. Declaring the parameter as nullable (Object? other) and then comparing it with null, applying ??, or using ?. on it is dead code.

class Person {
  final String? name;
  @override
  bool operator ==(Object? other) =>
      other != null && other is Person && name == other.name; // flagged
}

Only operators whose single parameter is explicitly declared nullable are inspected; dynamic is excluded, matching the analyzer's nullability suffix check. An operator body that rebinds the parameter name is skipped.

Invalid

example.dartdart
class Person {
  final String? name;
  Person(this.name);

  @override
  bool operator ==(Object? other) =>
      other != null &&
      other is Person &&
      name == other.name;

  @override
  int get hashCode => name.hashCode;
}

class Pet {
  final String? name;
  Pet(this.name);

  @override
  bool operator ==(Object? other) {
    if (null == other) return false;
    return other is Pet && name == other.name;
  }

  @override
  int get hashCode => name.hashCode;
}


class Box {
  final Object? content;
  Box(this.content);

  @override
  bool operator ==(Object? other) {
    final value = other ?? this;
    return value is Box && value.content == content;
  }

  @override
  int get hashCode => content.hashCode;
}

class Tag {
  final String label;
  Tag(this.label);

  @override
  bool operator ==(Object? other) =>
      other?.toString() == label;

  @override
  int get hashCode => label.hashCode;
}
Remove the unnecessary null comparison from this `==` implementation.
6 bool operator ==(Object? other) =>
7 other != null &&
Remove the unnecessary null comparison from this `==` implementation.
20 bool operator ==(Object? other) {
21 if (null == other) return false;
Remove the unnecessary null comparison from this `==` implementation.
35 bool operator ==(Object? other) {
36 final value = other ?? this;
Remove the unnecessary null comparison from this `==` implementation.
49 bool operator ==(Object? other) =>
50 other?.toString() == label;

Valid

example.dartdart
class Person {
  final String? name;
  Person(this.name);

  @override
  bool operator ==(Object other) => other is Person && name == other.name;

  @override
  int get hashCode => name.hashCode;
}

// Comparing a field with null is fine.
class Pet {
  final String? name;
  Pet(this.name);

  @override
  bool operator ==(Object other) => other is Pet && name != null && name == other.name;

  @override
  int get hashCode => name.hashCode;
}

// A rebound name refers to something else.
class Wrapper {
  final Object? value;
  Wrapper(this.value);

  @override
  bool operator ==(Object? other) {
    if (other is! Wrapper) return false;
    final inner = other.value;
    return [inner].any((other) => other != null);
  }

  @override
  int get hashCode => value.hashCode;
}

// Non-nullable parameter: `other` can never be null there anyway.
class Cell {
  final int? v;
  Cell(this.v);

  @override
  bool operator ==(Object other) => other is Cell && (v ?? 0) == (other.v ?? 0);

  @override
  int get hashCode => v.hashCode;
}

// Null checks in other operators and methods are not reported.
class Num {
  final int n;
  Num(this.n);

  bool equals(Object? other) => other != null && other is Num && other.n == n;

  Num operator +(Num? other) => Num(n + (other?.n ?? 0));
}
class Car {
  final String? model;
  Car(this.model);

  @override
  bool operator ==(dynamic other) =>
      other?.model == model;

  @override
  int get hashCode => model.hashCode;
}

Configure avoid-null-checks-in-equality-operators

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "avoid-null-checks-in-equality-operators": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/avoid-null-checks-in-equality-operators: <reason>