-
Notifications
You must be signed in to change notification settings - Fork 2
/
asasalint.go
166 lines (139 loc) · 3.47 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package asasalint
import (
"bytes"
"fmt"
"go/ast"
"go/printer"
"go/token"
"go/types"
"log"
"regexp"
"strings"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
const BuiltinExclusions = `^(fmt|log|logger|t|)\.(Print|Fprint|Sprint|Fatal|Panic|Error|Warn|Warning|Info|Debug|Log)(|f|ln)$`
type LinterSetting struct {
Exclude []string
NoBuiltinExclusions bool
IgnoreTest bool
}
func NewAnalyzer(setting LinterSetting) (*analysis.Analyzer, error) {
a, err := newAnalyzer(setting)
if err != nil {
return nil, err
}
return &analysis.Analyzer{
Name: "asasalint",
Doc: "check for pass []any as any in variadic func(...any)",
Run: a.run,
Requires: []*analysis.Analyzer{inspect.Analyzer},
}, nil
}
type analyzer struct {
excludes []*regexp.Regexp
setting LinterSetting
}
func newAnalyzer(setting LinterSetting) (*analyzer, error) {
a := &analyzer{
setting: setting,
}
if !a.setting.NoBuiltinExclusions {
a.excludes = append(a.excludes, regexp.MustCompile(BuiltinExclusions))
}
for _, exclude := range a.setting.Exclude {
if exclude != "" {
exp, err := regexp.Compile(exclude)
if err != nil {
return nil, err
}
a.excludes = append(a.excludes, exp)
}
}
return a, nil
}
func (a *analyzer) run(pass *analysis.Pass) (interface{}, error) {
inspectorInfo := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{(*ast.CallExpr)(nil)}
inspectorInfo.Preorder(nodeFilter, a.AsCheckVisitor(pass))
return nil, nil
}
func (a *analyzer) AsCheckVisitor(pass *analysis.Pass) func(ast.Node) {
return func(n ast.Node) {
if a.setting.IgnoreTest {
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, err := getFuncName(pass.Fset, caller)
if err != nil {
log.Println(err)
return
}
for _, exclude := range a.excludes {
if exclude.MatchString(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)
}
}
func getFuncName(fset *token.FileSet, caller *ast.CallExpr) (string, error) {
buf := new(bytes.Buffer)
if err := printer.Fprint(buf, fset, caller.Fun); err != nil {
return "", fmt.Errorf("unable to print node at %s: %w", fset.Position(caller.Fun.Pos()), err)
}
return buf.String(), nil
}
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
}