Skip to main content

prefer-parentheses-with-if-null

added in: 1.7.0
🛠
Pro+
preset: recommended

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;
// LINT: Prefer adding parentheses to ensure the right precedence.
if (nullableValue ?? false || someOtherCondition()) {} // `someOtherCondition()` won't be executed

final bool? nullableValue = true;
// LINT: Prefer adding parentheses to ensure the right precedence.
if (nullableValue ?? false && someOtherCondition()) {} // `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())) {}
}