-
Notifications
You must be signed in to change notification settings - Fork 1
/
notparam.go
59 lines (50 loc) · 1.32 KB
/
notparam.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
package notparam
import (
"go/ast"
"go/token"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
const doc = "notparam restricts not to use type parameters in declaration of functions and types"
var Analyzer = &analysis.Analyzer{
Name: "notparam",
Doc: doc,
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
}
func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{
(*ast.FuncDecl)(nil),
(*ast.GenDecl)(nil),
}
inspect.Preorder(nodeFilter, func(n ast.Node) {
switch n := n.(type) {
case *ast.FuncDecl:
if l := n.Type.TypeParams.NumFields(); l == 1 {
pass.Reportf(n.Pos(), "%s has a type parameter", n.Name.Name)
} else if l > 1 {
pass.Reportf(n.Pos(), "%s has type parameters", n.Name.Name)
}
case *ast.GenDecl:
if n.Tok != token.TYPE {
return
}
for _, spec := range n.Specs {
tspec, _ := spec.(*ast.TypeSpec)
if tspec == nil {
continue
}
if l := tspec.TypeParams.NumFields(); l == 1 {
pass.Reportf(n.Pos(), "%s has a type parameter", tspec.Name.Name)
} else if l > 1 {
pass.Reportf(n.Pos(), "%s has type parameters", tspec.Name.Name)
}
}
}
})
return nil, nil
}