avoid-assignments-as-conditions

Path
lint/suspicious/avoid-assignments-as-conditions
Default
Off, opt in
Origin
Falcon
Rule
16 of 512

Flags assignments evaluated inside control-flow conditions.

Assignments can hide a mistyped == or an unexpected state change. Move the assignment before the condition, or use a comparison. This checks all assignment operators in if, while, do/while, traditional for, ternary, and collection conditions, including nested expressions and if case or switch guards. Loop initializers, updates, and branches are excluded unless nested within a surrounding condition. Closure bodies never inherit an outer condition, but conditions inside them are checked. Increment/decrement operators and assert arguments are outside this rule's scope.

Invalid

example.dartdart
class Box {
  bool value = false;
}

bool identity(bool value) => value;

void conditions(bool input, bool other, bool? nullable, Box box) {
  var flag = false;
  var count = 0;
  if (flag = input) {}
  while (flag = input) { break; }
  do { count++; } while (flag = input);
  for (; flag = input;) { break; }
  final answer = (flag = input) ? 1 : 2;
  final list = [if (flag = input) answer];
  final set = {if (flag = input) answer};
  final map = {if (flag = input) 'answer': answer};
  if ((nullable ??= input) && other) {}
  if (identity(flag = other)) {}
  if ((count += 1) > 2) {}
  final generated = [for (; flag = input;) answer];
  final keyed = {for (; flag = input;) 'answer': answer};
  if (input ? (flag = other) : false) {}
  if ((box..value = input).value) {}
  final values = [false];
  if ((values..[0] = input).first) {}
  if (() {
    if (flag = input) {}
    return true;
  }()) {}
  if (input case true when (flag = other)) {}
  switch (input) {
    case true when (flag = other):
      break;
    default:
      break;
  }
  final switched = switch (input) {
    true when (flag = other) => 1,
    _ => 0,
  };
}
Avoid assignments inside conditions; move the assignment out or use a comparison.
9 var count = 0;
10 if (flag = input) {}
Avoid assignments inside conditions; move the assignment out or use a comparison.
10 if (flag = input) {}
11 while (flag = input) { break; }
Avoid assignments inside conditions; move the assignment out or use a comparison.
11 while (flag = input) { break; }
12 do { count++; } while (flag = input);
Avoid assignments inside conditions; move the assignment out or use a comparison.
12 do { count++; } while (flag = input);
13 for (; flag = input;) { break; }
Avoid assignments inside conditions; move the assignment out or use a comparison.
13 for (; flag = input;) { break; }
14 final answer = (flag = input) ? 1 : 2;
Avoid assignments inside conditions; move the assignment out or use a comparison.
14 final answer = (flag = input) ? 1 : 2;
15 final list = [if (flag = input) answer];
Avoid assignments inside conditions; move the assignment out or use a comparison.
15 final list = [if (flag = input) answer];
16 final set = {if (flag = input) answer};
Avoid assignments inside conditions; move the assignment out or use a comparison.
16 final set = {if (flag = input) answer};
17 final map = {if (flag = input) 'answer': answer};
Avoid assignments inside conditions; move the assignment out or use a comparison.
17 final map = {if (flag = input) 'answer': answer};
18 if ((nullable ??= input) && other) {}
Avoid assignments inside conditions; move the assignment out or use a comparison.
18 if ((nullable ??= input) && other) {}
19 if (identity(flag = other)) {}
Avoid assignments inside conditions; move the assignment out or use a comparison.
19 if (identity(flag = other)) {}
20 if ((count += 1) > 2) {}
Avoid assignments inside conditions; move the assignment out or use a comparison.
20 if ((count += 1) > 2) {}
21 final generated = [for (; flag = input;) answer];
Avoid assignments inside conditions; move the assignment out or use a comparison.
21 final generated = [for (; flag = input;) answer];
22 final keyed = {for (; flag = input;) 'answer': answer};
Avoid assignments inside conditions; move the assignment out or use a comparison.
22 final keyed = {for (; flag = input;) 'answer': answer};
23 if (input ? (flag = other) : false) {}
Avoid assignments inside conditions; move the assignment out or use a comparison.
23 if (input ? (flag = other) : false) {}
24 if ((box..value = input).value) {}
Avoid assignments inside conditions; move the assignment out or use a comparison.
25 final values = [false];
26 if ((values..[0] = input).first) {}
Avoid assignments inside conditions; move the assignment out or use a comparison.
27 if (() {
28 if (flag = input) {}
Avoid assignments inside conditions; move the assignment out or use a comparison.
30 }()) {}
31 if (input case true when (flag = other)) {}
Avoid assignments inside conditions; move the assignment out or use a comparison.
32 switch (input) {
33 case true when (flag = other):
Avoid assignments inside conditions; move the assignment out or use a comparison.
38 final switched = switch (input) {
39 true when (flag = other) => 1,

Valid

example.dartdart
bool invoke(bool Function() callback) => callback();

void conditions(bool input, bool other, bool? nullable) {
  var flag = false;
  var count = 0;
  flag = input;
  if (flag == other) { flag = other; }
  while (flag) { flag = false; }
  do { flag = input; } while (flag);
  for (flag = input; flag; flag = false) { count++; }
  final branch = input ? (flag = other) : false;
  final list = [if (input) flag = other];
  final map = {if (input) 'value': flag = other};
  final generated = [for (flag = input; flag; flag = false) count];
  final keyed = {for (flag = input; flag; flag = false) 'value': count};
  if (() { flag = input; return flag; }()) {}
  while ((() => flag = input)()) { break; }
  if (invoke(() => flag = input)) {}
  final ternary = (() => flag = input)() ? 1 : 2;
  final closures = [if ((() => flag = input)()) 1];
  if ((++count) > 2) {}
  assert(flag = input);
  nullable ??= other;
  switch (input) {
    case true when (() => flag = other)():
      flag = false;
      break;
    default:
      break;
  }
  final switched = switch (input) {
    true when input => flag = other,
    _ => false,
  };
}

Configure avoid-assignments-as-conditions

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

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

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

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