Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assist for merging single-body swicth cases with a logical-or pattern #56180

Open
FMorschel opened this issue Jul 8, 2024 · 4 comments
Open
Labels
analyzer-assist Issues with analysis server assists area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug

Comments

@FMorschel
Copy link
Contributor

I'd like to ask for an assist for merging single-body swicth cases with a logical-or pattern

So cases like:

int a(int x) {
  switch (x) {
    case 0:
      return 0;
    case 2:
    case 1:
      return 1;
    default:
      return throw UnimplementedError();
  }
}

Can become:

switch (x) {
  case 0:
    return 0;
  case 1 || 2: // or swapped I'm not sure what to put first here (not sure it matters, just an UX question)
    return 1;
  default:
    return throw UnimplementedError();
}

Originally posted by @FMorschel in #55861 (comment)

@dart-github-bot
Copy link
Collaborator

Summary: The user proposes a syntax change to Dart's switch statement, allowing multiple case values to be combined using logical OR (||) for more concise code. They specifically want to merge cases with single-body statements.

@dart-github-bot dart-github-bot added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug labels Jul 8, 2024
@devoncarew devoncarew added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. and removed area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Jul 8, 2024
@bwilkerson bwilkerson added the analyzer-assist Issues with analysis server assists label Jul 8, 2024
@keertip keertip added the P3 A lower priority bug or feature request label Jul 9, 2024
@lrhn lrhn removed the triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. label Jul 19, 2024
@FMorschel
Copy link
Contributor Author

FMorschel commented Aug 7, 2024

Now that #55861 is closed, a workaround is to do both conversions to and from switch expression. It will give the same result asked above.

But it obviously just works when the switch statement can be converted.

@FMorschel
Copy link
Contributor Author

If dart-lang/linter#5078 is accepted it would work great with this as well.

@FMorschel
Copy link
Contributor Author

Also, dart-lang/linter#5079 (if accepted) would help ensure duplicates are removed as well as unreachable_switch_case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-assist Issues with analysis server assists area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

6 participants