unnecessary-null-in-if-null-operators
Same as unnecessary_null_in_if_null_operators in Dart lints
- Group
- Suspicious
- Path
lint/suspicious/unnecessary-null-in-if-null-operators- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 465 of 512
Flags x ?? null and null ?? x, where the null operand is redundant.
The ?? operator falls back to its right operand only when the left is null,
so x ?? null always evaluates to x and null ?? x always evaluates to
x. The literal null contributes nothing and typically marks an unfinished
edit or a misunderstanding of the operator. Delete the null operand, and the
?? along with it.
Invalid
void f(int? a, int? b) {
var r1 = a ?? null;
var r2 = null ?? a;
var r3 = (a ?? b) ?? null;
var r4 = a ?? (null);
var r5 = b ?? null ?? a;
print('$r1 $r2 $r3 $r4 $r5');
}void f(int? a, int? b) { var r1 = a ?? null; var r1 = a ?? null; var r2 = null ?? a; var r2 = null ?? a; var r3 = (a ?? b) ?? null; var r3 = (a ?? b) ?? null; var r4 = a ?? (null); var r4 = a ?? (null); var r5 = b ?? null ?? a;Valid
void f(int? a, int? b, int c) {
var r1 = a ?? b;
var r2 = a ?? c;
var r3 = a ?? b ?? c;
var r4 = a == null ? b : a;
var r5 = a! + c;
print('$r1 $r2 $r3 $r4 $r5');
}Configure unnecessary-null-in-if-null-operators
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"suspicious": {
"unnecessary-null-in-if-null-operators": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/suspicious/unnecessary-null-in-if-null-operators: <reason>More suspicious rules
- use-rethrow-when-possible
use_rethrow_when_possiblerecommendedFlags
throw einside acatch (e)by matching the exception parameter's name. - avoid-accessing-other-classes-private-members
Enforces class-level encapsulation even inside one Dart library.
- avoid-assignments-as-conditions
Flags assignments evaluated inside control-flow conditions.
- avoid-banned-annotations
Ban configured dotted annotation names, without the leading
@. - avoid-banned-exports
Ban decoded export URIs, including all conditional alternatives.
- avoid-banned-names
Ban declaration names, exempting references, initializing/super formals, and
@override-annotated members and their parameters. - avoid-banned-types
Ban resolved explicit type annotations, including nested generic arguments.
- avoid-barrel-files
Discourage files containing only re-exports, without declarations or parts.