avoid-assignments-as-conditions
- Group
- Suspicious
- 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
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,
};
} var count = 0; if (flag = input) {} if (flag = input) {} while (flag = input) { break; } while (flag = input) { break; } do { count++; } while (flag = input); do { count++; } while (flag = input); for (; flag = input;) { break; } for (; flag = input;) { break; } final answer = (flag = input) ? 1 : 2; final answer = (flag = input) ? 1 : 2; final list = [if (flag = input) answer]; final list = [if (flag = input) answer]; final set = {if (flag = input) answer}; final set = {if (flag = input) answer}; final map = {if (flag = input) 'answer': answer}; final map = {if (flag = input) 'answer': answer}; if ((nullable ??= input) && other) {} if ((nullable ??= input) && other) {} if (identity(flag = other)) {} if (identity(flag = other)) {} if ((count += 1) > 2) {} if ((count += 1) > 2) {} final generated = [for (; flag = input;) answer]; final generated = [for (; flag = input;) answer]; final keyed = {for (; flag = input;) 'answer': answer}; final keyed = {for (; flag = input;) 'answer': answer}; if (input ? (flag = other) : false) {} if (input ? (flag = other) : false) {} if ((box..value = input).value) {} final values = [false]; if ((values..[0] = input).first) {} if (() { if (flag = input) {} }()) {} if (input case true when (flag = other)) {} switch (input) { case true when (flag = other): final switched = switch (input) { true when (flag = other) => 1,Valid
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.
{
"linter": {
"rules": {
"suspicious": {
"avoid-assignments-as-conditions": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/suspicious/avoid-assignments-as-conditions: <reason>More suspicious rules
- avoid-banned-annotations
Ban configured dotted annotation names, without the leading
@. - avoid-banned-exports
Ban decoded export URIs, including all conditional alternatives.
- avoid-banned-names
Ban declaration names, exempting references, initializing/super formals, and
@override-annotated members and their parameters. - avoid-banned-types
Ban resolved explicit type annotations, including nested generic arguments.
- avoid-barrel-files
Discourage files containing only re-exports, without declarations or parts.
- avoid-bitwise-operators-with-booleans
Flags
&and|applied to two non-nullable boolean operands. - avoid-commented-out-code
Flags ordinary comments that contain recognizable Dart statements.
- avoid-double-slash-imports
Reject repeated path separators in directive URIs (import, export and part).