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
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
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;
} bool operator ==(Object? other) => other != null && bool operator ==(Object? other) { if (null == other) return false; bool operator ==(Object? other) { final value = other ?? this; bool operator ==(Object? other) => other?.toString() == label;Valid
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.
{
"linter": {
"rules": {
"style": {
"avoid-null-checks-in-equality-operators": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/avoid-null-checks-in-equality-operators: <reason>More style rules
- avoid-positional-boolean-parameters
avoid_positional_boolean_parametersFlags a positional
boolparameter in a public signature. - avoid-positional-fields-in-records
avoid_positional_fields_in_recordsFlags positional fields in record types.
- avoid-private-typedef-functions
avoid_private_typedef_functionsFlags a private function typedef that is used at most once.
- avoid-public-notifier-propertiesriverpod
Keeps notifier state behind Riverpod's observable state property.
- avoid-redundant-argument-values
avoid_redundant_argument_valuesFlags arguments that pass exactly the parameter's default value.
- avoid-redundant-else
Flags else branches after an if body that unconditionally returns or throws.
- avoid-redundant-pattern-field-names
avoid_redundant_pattern_field_namesrecommendedFlags redundant field names in object and record patterns.
- avoid-renaming-method-parameters
avoid_renaming_method_parametersRequires overriding methods to keep the positional parameter names of the member they inherit.