@@ -47,36 +47,39 @@ func (w *lintUselessBreak) Visit(node ast.Node) ast.Visitor {
47
47
w .inLoopBody = false
48
48
return nil
49
49
case * ast.CommClause :
50
- for _ , n := range v .Body {
51
- w .inspectCaseStatement (n )
52
- }
50
+ w .inspectCaseStatement (v .Body )
53
51
return nil
54
52
case * ast.CaseClause :
55
- for _ , n := range v .Body {
56
- w .inspectCaseStatement (n )
57
- }
53
+ w .inspectCaseStatement (v .Body )
58
54
return nil
59
55
}
60
56
return w
61
57
}
62
58
63
- func (w * lintUselessBreak ) inspectCaseStatement (n ast.Stmt ) {
64
- switch s := n .(type ) {
65
- case * ast.BranchStmt :
66
- if s .Tok != token .BREAK {
67
- return // not a break statement
68
- }
69
- if s .Label != nil {
70
- return // labeled break statement, usually affects a nesting loop
71
- }
72
- msg := "useless break in case clause"
73
- if w .inLoopBody {
74
- msg += " (WARN: this break statement affects this switch or select statement and not the loop enclosing it)"
75
- }
76
- w .onFailure (lint.Failure {
77
- Confidence : 1 ,
78
- Node : s ,
79
- Failure : msg ,
80
- })
59
+ func (w * lintUselessBreak ) inspectCaseStatement (body []ast.Stmt ) {
60
+ l := len (body )
61
+ if l == 0 {
62
+ return // empty body, nothing to do
81
63
}
64
+
65
+ s := body [l - 1 ] // pick the last statement
66
+ if ! isUnlabelledBreak (s ) {
67
+ return
68
+ }
69
+
70
+ msg := "useless break in case clause"
71
+ if w .inLoopBody {
72
+ msg += " (WARN: this break statement affects this switch or select statement and not the loop enclosing it)"
73
+ }
74
+
75
+ w .onFailure (lint.Failure {
76
+ Confidence : 1 ,
77
+ Node : s ,
78
+ Failure : msg ,
79
+ })
80
+ }
81
+
82
+ func isUnlabelledBreak (stmt ast.Stmt ) bool {
83
+ s , ok := stmt .(* ast.BranchStmt )
84
+ return ok && s .Tok == token .BREAK && s .Label == nil
82
85
}
0 commit comments