-
Notifications
You must be signed in to change notification settings - Fork 2
/
asasalint.go
152 lines (130 loc) · 3.22 KB
/
asasalint.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
package asasalint
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"strings"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
const DefaultExclude = `Printf,Println,Errorf,Fprintf,Fprintln,Fatal,Fatalf,Panic,Panicf,Panicln,Print,Printf,Println,Sprintf,Sprintln,Error,Errorf,Info,Infof,Warn,Warnf,Debug,Debugf`
type LinterSetting struct {
Exclude []string
NoDefaultExclude bool
IgnoreInTest bool
}
func NewAnalyzer(setting LinterSetting) *analysis.Analyzer {
a := newAnalyzer(setting)
return &analysis.Analyzer{
Name: "asasalint",
Doc: "check for pass []any as any in variadic func(...any)",
Run: a.run,
Requires: []*analysis.Analyzer{inspect.Analyzer},
}
}
type analyzer struct {
excludes map[string]bool
setting LinterSetting
}
func newAnalyzer(setting LinterSetting) *analyzer {
a := &analyzer{
excludes: make(map[string]bool),
setting: setting,
}
if !a.setting.NoDefaultExclude {
for _, exclude := range strings.Split(DefaultExclude, `,`) {
a.excludes[exclude] = true
}
}
for _, exclude := range a.setting.Exclude {
if exclude != "" {
a.excludes[exclude] = true
}
}
return a
}
func (a *analyzer) run(pass *analysis.Pass) (interface{}, error) {
inspectorInfo := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{(*ast.CallExpr)(nil)}
inspectorInfo.Nodes(nodeFilter, a.AsCheckVisitor(pass))
return nil, nil
}
func (a *analyzer) AsCheckVisitor(pass *analysis.Pass) func(n ast.Node, push bool) bool {
return func(n ast.Node, push bool) (processed bool) {
processed = true
if a.setting.IgnoreInTest {
pos := pass.Fset.Position(n.Pos())
if strings.HasSuffix(pos.Filename, "_test.go") {
return
}
}
caller, ok := n.(*ast.CallExpr)
if !ok {
return
}
if caller.Ellipsis != token.NoPos {
return
}
if len(caller.Args) == 0 {
return
}
fnName := getFuncName(caller)
if a.excludes[fnName] {
return
}
fnType := pass.TypesInfo.TypeOf(caller.Fun)
if !isSliceAnyVariadicFuncType(fnType) {
return
}
fnSign := fnType.(*types.Signature)
if len(caller.Args) != fnSign.Params().Len() {
return
}
lastArg := caller.Args[len(caller.Args)-1]
argType := pass.TypesInfo.TypeOf(lastArg)
if !isSliceAnyType(argType) {
return
}
node := lastArg
d := analysis.Diagnostic{
Pos: node.Pos(),
End: node.End(),
Message: fmt.Sprintf("pass []any as any to func %s %s",
fnName, fnType.String()),
Category: "asasalint",
}
pass.Report(d)
return
}
}
func getFuncName(caller *ast.CallExpr) string {
if id, ok := caller.Fun.(*ast.Ident); ok {
return id.Name
}
if s, ok := caller.Fun.(*ast.SelectorExpr); ok {
return s.Sel.Name
}
return ""
}
func isSliceAnyVariadicFuncType(typ types.Type) (r bool) {
fnSign, ok := typ.(*types.Signature)
if !ok || !fnSign.Variadic() {
return false
}
params := fnSign.Params()
lastParam := params.At(params.Len() - 1)
return isSliceAnyType(lastParam.Type())
}
func isSliceAnyType(typ types.Type) (r bool) {
sliceType, ok := typ.(*types.Slice)
if !ok {
return
}
elemType, ok := sliceType.Elem().(*types.Interface)
if !ok {
return
}
return elemType.NumMethods() == 0
}