unnecessary_breaks
Don't use explicit break
s when a break is implied.
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 break
s at the end of non-empty switch case statements
are unnecessary.
BAD:
switch (1) {
case 1:
print("one");
break;
case 2:
print("two");
break;
}
GOOD:
switch (1) {
case 1:
print("one");
case 2:
print("two");
}
switch (1) {
case 1:
case 2:
print("one or two");
}
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.
Enable
#To enable the unnecessary_breaks
rule,
add unnecessary_breaks
under linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- unnecessary_breaks
If you're instead using the YAML map syntax to configure linter rules,
add unnecessary_breaks: true
under linter > rules:
linter:
rules:
unnecessary_breaks: true
除非另有说明,文档之所提及适用于 Dart 3.7.1 版本,本页面最后更新时间: 2025-01-27。 查看文档源码 或者 报告页面问题。