unnecessary-null-in-if-null-operators

Same as unnecessary_null_in_if_null_operators in Dart lints

Path
lint/suspicious/unnecessary-null-in-if-null-operators
Default
On (recommended)
Origin
Dart lints
Rule
465 of 512
recommended

Flags x ?? null and null ?? x, where the null operand is redundant.

The ?? operator falls back to its right operand only when the left is null, so x ?? null always evaluates to x and null ?? x always evaluates to x. The literal null contributes nothing and typically marks an unfinished edit or a misunderstanding of the operator. Delete the null operand, and the ?? along with it.

Invalid

example.dartdart
void f(int? a, int? b) {
  var r1 = a ?? null;
  var r2 = null ?? a;
  var r3 = (a ?? b) ?? null;
  var r4 = a ?? (null);
  var r5 = b ?? null ?? a;
  print('$r1 $r2 $r3 $r4 $r5');
}
Remove the unnecessary `null` from this if-null (`??`) expression.
1void f(int? a, int? b) {
2 var r1 = a ?? null;
Remove the unnecessary `null` from this if-null (`??`) expression.
2 var r1 = a ?? null;
3 var r2 = null ?? a;
Remove the unnecessary `null` from this if-null (`??`) expression.
3 var r2 = null ?? a;
4 var r3 = (a ?? b) ?? null;
Remove the unnecessary `null` from this if-null (`??`) expression.
4 var r3 = (a ?? b) ?? null;
5 var r4 = a ?? (null);
Remove the unnecessary `null` from this if-null (`??`) expression.
5 var r4 = a ?? (null);
6 var r5 = b ?? null ?? a;

Valid

example.dartdart
void f(int? a, int? b, int c) {
  var r1 = a ?? b;
  var r2 = a ?? c;
  var r3 = a ?? b ?? c;
  var r4 = a == null ? b : a;
  var r5 = a! + c;
  print('$r1 $r2 $r3 $r4 $r5');
}

Configure unnecessary-null-in-if-null-operators

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "suspicious": {
        "unnecessary-null-in-if-null-operators": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/suspicious/unnecessary-null-in-if-null-operators: <reason>