prefer-conditional-assignment

Same as prefer_conditional_assignment in Dart lints

Path
lint/complexity/prefer-conditional-assignment
Default
On (recommended)
Origin
Dart lints
Rule
324 of 512
recommended

Flags if (x == null) { x = y; } with no else branch.

A null-guarded assignment like this is exactly what the ??= compound operator expresses: x ??= y. The shorter form removes a branch and states the intent — assign only when currently null — directly. The rule matches an if with no else whose condition is an == null check and whose single then-statement assigns to the same canonical identifier/property chain. Indexed targets are not identifiers; inherited accessor halves must belong to the same declaring variable.

Invalid

example.dartdart
void f(int? a, C obj) {
  if (a == null) { a = 0; }
  if (a == null) a = 1;
  if (null == a) { a = 2; }
  if (obj.field == null) { obj.field = 3; }
  if (obj.field == null) obj.field = 4;
  print('$a ${obj.field}');
}

class C {
  int? field;
}

class StateBase {
  int? get state => null;
  set state(int? value) {}
}

class InheritedState extends StateBase {}

void pairedAccessors(InheritedState notifier, List<InheritedState> notifiers, int? value) {
  if (notifier.state == null) notifier.state = 1;
  if (notifiers.first.state == null) notifiers.first.state = 1;
  if (value == null) { { value = 1; } }
  if (value == null) value ??= 1;
}

void unresolvedMembers(dynamic receiver) {
  // Unresolved dynamic properties have the same null canonical element in Dart.
  if (receiver.first == null) receiver.second = 1;
}
Prefer using a conditional assignment `??=`.
1void f(int? a, C obj) {
2 if (a == null) { a = 0; }
Prefer using a conditional assignment `??=`.
2 if (a == null) { a = 0; }
3 if (a == null) a = 1;
Prefer using a conditional assignment `??=`.
3 if (a == null) a = 1;
4 if (null == a) { a = 2; }
Prefer using a conditional assignment `??=`.
4 if (null == a) { a = 2; }
5 if (obj.field == null) { obj.field = 3; }
Prefer using a conditional assignment `??=`.
5 if (obj.field == null) { obj.field = 3; }
6 if (obj.field == null) obj.field = 4;
Prefer using a conditional assignment `??=`.
21void pairedAccessors(InheritedState notifier, List<InheritedState> notifiers, int? value) {
22 if (notifier.state == null) notifier.state = 1;
Prefer using a conditional assignment `??=`.
23 if (notifiers.first.state == null) notifiers.first.state = 1;
24 if (value == null) { { value = 1; } }
Prefer using a conditional assignment `??=`.
24 if (value == null) { { value = 1; } }
25 if (value == null) value ??= 1;
Prefer using a conditional assignment `??=`.
29 // Unresolved dynamic properties have the same null canonical element in Dart.
30 if (receiver.first == null) receiver.second = 1;

Valid

example.dartdart
void f(int? a, int? b, C obj) {
  if (a == null) {
    a = 0;
  } else {
    a = 1;
  }
  if (a == null) {
    b = 2;
  }
  if (a != null) {
    a = 3;
  }
  if (a == null) {
    print(a);
  }
  if (a == null) {
    a = 0;
    b = 1;
  }
  a ??= 5;
  print('$a $b ${obj.field}');
}

class C {
  int? field;
}

class StateBase {
  int? get state => null;
  set state(int? value) {}
}

class ReadOverride extends StateBase {
  @override
  int? get state => super.state;
}

void distinctTargets(
  ReadOverride notifier,
  StateBase first,
  StateBase second,
  List<int?> values,
  Map<String, int?> entries,
) {
  if (notifier.state == null) notifier.state = 1;
  if (first.state == null) second.state = 1;
  if (values[0] == null) values[0] = 1;
  if (entries['key'] == null) entries['key'] = 1;
}

void differentAccessSyntax(StateBase notifier) {
  if (notifier.state == null) (notifier).state = 1;
  if (notifier.state == null) (notifier.state = 1);
}

Configure prefer-conditional-assignment

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "complexity": {
        "prefer-conditional-assignment": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/complexity/prefer-conditional-assignment: <reason>