This repository has been archived by the owner on Dec 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
builtinShadow_checker.go
87 lines (76 loc) · 1.87 KB
/
builtinShadow_checker.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
package checkers
import (
"go/ast"
"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
)
func init() {
var info lintpack.CheckerInfo
info.Name = "builtinShadow"
info.Tags = []string{"style", "opinionated"}
info.Summary = "Detects when predeclared identifiers shadowed in assignments"
info.Before = `len := 10`
info.After = `length := 10`
collection.AddChecker(&info, func(ctx *lintpack.CheckerContext) lintpack.FileWalker {
builtins := map[string]bool{
// Types
"bool": true,
"byte": true,
"complex64": true,
"complex128": true,
"error": true,
"float32": true,
"float64": true,
"int": true,
"int8": true,
"int16": true,
"int32": true,
"int64": true,
"rune": true,
"string": true,
"uint": true,
"uint8": true,
"uint16": true,
"uint32": true,
"uint64": true,
"uintptr": true,
// Constants
"true": true,
"false": true,
"iota": true,
// Zero value
"nil": true,
// Functions
"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,
}
c := &builtinShadowChecker{ctx: ctx, builtins: builtins}
return astwalk.WalkerForLocalDef(c, ctx.TypesInfo)
})
}
type builtinShadowChecker struct {
astwalk.WalkHandler
ctx *lintpack.CheckerContext
builtins map[string]bool
}
func (c *builtinShadowChecker) VisitLocalDef(name astwalk.Name, _ ast.Expr) {
if _, isBuiltin := c.builtins[name.ID.String()]; isBuiltin {
c.warn(name.ID)
}
}
func (c *builtinShadowChecker) warn(ident *ast.Ident) {
c.ctx.Warn(ident, "shadowing of predeclared identifier: %s", ident)
}