unnecessary-null-aware-assignments
Same as unnecessary_null_aware_assignments in Dart lints
- Group
- Suspicious
- Path
lint/suspicious/unnecessary-null-aware-assignments- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 462 of 512
Flags x ??= null, which can never have an effect.
The ??= operator assigns only when the target is currently null, so
assigning null is a guaranteed no-op: a null target stays null, and
otherwise nothing happens. Such a statement is dead code that usually points
to a mistaken right-hand side — the author likely meant a real default value.
Remove the statement, or supply the value that was intended.
Invalid
void f(int? a, C obj, Map<String, int> m) {
a ??= null;
obj.field ??= null;
m['k'] ??= null;
_top ??= null;
obj.nested.value ??= null;
print('$a ${obj.field} $m');
}
int? _top;
class C {
int? field;
int? value;
C get nested => this;
}void f(int? a, C obj, Map<String, int> m) { a ??= null; a ??= null; obj.field ??= null; obj.field ??= null; m['k'] ??= null; m['k'] ??= null; _top ??= null; _top ??= null; obj.nested.value ??= null;Valid
void f(int? a, C obj) {
a ??= 0;
a ??= compute();
a = null;
obj.field ??= 5;
a = (a ?? 0) + 1;
print('$a ${obj.field}');
}
int compute() => 0;
class C {
int? field;
}Configure unnecessary-null-aware-assignments
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"suspicious": {
"unnecessary-null-aware-assignments": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/suspicious/unnecessary-null-aware-assignments: <reason>More suspicious rules
- unnecessary-null-in-if-null-operators
unnecessary_null_in_if_null_operatorsrecommendedFlags
x ?? nullandnull ?? x, where thenulloperand is redundant. - 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.