-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtypeparam.go
76 lines (60 loc) · 1.6 KB
/
typeparam.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
package varnamelen
import (
"go/ast"
"golang.org/x/tools/go/analysis"
)
// typeParam represents a declared type parameter.
type typeParam struct {
// name is the name of the type parameter.
name string
// typ is the type of the type parameter.
typ string
// field is the field that declares the type parameter.
field *ast.Field
}
// match returns whether p matches decl.
func (p typeParam) match(decl identDeclaration) bool {
if p.name != decl.name {
return false
}
return decl.matchType(p.typ)
}
// checkTypeParams applies the analysis to type parameters in paramToDist, according to cfg.
func checkTypeParams(pass *analysis.Pass, paramToDist map[typeParam]int, cfg configuration) {
for param, dist := range paramToDist {
if cfg.ignoreNames.contains(param.name) {
continue
}
if cfg.ignoreDecls.matchTypeParameter(param) {
continue
}
if checkNameAndDistance(param.name, dist, cfg) {
continue
}
pass.Reportf(param.field.Pos(), "type parameter name '%s' is too short for the scope of its usage", param.name)
}
}
// isTypeParam returns true if field is a type parameter of any of the given funcs.
func isTypeParam(field *ast.Field, funcs []*ast.FuncDecl, funcLits []*ast.FuncLit) bool { //nolint:gocognit // it's not that complicated
for _, f := range funcs {
if f.Type.TypeParams == nil {
continue
}
for _, p := range f.Type.TypeParams.List {
if p == field {
return true
}
}
}
for _, f := range funcLits {
if f.Type.TypeParams == nil {
continue
}
for _, p := range f.Type.TypeParams.List {
if p == field {
return true
}
}
}
return false
}