目录

unnecessary_breaks

目录

Don't use explicit breaks when a break is implied.

This rule is available as of Dart 3.0.

This rule has a quick fix available.

Details

#

Only use a break in a non-empty switch case statement if you need to break before the end of the case body. Dart does not support fallthrough execution for non-empty cases, so breaks at the end of non-empty switch case statements are unnecessary.

BAD:

dart
switch (1) {
  case 1:
    print("one");
    break;
  case 2:
    print("two");
    break;
}

GOOD:

dart
switch (1) {
  case 1:
    print("one");
  case 2:
    print("two");
}
dart
switch (1) {
  case 1:
  case 2:
    print("one or two");
}
dart
switch (1) {
  case 1:
    break;
  case 2:
    print("just two");
}

NOTE: This lint only reports unnecessary breaks in libraries with a language version of 3.0 or greater. Explicit breaks are still required in Dart 2.19 and below.

Usage

#

To enable the unnecessary_breaks rule, add unnecessary_breaks under linter > rules in your analysis_options.yaml file:

analysis_options.yaml
yaml
linter:
  rules:
    - unnecessary_breaks