This repository was archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 166
+ unnecessary_breaks #3981
Merged
Merged
+ unnecessary_breaks #3981
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:analyzer/dart/analysis/features.dart'; | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/dart/ast/visitor.dart'; | ||
|
||
import '../analyzer.dart'; | ||
|
||
const _desc = r"Don't use explicit `break`s when a break is implied."; | ||
|
||
const _details = r''' | ||
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:** | ||
```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"); | ||
} | ||
``` | ||
'''; | ||
|
||
class UnnecessaryBreaks extends LintRule { | ||
static const LintCode code = LintCode( | ||
'unnecessary_breaks', "Unnecessary 'break' statement.", | ||
correctionMessage: "Try removing the 'break'."); | ||
|
||
UnnecessaryBreaks() | ||
: super( | ||
name: 'unnecessary_breaks', | ||
description: _desc, | ||
details: _details, | ||
group: Group.style); | ||
|
||
@override | ||
LintCode get lintCode => code; | ||
|
||
@override | ||
void registerNodeProcessors( | ||
NodeLintRegistry registry, LinterContext context) { | ||
if (!context.isEnabled(Feature.patterns)) return; | ||
var visitor = _Visitor(this); | ||
pq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
registry.addBreakStatement(this, visitor); | ||
} | ||
} | ||
|
||
class _Visitor extends SimpleAstVisitor { | ||
final LintRule rule; | ||
|
||
_Visitor(this.rule); | ||
|
||
@override | ||
visitBreakStatement(BreakStatement node) { | ||
pq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (node.label != null) return; | ||
var parent = node.parent; | ||
if (parent is SwitchPatternCase) { | ||
var statements = parent.statements; | ||
if (statements.length == 1) return; | ||
if (node == statements.last) { | ||
rule.reportLint(node); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:test_reflective_loader/test_reflective_loader.dart'; | ||
|
||
import '../rule_test_support.dart'; | ||
|
||
main() { | ||
defineReflectiveSuite(() { | ||
defineReflectiveTests(UnnecessaryBreaksTest); | ||
}); | ||
} | ||
|
||
@reflectiveTest | ||
class UnnecessaryBreaksTest extends LintRuleTest { | ||
pq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@override | ||
List<String> get experiments => ['patterns', 'records']; | ||
|
||
@override | ||
String get lintRule => 'unnecessary_breaks'; | ||
|
||
test_switch_pre30_ok() async { | ||
await assertNoDiagnostics(r''' | ||
// @dart=2.19 | ||
f() { | ||
switch (1) { | ||
case 1: | ||
f(); | ||
break; | ||
} | ||
} | ||
'''); | ||
} | ||
|
||
test_switchPatternCase() async { | ||
await assertDiagnostics(r''' | ||
f() { | ||
switch (1) { | ||
case 1: | ||
f(); | ||
break; | ||
case 2: | ||
f(); | ||
} | ||
} | ||
''', [ | ||
lint(50, 6), | ||
]); | ||
} | ||
|
||
test_switchPatternCase_empty_ok() async { | ||
await assertNoDiagnostics(r''' | ||
f() { | ||
switch (1) { | ||
case 1: | ||
break; | ||
case 2: | ||
f(); | ||
} | ||
} | ||
'''); | ||
} | ||
|
||
test_switchPatternCase_labeled_ok() async { | ||
await assertNoDiagnostics( | ||
r''' | ||
f() { | ||
l: | ||
switch (1) { | ||
case 1: | ||
break l; | ||
case 2: | ||
f(); | ||
} | ||
} | ||
''', | ||
); | ||
} | ||
|
||
test_switchPatternCase_notDirectChild_ok() async { | ||
await assertNoDiagnostics(r''' | ||
f(bool c) { | ||
switch (1) { | ||
case 1: | ||
if (c) break; | ||
f(true); | ||
case 2: | ||
f(true); | ||
} | ||
} | ||
'''); | ||
} | ||
|
||
test_switchPatternCase_notLast_ok() async { | ||
await assertDiagnostics(r''' | ||
f(bool c) { | ||
switch (1) { | ||
case 1: | ||
break; | ||
f(true); | ||
case 2: | ||
f(true); | ||
} | ||
} | ||
''', [ | ||
// No lint. | ||
error(HintCode.DEAD_CODE, 58, 8), | ||
]); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.