prefer-if-null-operators
Same as prefer_if_null_operators in Dart lints
- Group
- Complexity
- Path
lint/complexity/prefer-if-null-operators- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 367 of 512
Flags x == null ? y : x (and the inverted x != null ? x : y).
A ternary that returns its own operand on the non-null branch is exactly the
?? if-null operator: x ?? y. The operator is shorter and evaluates x
only once. The rule takes the operand of the == null/!= null check and
compares it (ignoring whitespace) against the branch that falls through to
it, firing when the two are textually equal.
Invalid
void f(int? a, int? b, String fallback, C obj) {
var r1 = a == null ? b : a;
var r2 = a != null ? a : b;
var r3 = null == a ? b : a;
var r4 = a == null ? 0 : a;
var r5 = obj.name == null ? fallback : obj.name;
print('$r1 $r2 $r3 $r4 $r5');
}
class C {
String? name;
}void f(int? a, int? b, String fallback, C obj) { var r1 = a == null ? b : a; var r1 = a == null ? b : a; var r2 = a != null ? a : b; var r2 = a != null ? a : b; var r3 = null == a ? b : a; var r3 = null == a ? b : a; var r4 = a == null ? 0 : a; var r4 = a == null ? 0 : a; var r5 = obj.name == null ? fallback : obj.name;Valid
void f(int? a, int? b, C obj) {
var r1 = a == null ? a : b;
var r2 = a == null ? b : b;
var r3 = a != null ? b : a;
var r4 = a! > 0 ? a : b;
var r5 = obj.name == null ? a : obj.other;
var r6 = a ?? b;
print('$r1 $r2 $r3 $r4 $r5 $r6');
}
class C {
int? name;
int? other;
}Configure prefer-if-null-operators
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"complexity": {
"prefer-if-null-operators": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/complexity/prefer-if-null-operators: <reason>More complexity rules
- prefer-immediate-return
prefer-immediate-returnFlags a local variable that is returned on the immediately following statement.
- prefer-iterable-any
prefer_iterable_anyrecommendedFlags
iterable.where(...).isNotEmpty. - prefer-iterable-every
prefer_iterable_everyrecommendedFlags
iterable.where(predicate).isEmptyand filtered-length comparisons. - prefer-moving-to-variable
prefer-moving-to-variableFlags repeated property/getter chains in local variable initializers.
- prefer-null-aware-operators
prefer_null_aware_operatorsrecommendedFlags
x == null ? null : x.y(and the invertedx != null ? x.y : null). - unnecessary-const
unnecessary_constrecommendedFlags a redundant
constkeyword inside an already-const context. - unnecessary-getters-setters
unnecessary_getters_settersrecommendedFlags a trivial getter/setter pair that only reads and writes a private field.
- unnecessary-lambdas
unnecessary_lambdasrecommendedFlags a closure that only forwards its parameters to a function a tear-off could name.