dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.24k stars 1.57k forks source link

Support "fixing" switches with cascading conditions into switch expressions with `||` #52958

Closed kevmoo closed 1 month ago

kevmoo commented 1 year ago

I think I'm using the terms correctly.

  int get elementSize {
    switch (type) {
      case 'float':
      case 'int':
        return 4;
      case 'short':
        return 2;
      case 'byte':
        return 1;
      default:
        return 0;
    }

should have a suggested fix to

    return switch (type) {
      'float' || 'int' => 4,
      'short' => 2,
      'byte' => 1,
      _ => 0
    };

but it does not!

pq commented 1 year ago

Yeah. We don't currently have the smarts to handle:

case 'float':
case 'int':
  return 4;

(but we should add 'em!)

parlough commented 12 months ago

An interesting workaround here is that you can convert to an if-case statement chain, then back to a switch statement, then finally to a switch expression :P

FMorschel commented 2 months ago

I believe this specific ask was already solved by https://github.com/dart-lang/sdk/issues/54567 (@srawlins to close this). I also created my request to it https://github.com/dart-lang/sdk/issues/55861 missing both.

Current related issues that I know of (1 request to separate the assist to merge the cases and 2 bugs with the current assist respectively):

There are also some other issues I created related to the formatter and some suggested lints all linked to the above issues.

srawlins commented 1 month ago

Ah excellent, closing then.