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
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
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;
}
}
} print('one'); break; print('two or three'); break; print('other'); break; print(i + 1); break; } break;Valid
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.
{
"linter": {
"rules": {
"style": {
"unnecessary-breaks": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/unnecessary-breaks: <reason>More style rules
- unnecessary-constructor-name
unnecessary_constructor_namerecommendedFlags a redundant
.newon constructor declarations and invocations. - unnecessary-final
unnecessary_finalDon't use
finalfor local variables and parameters. - unnecessary-late
unnecessary_laterecommendedFlags a redundant
latemodifier on an initialized static or top-level variable. - unnecessary-library-directive
unnecessary_library_directiverecommendedFlags a
librarydirective that has neither a doc comment nor annotations. - unnecessary-library-name
unnecessary_library_namerecommendedFlags a
librarydirective that carries an explicit name. - unnecessary-new
unnecessary_newrecommendedFlags an explicit
newkeyword in an instance-creation expression. - unnecessary-null-aware-operator-on-extension-on-nullable
unnecessary_null_aware_operator_on_extension_on_nullableFlags a null-aware access whose member comes from an extension on a nullable type.
- unnecessary-null-checks
unnecessary_null_checksFlags a null assertion
!on a value that flows into a nullable context.