-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitchdefaults.go
52 lines (40 loc) · 960 Bytes
/
switchdefaults.go
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
package consistent
import (
"go/ast"
"github.com/go-toolsmith/astcast"
"golang.org/x/tools/go/analysis"
)
const (
switchDefaultsLast = "last"
switchDefaultsFirst = "first"
)
var switchDefaultsFlagAllowedValues = []string{flagIgnore, switchDefaultsLast, switchDefaultsFirst}
func checkSwitchDefault(pass *analysis.Pass, stmt *ast.SwitchStmt, mode string) {
if mode == flagIgnore {
return
}
idx := defaultClauseIndex(stmt)
if idx < 0 {
return
}
switch mode {
case switchDefaultsLast:
if idx == len(stmt.Body.List)-1 {
return
}
reportf(pass, stmt.Body.List[idx].Pos(), "move switch default clause to the end")
case switchDefaultsFirst:
if idx == 0 {
return
}
reportf(pass, stmt.Body.List[idx].Pos(), "move switch default clause to the beginning")
}
}
func defaultClauseIndex(stmt *ast.SwitchStmt) int {
for i, c := range stmt.Body.List {
if astcast.ToCaseClause(c).List == nil {
return i
}
}
return -1
}