@@ -14,7 +14,6 @@ func (*NestedStructs) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
14
14
var failures []lint.Failure
15
15
16
16
walker := & lintNestedStructs {
17
- fileAST : file .AST ,
18
17
onFailure : func (failure lint.Failure ) {
19
18
failures = append (failures , failure )
20
19
},
@@ -31,47 +30,46 @@ func (*NestedStructs) Name() string {
31
30
}
32
31
33
32
type lintNestedStructs struct {
34
- fileAST * ast.File
35
33
onFailure func (lint.Failure )
36
34
}
37
35
38
36
func (l * lintNestedStructs ) Visit (n ast.Node ) ast.Visitor {
39
- switch v := n .(type ) {
40
- case * ast.TypeSpec :
41
- _ , isInterface := v .Type .(* ast.InterfaceType )
42
- if isInterface {
43
- return nil // do not analyze interface declarations
44
- }
45
- case * ast.FuncDecl :
46
- if v .Body != nil {
47
- ast .Walk (l , v .Body )
48
- }
49
- return nil
50
- case * ast.Field :
51
- _ , isChannelField := v .Type .(* ast.ChanType )
52
- if isChannelField {
53
- return nil
54
- }
37
+ if v , ok := n .(* ast.StructType ); ok {
38
+ ls := & lintStruct {l .onFailure }
39
+ ast .Walk (ls , v .Fields )
40
+ }
55
41
56
- filter := func (n ast.Node ) bool {
57
- switch n .(type ) {
58
- case * ast.StructType :
59
- return true
60
- default :
61
- return false
62
- }
63
- }
64
- structs := pick (v , filter , nil )
65
- for _ , s := range structs {
66
- l .onFailure (lint.Failure {
67
- Failure : "no nested structs are allowed" ,
68
- Category : "style" ,
69
- Node : s ,
70
- Confidence : 1 ,
71
- })
42
+ return l
43
+ }
44
+
45
+ type lintStruct struct {
46
+ onFailure func (lint.Failure )
47
+ }
48
+
49
+ func (l * lintStruct ) Visit (n ast.Node ) ast.Visitor {
50
+ switch s := n .(type ) {
51
+ case * ast.StructType :
52
+ l .fail (s )
53
+ return nil
54
+ case * ast.ArrayType :
55
+ if _ , ok := s .Elt .(* ast.StructType ); ok {
56
+ l .fail (s )
72
57
}
73
- return nil // no need to visit (again) the field
58
+ return nil
59
+ case * ast.ChanType :
60
+ return nil
61
+ case * ast.MapType :
62
+ return nil
63
+ default :
64
+ return l
74
65
}
66
+ }
75
67
76
- return l
68
+ func (l * lintStruct ) fail (n ast.Node ) {
69
+ l .onFailure (lint.Failure {
70
+ Failure : "no nested structs are allowed" ,
71
+ Category : "style" ,
72
+ Node : n ,
73
+ Confidence : 1 ,
74
+ })
77
75
}
0 commit comments