avoid-assignments-as-conditions
preset: recommended
Warns when an assignment is used inside a condition.
Assigning to a variable inside a condition can be confusing or indicate an incorrect operator (=
instead of ==
). Consider replacing it with ==
or moving the assignment out of the condition to clearly indicate that this is intentional.
Example
❌ Bad:
void fn(List<String> values) {
bool? flag;
if (flag ??= values.isEmpty) {} // LINT: Avoid assignments as conditions. Try moving this assignment out.
if (flag = values.isEmpty) {} // LINT: Avoid assignments as conditions. Try moving this assignment out.
}
✅ Good:
void fn(List<String> values) {
bool? flag;
flag ??= values.isEmpty; // Correct, assigning outside of the condition
if (flag) {
...
}
}