Skip to content

[unnecessary_parenthesis] false negative inside switch expressions #57137

@FMorschel

Description

@FMorschel

Describe the issue
unnecessary_parenthesis false negative.

When having a code like:

T a<T>(int x, bool test) {
  switch (x) {
    case 0:
      return test ? 1 : 2 as T;
    default:
      return 3 as T;
  }
}

The return of case 0 warns: A value of type 'Object?' can't be returned from the function 'a' because it has a return type of 'T'.

So in that case, we need to wrap that in parentheses.

return (test ? 1 : 2) as T;

This doesn't throw anymore.

When changing to the new pattern switch syntax, we can cast at the end as follows:

T a<T>(int x, bool test) {
  return switch (x) {
    0 => (test ? 1 : 2),   // Not triggering
    _ => 3,
  } as T;
}

And now that is not needed anymore and should trigger the lint.

Metadata

Metadata

Assignees

Labels

P2A bug or feature request we're likely to work onlegacy-area-analyzerUse area-devexp instead.linter-false-negativeIssues related to lint rules that fail to report a problem.triage-automationSee https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot.type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions