Skip to content

Linter: spread collections phase 2 #57898

Closed
@pq

Description

@pq

Linter-specific issue corresponding to #35570 and tracking new lints to support spread collections as well as any spread collection enhanced behavior of existing ones.

New Rules

Proposal: prefer_spread_collections

(Corresponding roughly to the server assist: DartAssistKind.CONVERT_TO_SPREAD.)

Description: Use spread collections when possible.

Details: Collection literals are excellent when you want to create a new collection out of individual items. But, when existing items are already stored in another collection, spread collection syntax leads to simpler code.

BAD:

Widget build(BuildContext context) {
  return CupertinoPageScaffold(
    child: ListView(
      children: [
        Tab2Header(),
      ]..addAll(buildTab2Conversation()),
    ),
  );
}
var ints = [1, 2, 3];
print(['a']..addAll(ints.map((i) => i.toString()))..addAll(['c']));
var things;
var l = ['a']..addAll(things ?? const []);

GOOD:

Widget build(BuildContext context) {
  return CupertinoPageScaffold(
    child: ListView(
      children: [
        Tab2Header(),
        ...buildTab2Conversation(),
      ],
    ),
  );
}
var ints = [1, 2, 3];
print(['a', ...ints.map((i) => i.toString()), 'c');
var things;
var l = ['a', ...?things];

References

Language tracking issue: dart-lang/language#164
Feature specification: https://github.com/dart-lang/language/blob/master/accepted/future-releases/spread-collections/feature-specification.md
Implementation plan: https://github.com/dart-lang/language/blob/master/accepted/future-releases/spread-collections/implementation-plan.md

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-metaCross-cutting, high-level issues (for tracking many other implementation issues, ...).devexp-linterIssues with the analyzer's support for the linter packagelegacy-area-analyzerUse area-devexp instead.linter-new-language-feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions