/
typeswitch.go
142 lines (118 loc) · 2.72 KB
/
typeswitch.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package typeswitch
import (
"go/ast"
"go/types"
"strings"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
var Analyzer = &analysis.Analyzer{
Name: "typeswitch",
Doc: Doc,
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
}
const Doc = "typeswitch finds a type which implement an interfaces which are used in type-switch but the type does not appear in any cases of the type-switch"
type enum struct {
Interface *types.Interface
Implements []types.Object
}
func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
// find enum-like interfaces
enums := getEnums(pass.Pkg)
for _, p := range pass.Pkg.Imports() {
for k, v := range getEnums(p) {
enums[k] = v
}
}
nodeFilter := []ast.Node{
(*ast.TypeSwitchStmt)(nil),
}
inspect.Preorder(nodeFilter, func(n ast.Node) {
sw, ok := n.(*ast.TypeSwitchStmt)
if !ok {
return
}
var typ types.Type
switch stmt := sw.Assign.(type) {
case *ast.ExprStmt:
if expr, ok := stmt.X.(*ast.TypeAssertExpr); ok {
typ = pass.TypesInfo.TypeOf(expr.X).Underlying()
}
case *ast.AssignStmt:
if expr, ok := stmt.Rhs[0].(*ast.TypeAssertExpr); ok {
typ = pass.TypesInfo.TypeOf(expr.X).Underlying()
}
default:
panic("unexpected type")
}
e := enums[typ]
if e == nil {
return
}
var ids []string
for _, obj := range e.Implements {
if !hasCase(pass, obj.Type(), sw) {
ids = append(ids, obj.Id())
}
}
if len(ids) != 0 {
pass.Reportf(sw.Pos(), "type %s does not appear in any cases", strings.Join(ids, ","))
}
})
return nil, nil
}
func getEnums(pkg *types.Package) map[types.Type]*enum {
var itfs []*types.Interface
var typs []types.Object
// find interfaces
for _, n := range pkg.Scope().Names() {
obj, ok := pkg.Scope().Lookup(n).(*types.TypeName)
if !ok {
continue
}
if itf, ok := obj.Type().Underlying().(*types.Interface); ok {
itfs = append(itfs, itf)
} else {
typs = append(typs, obj)
}
}
// find implements
enums := map[types.Type]*enum{}
for _, itf := range itfs {
e := &enum{
Interface: itf,
}
for _, typ := range typs {
if types.Implements(typ.Type(), itf) {
e.Implements = append(e.Implements, typ)
}
}
if len(e.Implements) >= 2 {
enums[itf] = e
}
}
return enums
}
func hasCase(pass *analysis.Pass, t types.Type, sw *ast.TypeSwitchStmt) bool {
for _, s := range sw.Body.List {
c, ok := s.(*ast.CaseClause)
if !ok {
continue
}
// default
if c.List == nil {
return true
}
for _, expr := range c.List {
if types.Identical(t, pass.TypesInfo.TypeOf(expr)) {
return true
}
}
}
return false
}