-
Notifications
You must be signed in to change notification settings - Fork 0
/
fieldlist.go
46 lines (37 loc) · 868 Bytes
/
fieldlist.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
package consistent
import (
"go/ast"
"golang.org/x/tools/go/analysis"
)
const (
fieldListExplicit = "explicit"
fieldListCompact = "compact"
)
func checkFieldList(pass *analysis.Pass, fields *ast.FieldList, fieldTypePlural string, mode string) {
if fields == nil {
return
}
switch mode {
case flagIgnore:
case fieldListExplicit:
for _, f := range fields.List {
if len(f.Names) > 1 {
reportf(pass, fields.Pos(), "declare the type of %s explicitly", fieldTypePlural)
break
}
}
case fieldListCompact:
list := fields.List
if len(list) <= 1 {
return
}
for i, f := range list[1:] {
typ := pass.TypesInfo.TypeOf(f.Type)
prevTyp := pass.TypesInfo.TypeOf(list[i].Type)
if typ == prevTyp {
reportf(pass, fields.Pos(), "declare the type of similar consecutive %s only once", fieldTypePlural)
break
}
}
}
}