Skip to content

spread_of_empty_collection

Reports a spread of an empty collection literal or of null, which adds nothing. Delete the spread.

Tier
advisory
Scope
Dart
Lowest preset
recommended
Fix
deletes the spread, with its comma or its else, in a literal with type arguments

A spread of a collection literal with no elements (...[], ...<int>{}, ...const []), or of null with ...?. The violation covers the spread.

It does not report a spread of:

  • a literal with elements;
  • a value that may be empty at run time;
  • a constructor such as List.empty().

The fix deletes the spread, with its comma or its else, only where the enclosing literal has written type arguments: without them, the empty spread helps infer the literal’s type, as [1, ...[]] is a List<dynamic> and [1] a List<int>. It is not offered for a spread that is the first branch of an if element or the body of a for element.

The spread adds nothing to the collection it stands in.

Violating
List<int> items(List<int> base) => <int>[...base, ...[]];
Corrected
List<int> items(List<int> base) => <int>[...base];

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

Write why on the line directly above the suppression:

// Why it stays as written here.
// ignore: assay/spread_of_empty_collection

// ignore_for_file: assay/spread_of_empty_collection silences it in a whole file, and // ignore: spread_of_empty_collection without the prefix works too. See Suppressions.