1
0
mirror of https://github.com/mgechev/revive.git synced 2025-11-25 22:12:38 +02:00
Files
revive/testdata/unnecessary_stmt.go
chavacava 92f28cb5e1 refactor: moves code related to AST from rule.utils into astutils package (#1380)
Modifications summary:

* Moves AST-related functions from rule/utils.go to astutils/ast_utils.go (+ modifies function calls)
* Renames some of these AST-related functions
* Avoids instantiating a printer config at each call to astutils.GoFmt
* Uses astutils.IsIdent and astutils.IsPkgDotName when possible
2025-05-26 13:18:38 +02:00

56 lines
965 B
Go

package fixtures
import (
ast "go/ast"
"github.com/mgechev/revive/lint"
)
func foo(a, b, c, d int) {
switch n := node.(type) { // MATCH /switch with only one case can be replaced by an if-then/
case *ast.SwitchStmt:
caseSelector := func(n ast.Node) bool {
_, ok := n.(*ast.CaseClause)
return ok
}
cases := astutils.PickNodes(n.Body, caseSelector, nil)
if len(cases) == 1 {
cs, ok := cases[0].(*ast.CaseClause)
if ok && len(cs.List) == 1 {
w.onFailure(lint.Failure{
Confidence: 1,
Node: n,
Category: "style",
Failure: "switch can be replaced by an if-then",
})
}
}
}
}
func bar() {
a := 1
switch a {
case 1, 2:
a++
}
loop:
for {
switch a {
case 1:
a++
println("one")
break // MATCH /omit unnecessary break at the end of case clause/
case 2:
println("two")
break loop
default:
println("default")
}
}
return // MATCH /omit unnecessary return statement/
}