Rules / Style / prefer-inlined-adds
prefer-inlined-adds
Flags an add cascade on a list literal whose element could be written inline.
Building a list by literal-then-cascade ([a]..add(b)) is more verbose than
placing the element directly in the literal ([a, b]), and the inline form
reads as a single collection rather than a construction followed by mutation.
The rule matches each ..add(...) section cascaded onto a list literal that
takes exactly one positional argument and no named arguments; multi-argument
or named calls, and cascades on non-list receivers, are ignored.
Invalid
example.dartdart
void bad() {
var a = [1]..add(2);
var b = <int>[]..add(1);
var c = []..add(1)..add(2);
var d = ['a']..add('b');
var e = <String>['x']..add('y');
}
Inline the added element into the collection literal instead of using 'add'.
1void bad() {
2 var a = [1]..add(2);
∙
Inline the added element into the collection literal instead of using 'add'.
2 var a = [1]..add(2);
3 var b = <int>[]..add(1);
∙
Inline the added element into the collection literal instead of using 'add'.
3 var b = <int>[]..add(1);
4 var c = []..add(1)..add(2);
∙
Inline the added element into the collection literal instead of using 'add'.
3 var b = <int>[]..add(1);
4 var c = []..add(1)..add(2);
∙
Inline the added element into the collection literal instead of using 'add'.
4 var c = []..add(1)..add(2);
5 var d = ['a']..add('b');
∙
Inline the added element into the collection literal instead of using 'add'.
5 var d = ['a']..add('b');
6 var e = <String>['x']..add('y');
∙
Valid
example.dartdart
void good() {
final items = <int>[];
var a = [1, 2, 3];
items.add(4);
var b = items..add(5);
var c = [1]..addAll([2, 3]);
var d = [1, 2]..sort();
}
How to configure
Set the severity of prefer-inlined-adds in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"style": {
"prefer-inlined-adds": "error"
}
}
}
}