|
| 1 | +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:analyzer/dart/analysis/features.dart'; |
| 6 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 7 | +import 'package:analyzer/dart/ast/visitor.dart'; |
| 8 | + |
| 9 | +import '../analyzer.dart'; |
| 10 | + |
| 11 | +const _desc = r"Don't use explicit `break`s when a break is implied."; |
| 12 | + |
| 13 | +const _details = r''' |
| 14 | +Only use a `break` in a non-empty switch case statement if you need to break |
| 15 | +before the end of the case body. Dart does not support fallthrough execution |
| 16 | +for non-empty cases, so `break`s at the end of non-empty switch case statements |
| 17 | +are unnecessary. |
| 18 | +
|
| 19 | +**BAD:** |
| 20 | +```dart |
| 21 | +switch (1) { |
| 22 | + case 1: |
| 23 | + print("one"); |
| 24 | + break; |
| 25 | + case 2: |
| 26 | + print("two"); |
| 27 | + break; |
| 28 | +} |
| 29 | +``` |
| 30 | +
|
| 31 | +**GOOD:** |
| 32 | +```dart |
| 33 | +switch (1) { |
| 34 | + case 1: |
| 35 | + print("one"); |
| 36 | + case 2: |
| 37 | + print("two"); |
| 38 | +} |
| 39 | +``` |
| 40 | +
|
| 41 | +```dart |
| 42 | +switch (1) { |
| 43 | + case 1: |
| 44 | + case 2: |
| 45 | + print("one or two"); |
| 46 | +} |
| 47 | +``` |
| 48 | +
|
| 49 | +```dart |
| 50 | +switch (1) { |
| 51 | + case 1: |
| 52 | + break; |
| 53 | + case 2: |
| 54 | + print("just two"); |
| 55 | +} |
| 56 | +``` |
| 57 | +'''; |
| 58 | + |
| 59 | +class UnnecessaryBreaks extends LintRule { |
| 60 | + static const LintCode code = LintCode( |
| 61 | + 'unnecessary_breaks', "Unnecessary 'break' statement.", |
| 62 | + correctionMessage: "Try removing the 'break'."); |
| 63 | + |
| 64 | + UnnecessaryBreaks() |
| 65 | + : super( |
| 66 | + name: 'unnecessary_breaks', |
| 67 | + description: _desc, |
| 68 | + details: _details, |
| 69 | + group: Group.style); |
| 70 | + |
| 71 | + @override |
| 72 | + LintCode get lintCode => code; |
| 73 | + |
| 74 | + @override |
| 75 | + void registerNodeProcessors( |
| 76 | + NodeLintRegistry registry, LinterContext context) { |
| 77 | + if (!context.isEnabled(Feature.patterns)) return; |
| 78 | + var visitor = _Visitor(this); |
| 79 | + registry.addBreakStatement(this, visitor); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +class _Visitor extends SimpleAstVisitor { |
| 84 | + final LintRule rule; |
| 85 | + |
| 86 | + _Visitor(this.rule); |
| 87 | + |
| 88 | + @override |
| 89 | + visitBreakStatement(BreakStatement node) { |
| 90 | + if (node.label != null) return; |
| 91 | + var parent = node.parent; |
| 92 | + if (parent is SwitchPatternCase) { |
| 93 | + var statements = parent.statements; |
| 94 | + if (statements.length == 1) return; |
| 95 | + if (node == statements.last) { |
| 96 | + rule.reportLint(node); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments