avoid-nested-assignments

Path
lint/suspicious/avoid-nested-assignments
Default
Off, opt in
Origin
Falcon
Rule
97 of 512

Require assignments to be standalone statements, arrow bodies or loop operations. ignore-null-aware-assignments (default false) exempts ??=. Conditions and chained assignments are handled by their dedicated rules.

Invalid

example.dartdart
void f(dynamic x, dynamic y, dynamic z) {
  print(x = 1);
  final value = (x = 2);
  returnValue(x += 3);
  final message = '${x = 4}';
  print(z ??= 6);
}
Move this assignment out of the surrounding expression.
1void f(dynamic x, dynamic y, dynamic z) {
2 print(x = 1);
Move this assignment out of the surrounding expression.
2 print(x = 1);
3 final value = (x = 2);
Move this assignment out of the surrounding expression.
3 final value = (x = 2);
4 returnValue(x += 3);
Move this assignment out of the surrounding expression.
4 returnValue(x += 3);
5 final message = '${x = 4}';
Move this assignment out of the surrounding expression.
5 final message = '${x = 4}';
6 print(z ??= 6);

Valid

example.dartdart
void f(dynamic x) {
  x = 1;
  x += 2;
  (x = 3);
  for (x = 0; x < 5; x = x + 1) {}
  final closure = () { x = 4; };
  x ??= 6;
  final arrow = () => x = 7;
  final lazy = () => x ??= 8;
  x = (x = 9);
  if ((x = true)) {}
  while ((x = readLine()) != null) {}
  final owner = Owner();
  owner..value = x = 10;
  consume(x = x = 11);
}

class Owner {
  dynamic value;
  set name(String input) => value = input;
  dynamic get cached => value ??= Owner();
}

Configure avoid-nested-assignments

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "suspicious": {
        "avoid-nested-assignments": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/suspicious/avoid-nested-assignments: <reason>