prefer-inlined-adds
Same as prefer_inlined_adds in Dart lints
- Group
- Style
- Path
lint/style/prefer-inlined-adds- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 373 of 512
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
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');
}void bad() { var a = [1]..add(2); var a = [1]..add(2); var b = <int>[]..add(1); var b = <int>[]..add(1); var c = []..add(1)..add(2); var b = <int>[]..add(1); var c = []..add(1)..add(2); var c = []..add(1)..add(2); var d = ['a']..add('b'); var d = ['a']..add('b'); var e = <String>['x']..add('y');Valid
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();
}Configure prefer-inlined-adds
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"prefer-inlined-adds": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/prefer-inlined-adds: <reason>More style rules
- prefer-int-literals
prefer_int_literalsFlags a whole-number double literal, such as
1.0, where the context type isdoubleso the int literal1means the same value. - prefer-interpolation-to-compose-strings
prefer_interpolation_to_compose_stringsrecommendedPrefers string interpolation to composing strings with
+. - prefer-is-empty
prefer_is_emptyrecommendedFlags
.lengthcomparisons equivalent to a.isEmptycheck. - prefer-is-not-empty
prefer_is_not_emptyrecommendedFlags
.lengthcomparisons equivalent to a.isNotEmptycheck. - prefer-is-not-operator
prefer_is_not_operatorrecommendedFlags a negated type test
!(x is T)that should use theis!operator. - prefer-iterable-of
prefer-iterable-ofrecommendedFlags
.fromcollection constructors in favor of the.ofconstructor. - prefer-iterable-where-type
prefer_iterable_whereTyperecommendedFlags
.where((e) => e is T), which.whereType<T>()expresses directly. - prefer-last
prefer-lastFlags manual last-element indexing in favor of the
.lastgetter.