linter icon indicating copy to clipboard operation
linter copied to clipboard

False positive for unnecessary_parenthesis: extension on nullable type

Open spkersten opened this issue 1 year ago • 3 comments

The line with the (*) below causes info: Unnecessary use of parentheses. (unnecessary_parenthesis at line 1, but without parenthesis around foo?.items it's a syntax error.

Iterable<int> bar(Foo? foo) => (foo?.items).orEmptyWhenNull;  // *

class Foo {
  const Foo(this.items);

  final List<int> items;
}

extension IterableOrEmptyWhenNull<T> on Iterable<T>? {
  Iterable<T> get orEmptyWhenNull => this ?? Iterable<T>.empty();
}

Dart SDK version: 3.0.0 (stable) (Thu May 4 01:11:00 2023 -0700) on "macos_arm64"

(No warning was generated with the previous Dart version, 2.19.6)

spkersten avatar May 11 '23 08:05 spkersten

@oprypin: curious if this is related to your recent changes?

pq avatar May 11 '23 22:05 pq

I confirm this finding.

https://github.com/dart-lang/linter/commit/823ea6c8e79b61d51141d50bfa86e2d4a714f921 is the first bad commit.

It's about having an extension on nullable. The code of the lint can detect built-in methods like (nullable).hashCode, but makes no attempt to detect (nullable).extensionMethod.

oprypin avatar May 14 '23 14:05 oprypin

Just wanted to chime in that while our team loves this linter rule, we're planning to disable it since applying the quickfix has the potential to introduce bugs into our app, which is frankly outright dangerous for a quickfix.

kswoll avatar Nov 30 '23 23:11 kswoll

Fixed by https://github.com/dart-lang/sdk/commit/6c071d3148ddc8aa2226e859ea6650fc8b020c6a

srawlins avatar Apr 17 '24 15:04 srawlins