Skip to main content

avoid-assignments-as-conditions

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;

// LINT: Avoid assignments as conditions. Try moving this assignment out.
if (flag ??= values.isEmpty) {}

// LINT: Avoid assignments as conditions. Try moving this assignment out.
if (flag = values.isEmpty) {}
}

✅ Good:

void fn(List<String> values) {
bool? flag;

flag ??= values.isEmpty; // Correct, assigning outside of the condition
if (flag) {
...
}
}

Additional Resources