unnecessary-to-list-in-spreads

Same as unnecessary_to_list_in_spreads in Dart lints

Path
lint/performance/unnecessary-to-list-in-spreads
Default
On (recommended)
Origin
Dart lints
Rule
476 of 512
recommended

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()];
}
Remove the unnecessary `toList()` call from this spread.
1void bad() {
2 var a = [...items.toList()];
Remove the unnecessary `toList()` call from this spread.
2 var a = [...items.toList()];
3 var b = [1, ...more.toList()];
Remove the unnecessary `toList()` call from this spread.
3 var b = [1, ...more.toList()];
4 var c = <int>{...values.toList()};
Remove the unnecessary `toList()` call from this spread.
4 var c = <int>{...values.toList()};
5 var d = [...?maybe.toList()];
Remove the unnecessary `toList()` call from this spread.
5 var d = [...?maybe.toList()];
6 var e = [...first.toList(), ...second.toList()];
Remove the unnecessary `toList()` call from this 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()];
}

Configure unnecessary-to-list-in-spreads

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "performance": {
        "unnecessary-to-list-in-spreads": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/performance/unnecessary-to-list-in-spreads: <reason>