Skip to main content

prefer-parentheses-with-if-null

added in: 1.7.0

Warns when an if null (??) has a binary expression without parentheses.

Not adding parentheses may lead to unexpected execution order.

Example

❌ Bad:

void main() {
final bool? nullableValue = false;
if (nullableValue ?? false || someOtherCondition()) {} // LINT, `someOtherCondition()` won't be executed

final bool? nullableValue = true;
if (nullableValue ?? false && someOtherCondition()) {} // LINT, `false && someOtherCondition()` won't be executed
}

✅ Good:

void main() {
final bool? nullableValue = false;
if ((nullableValue ?? false) || someOtherCondition()) {}
if (nullableValue ?? (false || someOtherCondition())) {}

final bool? nullableValue = true;
if ((nullableValue ?? false) && someOtherCondition()) {}
if (nullableValue ?? (false && someOtherCondition())) {}
}