Rules / Performance / unnecessary-to-list-in-spreads
unnecessary-to-list-in-spreads
Flags a .toList() call on the operand of a spread, e.g. ...x.toList().
A spread element accepts any iterable, so converting to a list first
allocates a throwaway List for no reason — spread the iterable directly.
The rule matches a no-argument .toList() call spread into a list, set, or
map literal, including null-aware spreads (...?x.toList()).
Invalid
example.dartdart
void bad() {
var a = [...items.toList()];
var b = [1, ...more.toList()];
var c = <int>{...values.toList()};
var d = [...?maybe.toList()];
var e = [...first.toList(), ...second.toList()];
}
Unnecessary 'toList()' in a spread.
1void bad() {
2 var a = [...items.toList()];
∙
Unnecessary 'toList()' in a spread.
2 var a = [...items.toList()];
3 var b = [1, ...more.toList()];
∙
Unnecessary 'toList()' in a spread.
3 var b = [1, ...more.toList()];
4 var c = <int>{...values.toList()};
∙
Unnecessary 'toList()' in a spread.
4 var c = <int>{...values.toList()};
5 var d = [...?maybe.toList()];
∙
Unnecessary 'toList()' in a spread.
5 var d = [...?maybe.toList()];
6 var e = [...first.toList(), ...second.toList()];
∙
Unnecessary 'toList()' in a spread.
5 var d = [...?maybe.toList()];
6 var e = [...first.toList(), ...second.toList()];
∙
Valid
example.dartdart
void good() {
var a = [...items];
var b = [1, ...more];
var c = <int>{...values};
var d = [...?maybe];
var e = [...items.map((e) => e * 2)];
var f = [items.toList()];
}
How to configure
Set the severity of unnecessary-to-list-in-spreads in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"performance": {
"unnecessary-to-list-in-spreads": "error"
}
}
}
}