1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
package rule
import (
"go/ast"
"go/token"
"github.com/mgechev/revive/lint"
)
// UnnecessaryStmtRule warns on unnecessary statements.
type UnnecessaryStmtRule struct{}
// Apply applies the rule to given file.
func (r *UnnecessaryStmtRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure
onFailure := func(failure lint.Failure) {
failures = append(failures, failure)
}
w := lintUnnecessaryStmtRule{onFailure}
ast.Walk(w, file.AST)
return failures
}
// Name returns the rule name.
func (r *UnnecessaryStmtRule) Name() string {
return "unnecessary-stmt"
}
type lintUnnecessaryStmtRule struct {
onFailure func(lint.Failure)
}
func (w lintUnnecessaryStmtRule) Visit(node ast.Node) ast.Visitor {
switch n := node.(type) {
case *ast.FuncDecl:
if n.Body == nil || n.Type.Results != nil {
return w
}
stmts := n.Body.List
if len(stmts) == 0 {
return w
}
lastStmt := stmts[len(stmts)-1]
rs, ok := lastStmt.(*ast.ReturnStmt)
if !ok {
return w
}
if len(rs.Results) == 0 {
w.newFailure(lastStmt, "omit unnecessary return statement")
}
case *ast.SwitchStmt:
w.checkSwitchBody(n.Body)
case *ast.TypeSwitchStmt:
w.checkSwitchBody(n.Body)
case *ast.CaseClause:
if n.Body == nil {
return w
}
stmts := n.Body
if len(stmts) == 0 {
return w
}
lastStmt := stmts[len(stmts)-1]
rs, ok := lastStmt.(*ast.BranchStmt)
if !ok {
return w
}
if rs.Tok == token.BREAK && rs.Label == nil {
w.newFailure(lastStmt, "omit unnecessary break at the end of case clause")
}
}
return w
}
func (w lintUnnecessaryStmtRule) checkSwitchBody(b *ast.BlockStmt) {
cases := b.List
if len(cases) != 1 {
return
}
cc, ok := cases[0].(*ast.CaseClause)
if !ok {
return
}
if len(cc.List) > 1 { // skip cases with multiple expressions
return
}
w.newFailure(b, "switch with only one case can be replaced by an if-then")
}
func (w lintUnnecessaryStmtRule) newFailure(node ast.Node, msg string) {
w.onFailure(lint.Failure{
Confidence: 1,
Node: node,
Category: "style",
Failure: msg,
})
}
|