dart-lang / linter

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

`unnecessary_parenthesis` false positive for switch expressions #5008

Open JaffaKetchup opened 4 months ago

JaffaKetchup commented 4 months ago

Describe the issue When using a switch expression that is immediately followed by a method on the return result (such as .then when the expression returns Futures, parenthesis are required, but the linter creates a false positive.

To Reproduce

void valid() {
  const v = 0;
  (switch (v) { _ => Future.value() }).then((_) {}); // Lint thrown, syntax valid
}

void invalid() {
  const v = 0;
  switch (v) { _ => Future.value() }.then((_) {}); // Lint not thrown (suggested fix), invalid syntax
}

Expected behavior The lint should not be thrown

JaffaKetchup commented 3 months ago

Would appriciate a triage :) No rush ofc. Kinda feels similar to #4871 (an expression that should be wrapped with brackets in an unexpected place).

srawlins commented 3 months ago

I'm not sure why no one is triaging this repo. But I threw some labels on.

srawlins commented 2 weeks ago

Here's another interesting example. The code could remain legal when removing the parens, if we're not looking at an expression-statement. For example:

Future<void> invalid(Object v) {
  return switch (v) { _ => Future.value() }.then((_) {});
}

This is legal code. However, I think it might be surprising that parentheses aren't required around the switch expression, so I think they should be allowed, even if it doesn't cause a parse error.

JaffaKetchup commented 2 weeks ago

I'm surprised that that is legal but the version without return isn't. I guess the answers can be found in the language definition, but I agree that does seem like a case where you might want to allow unnecessary brackets.