prefer-conditional-assignment
Same as prefer_conditional_assignment in Dart lints
- Group
- Complexity
- Path
lint/complexity/prefer-conditional-assignment- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 324 of 512
Flags if (x == null) { x = y; } with no else branch.
A null-guarded assignment like this is exactly what the ??= compound
operator expresses: x ??= y. The shorter form removes a branch and states
the intent — assign only when currently null — directly. The rule matches an
if with no else whose condition is an == null check and whose single
then-statement assigns to the same canonical identifier/property chain.
Indexed targets are not identifiers; inherited accessor halves must belong
to the same declaring variable.
Invalid
void f(int? a, C obj) {
if (a == null) { a = 0; }
if (a == null) a = 1;
if (null == a) { a = 2; }
if (obj.field == null) { obj.field = 3; }
if (obj.field == null) obj.field = 4;
print('$a ${obj.field}');
}
class C {
int? field;
}
class StateBase {
int? get state => null;
set state(int? value) {}
}
class InheritedState extends StateBase {}
void pairedAccessors(InheritedState notifier, List<InheritedState> notifiers, int? value) {
if (notifier.state == null) notifier.state = 1;
if (notifiers.first.state == null) notifiers.first.state = 1;
if (value == null) { { value = 1; } }
if (value == null) value ??= 1;
}
void unresolvedMembers(dynamic receiver) {
// Unresolved dynamic properties have the same null canonical element in Dart.
if (receiver.first == null) receiver.second = 1;
}void f(int? a, C obj) { if (a == null) { a = 0; } if (a == null) { a = 0; } if (a == null) a = 1; if (a == null) a = 1; if (null == a) { a = 2; } if (null == a) { a = 2; } if (obj.field == null) { obj.field = 3; } if (obj.field == null) { obj.field = 3; } if (obj.field == null) obj.field = 4;void pairedAccessors(InheritedState notifier, List<InheritedState> notifiers, int? value) { if (notifier.state == null) notifier.state = 1; if (notifiers.first.state == null) notifiers.first.state = 1; if (value == null) { { value = 1; } } if (value == null) { { value = 1; } } if (value == null) value ??= 1; // Unresolved dynamic properties have the same null canonical element in Dart. if (receiver.first == null) receiver.second = 1;Valid
void f(int? a, int? b, C obj) {
if (a == null) {
a = 0;
} else {
a = 1;
}
if (a == null) {
b = 2;
}
if (a != null) {
a = 3;
}
if (a == null) {
print(a);
}
if (a == null) {
a = 0;
b = 1;
}
a ??= 5;
print('$a $b ${obj.field}');
}
class C {
int? field;
}
class StateBase {
int? get state => null;
set state(int? value) {}
}
class ReadOverride extends StateBase {
@override
int? get state => super.state;
}
void distinctTargets(
ReadOverride notifier,
StateBase first,
StateBase second,
List<int?> values,
Map<String, int?> entries,
) {
if (notifier.state == null) notifier.state = 1;
if (first.state == null) second.state = 1;
if (values[0] == null) values[0] = 1;
if (entries['key'] == null) entries['key'] = 1;
}
void differentAccessSyntax(StateBase notifier) {
if (notifier.state == null) (notifier).state = 1;
if (notifier.state == null) (notifier.state = 1);
}Configure prefer-conditional-assignment
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"complexity": {
"prefer-conditional-assignment": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/complexity/prefer-conditional-assignment: <reason>More complexity rules
- prefer-conditional-expressions
prefer-conditional-expressionsFlags an
if/elsewhose branches each reduce to a single return or a single assignment to the same target. - prefer-expression-function-bodies
prefer_expression_function_bodiesFlags block function bodies that consist of a single
returnof a value. - prefer-extracting-callbacks
prefer-extracting-callbacksflutterFlags inline block-body callbacks passed to Widget constructors.
- prefer-for-elements-to-map-from-iterable
prefer_for_elements_to_map_fromIterablerecommendedFlags
Map.fromIterable(x, key: ..., value: ...). - prefer-if-elements-to-conditional-expressions
prefer_if_elements_to_conditional_expressionsPrefers collection
ifelements to conditional expressions in list and set literals. - prefer-if-null-operators
prefer_if_null_operatorsrecommendedFlags
x == null ? y : x(and the invertedx != null ? x : y). - 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.