-
Notifications
You must be signed in to change notification settings - Fork 0
/
delrange.go
145 lines (119 loc) · 2.91 KB
/
delrange.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
143
144
145
package delrange
import (
"go/ast"
"go/types"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
const doc = "delrange is a static analysis tool which detects delete function is called with a value different from range key."
// Analyzer is ...
var Analyzer = &analysis.Analyzer{
Name: "delrange",
Doc: doc,
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
}
func run(pass *analysis.Pass) (interface{}, error) {
inspector := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{
(*ast.RangeStmt)(nil),
}
inspector.Preorder(nodeFilter, func(n ast.Node) {
rangeStmt, ok := n.(*ast.RangeStmt)
if !ok || rangeStmt == nil {
return
}
if _, ok := pass.TypesInfo.TypeOf(rangeStmt.X).(*types.Map); !ok {
return
}
ident, ok := rangeStmt.Key.(*ast.Ident)
if !ok || ident == nil {
return
}
assignStmt, ok := ident.Obj.Decl.(*ast.AssignStmt)
if !ok || assignStmt == nil {
return
}
keyIdent := getKeyIdent(assignStmt)
if keyIdent == nil {
return
}
reportUsingDeleteFuncWithIterationKey(pass, rangeStmt.Body, keyIdent)
})
return nil, nil
}
func getKeyIdent(stmt *ast.AssignStmt) *ast.Ident {
if len(stmt.Lhs) == 0 {
return nil
}
ident, ok := stmt.Lhs[0].(*ast.Ident)
if !ok || ident == nil {
return nil
}
if ident.Obj.Kind == ast.Var {
return ident
}
return nil
}
func reportUsingDeleteFuncWithIterationKey(pass *analysis.Pass, body *ast.BlockStmt, keyIdent *ast.Ident) {
sameValueKeyIdents := map[*ast.Object]*ast.Ident{keyIdent.Obj: keyIdent}
ast.Inspect(body, func(n ast.Node) bool {
if n == nil {
return false
}
switch n := n.(type) {
case *ast.AssignStmt:
updateSameValueKeyIdents(n, keyIdent, sameValueKeyIdents)
case *ast.CallExpr:
fun, ok := n.Fun.(*ast.Ident)
if !ok || fun == nil {
return true
}
if fun.Name != "delete" {
return true
}
if len(n.Args) != 2 {
return true
}
if secondArg, ok := n.Args[1].(*ast.Ident); ok {
for _, keyIdent := range sameValueKeyIdents {
if secondArg.Obj == keyIdent.Obj {
return true
}
}
}
pass.Reportf(fun.Pos(), "delete function is called with a value different from range key")
}
return true
})
}
func updateSameValueKeyIdents(n *ast.AssignStmt, keyIdent *ast.Ident, sameValueKeyIdents map[*ast.Object]*ast.Ident) {
for i, r := range n.Rhs {
ident, ok := r.(*ast.Ident)
if !ok || ident == nil {
continue
}
if ident.Obj == keyIdent.Obj {
l := n.Lhs[i].(*ast.Ident)
sameValueKeyIdents[l.Obj] = l
}
}
for i, l := range n.Lhs {
li, ok := l.(*ast.Ident)
if !ok || li == nil {
continue
}
if _, ok := sameValueKeyIdents[li.Obj]; ok {
r := n.Rhs[i]
if ri, ok := r.(*ast.Ident); ok {
if _, exist := sameValueKeyIdents[ri.Obj]; exist {
continue
}
}
delete(sameValueKeyIdents, li.Obj)
}
}
}