unnecessary-breaks

Same as unnecessary_breaks in Dart lints

Group
Style
Path
lint/style/unnecessary-breaks
Default
On (recommended)
Origin
Dart lints
Rule
451 of 512
recommended

Flags a trailing break in a switch case that already ends implicitly.

Since Dart 3.0, a non-empty case body never falls through, so an unlabeled break; as its last statement does nothing. A case whose only statement is break; is left alone: without it the case would be empty and share the next case's body.

switch (x) {
  case 1:
    log();
    break; // flagged
  case 2:
    break; // ok: keeps the case from sharing the next body
  default:
    log();
}

Like the official lint, the rule is silent in libraries whose language version predates Dart 3.0, where those breaks prevent fall-through.

Invalid

example.dartdart
void f(int x) {
  switch (x) {
    case 1:
      print('one');
      break;
    case 2:
    case 3:
      print('two or three');
      break;
    default:
      print('other');
      break;
  }
  for (var i = 0; i < x; i++) {
    switch (i) {
      case 0:
        print(i);
        print(i + 1);
        break;
      case 1:
        if (x > 2) {
          print(x);
        }
        break;
    }
  }
}
Remove the unnecessary `break` statement.
4 print('one');
5 break;
Remove the unnecessary `break` statement.
8 print('two or three');
9 break;
Remove the unnecessary `break` statement.
11 print('other');
12 break;
Remove the unnecessary `break` statement.
18 print(i + 1);
19 break;
Remove the unnecessary `break` statement.
23 }
24 break;

Valid

example.dartdart
void f(int x) {
  switch (x) {
    case 1:
      break;
    case 2:
      print('two');
    default:
      print('other');
  }
  outer:
  for (var i = 0; i < x; i++) {
    switch (i) {
      case 0:
        print(i);
        break outer;
      case 1:
        {
          print(i);
          break;
        }
      default:
        print('x');
    }
  }
  while (x > 0) {
    x--;
    if (x == 3) break;
  }
}

Configure unnecessary-breaks

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "unnecessary-breaks": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/unnecessary-breaks: <reason>