mirror of
https://github.com/mgechev/revive.git
synced 2025-06-08 23:26:29 +02:00
fix false positive in useless-break when a label follows a break (#1286)
Co-authored-by: chavacava <salvadorcavadini+github@gmail.com>
This commit is contained in:
parent
a50141aadb
commit
f4e39a7aa8
@ -47,36 +47,39 @@ func (w *lintUselessBreak) Visit(node ast.Node) ast.Visitor {
|
|||||||
w.inLoopBody = false
|
w.inLoopBody = false
|
||||||
return nil
|
return nil
|
||||||
case *ast.CommClause:
|
case *ast.CommClause:
|
||||||
for _, n := range v.Body {
|
w.inspectCaseStatement(v.Body)
|
||||||
w.inspectCaseStatement(n)
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
case *ast.CaseClause:
|
case *ast.CaseClause:
|
||||||
for _, n := range v.Body {
|
w.inspectCaseStatement(v.Body)
|
||||||
w.inspectCaseStatement(n)
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *lintUselessBreak) inspectCaseStatement(n ast.Stmt) {
|
func (w *lintUselessBreak) inspectCaseStatement(body []ast.Stmt) {
|
||||||
switch s := n.(type) {
|
l := len(body)
|
||||||
case *ast.BranchStmt:
|
if l == 0 {
|
||||||
if s.Tok != token.BREAK {
|
return // empty body, nothing to do
|
||||||
return // not a break statement
|
|
||||||
}
|
|
||||||
if s.Label != nil {
|
|
||||||
return // labeled break statement, usually affects a nesting loop
|
|
||||||
}
|
|
||||||
msg := "useless break in case clause"
|
|
||||||
if w.inLoopBody {
|
|
||||||
msg += " (WARN: this break statement affects this switch or select statement and not the loop enclosing it)"
|
|
||||||
}
|
|
||||||
w.onFailure(lint.Failure{
|
|
||||||
Confidence: 1,
|
|
||||||
Node: s,
|
|
||||||
Failure: msg,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s := body[l-1] // pick the last statement
|
||||||
|
if !isUnlabelledBreak(s) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
msg := "useless break in case clause"
|
||||||
|
if w.inLoopBody {
|
||||||
|
msg += " (WARN: this break statement affects this switch or select statement and not the loop enclosing it)"
|
||||||
|
}
|
||||||
|
|
||||||
|
w.onFailure(lint.Failure{
|
||||||
|
Confidence: 1,
|
||||||
|
Node: s,
|
||||||
|
Failure: msg,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func isUnlabelledBreak(stmt ast.Stmt) bool {
|
||||||
|
s, ok := stmt.(*ast.BranchStmt)
|
||||||
|
return ok && s.Tok == token.BREAK && s.Label == nil
|
||||||
}
|
}
|
||||||
|
12
testdata/useless_break.go
vendored
12
testdata/useless_break.go
vendored
@ -87,4 +87,16 @@ func UselessBreaks() {
|
|||||||
default:
|
default:
|
||||||
s.errorf("range can't iterate over %v", val)
|
s.errorf("range can't iterate over %v", val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// issue #1281
|
||||||
|
switch 1 {
|
||||||
|
case 1:
|
||||||
|
fmt.Println("foo")
|
||||||
|
break // MATCH /useless break in case clause/
|
||||||
|
case 2:
|
||||||
|
fmt.Println("bar")
|
||||||
|
break
|
||||||
|
fmt.Println("baz")
|
||||||
|
case 3:
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user