prefer-parentheses-with-if-null
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())) {}
}