dart-lang / linter

Linter for Dart.
https://dart.dev/tools/linter-rules
BSD 3-Clause "New" or "Revised" License
636 stars 170 forks source link

Add lint of a null check then spread or expand other lint #5093

Open parlough opened 2 months ago

parlough commented 2 months ago

Name

Some options:

Description

The analyzer produces this diagnostic when a list is added to a collection literal with a spread operator after checking if it's not null.

Categories

style, languageFeatureUsage

Bad examples

final list = [
  itemOne,
  itemTwo,
  if (extraItems != null) ...extraItems,
];

Good examples

final list = [
  itemOne,
  itemTwo,
  ...?extraItems,
];

Fix discussion

A widely applicable fix should be possible. If the lint isn't seen as valuable enough to many, just the assist would still be valuable.

Alternative(s)

Expand prefer_null_aware_operators to cover these cases as it covers other conditional checks then usages.

prefer_spread_collections could also work as it already covers some null-aware cases, and I imagine if one wants that, they'd almost always prefer to use it here too.

srawlins commented 2 months ago

I'd be in favor of enhancing prefer_spread_collections.