/
redefines-builtin-id.go
181 lines (158 loc) 路 3.72 KB
/
redefines-builtin-id.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package rule
import (
"fmt"
"go/ast"
"go/token"
"github.com/mgechev/revive/lint"
)
var builtInConstAndVars = map[string]bool{
"true": true,
"false": true,
"iota": true,
"nil": true,
}
var builtFunctions = map[string]bool{
"append": true,
"cap": true,
"close": true,
"complex": true,
"copy": true,
"delete": true,
"imag": true,
"len": true,
"make": true,
"new": true,
"panic": true,
"print": true,
"println": true,
"real": true,
"recover": true,
}
var builtInTypes = map[string]bool{
"ComplexType": true,
"FloatType": true,
"IntegerType": true,
"Type": true,
"Type1": true,
"bool": true,
"byte": true,
"complex128": true,
"complex64": true,
"error": true,
"float32": true,
"float64": true,
"int": true,
"int16": true,
"int32": true,
"int64": true,
"int8": true,
"rune": true,
"string": true,
"uint": true,
"uint16": true,
"uint32": true,
"uint64": true,
"uint8": true,
"uintptr": true,
"any": true,
}
// RedefinesBuiltinIDRule warns when a builtin identifier is shadowed.
type RedefinesBuiltinIDRule struct{}
// Apply applies the rule to given file.
func (r *RedefinesBuiltinIDRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure
onFailure := func(failure lint.Failure) {
failures = append(failures, failure)
}
astFile := file.AST
w := &lintRedefinesBuiltinID{onFailure}
ast.Walk(w, astFile)
return failures
}
// Name returns the rule name.
func (r *RedefinesBuiltinIDRule) Name() string {
return "redefines-builtin-id"
}
type lintRedefinesBuiltinID struct {
onFailure func(lint.Failure)
}
func (w *lintRedefinesBuiltinID) Visit(node ast.Node) ast.Visitor {
switch n := node.(type) {
case *ast.GenDecl:
switch n.Tok {
case token.TYPE:
typeSpec, ok := n.Specs[0].(*ast.TypeSpec)
if !ok {
return nil
}
id := typeSpec.Name.Name
if ok, bt := w.isBuiltIn(id); ok {
w.addFailure(n, fmt.Sprintf("redefinition of the built-in %s %s", bt, id))
}
case token.VAR, token.CONST:
for _, vs := range n.Specs {
valSpec, ok := vs.(*ast.ValueSpec)
if !ok {
continue
}
for _, name := range valSpec.Names {
if ok, bt := w.isBuiltIn(name.Name); ok {
w.addFailure(n, fmt.Sprintf("redefinition of the built-in %s %s", bt, name))
}
}
}
default:
return nil // skip if not type/var/const declaration
}
case *ast.FuncDecl:
if n.Recv != nil {
return w // skip methods
}
id := n.Name.Name
if ok, bt := w.isBuiltIn(id); ok {
w.addFailure(n, fmt.Sprintf("redefinition of the built-in %s %s", bt, id))
}
case *ast.AssignStmt:
for _, e := range n.Lhs {
id, ok := e.(*ast.Ident)
if !ok {
continue
}
if ok, bt := w.isBuiltIn(id.Name); ok {
var msg string
switch bt {
case "constant or variable":
if n.Tok == token.DEFINE {
msg = fmt.Sprintf("assignment creates a shadow of built-in identifier %s", id.Name)
} else {
msg = fmt.Sprintf("assignment modifies built-in identifier %s", id.Name)
}
default:
msg = fmt.Sprintf("redefinition of the built-in %s %s", bt, id)
}
w.addFailure(n, msg)
}
}
}
return w
}
func (w lintRedefinesBuiltinID) addFailure(node ast.Node, msg string) {
w.onFailure(lint.Failure{
Confidence: 1,
Node: node,
Category: "logic",
Failure: msg,
})
}
func (w lintRedefinesBuiltinID) isBuiltIn(id string) (r bool, builtInKind string) {
if builtFunctions[id] {
return true, "function"
}
if builtInConstAndVars[id] {
return true, "constant or variable"
}
if builtInTypes[id] {
return true, "type"
}
return false, ""
}