@@ -51,74 +51,51 @@ func (r *UnusedReceiverRule) Apply(file *lint.File, args lint.Arguments) []lint.
51
51
r .configureOnce .Do (func () { r .configure (args ) })
52
52
var failures []lint.Failure
53
53
54
- onFailure := func (failure lint.Failure ) {
55
- failures = append (failures , failure )
56
- }
57
-
58
- w := lintUnusedReceiverRule {
59
- onFailure : onFailure ,
60
- allowRegex : r .allowRegex ,
61
- failureMsg : r .failureMsg ,
62
- }
63
-
64
- ast .Walk (w , file .AST )
65
-
66
- return failures
67
- }
68
-
69
- // Name returns the rule name.
70
- func (* UnusedReceiverRule ) Name () string {
71
- return "unused-receiver"
72
- }
73
-
74
- type lintUnusedReceiverRule struct {
75
- onFailure func (lint.Failure )
76
- allowRegex * regexp.Regexp
77
- failureMsg string
78
- }
79
-
80
- func (w lintUnusedReceiverRule ) Visit (node ast.Node ) ast.Visitor {
81
- switch n := node .(type ) {
82
- case * ast.FuncDecl :
83
- if n .Recv == nil {
84
- return nil // skip this func decl, not a method
54
+ for _ , decl := range file .AST .Decls {
55
+ funcDecl , ok := decl .(* ast.FuncDecl )
56
+ isMethod := ok && funcDecl .Recv != nil
57
+ if ! isMethod {
58
+ continue
85
59
}
86
60
87
- rec := n .Recv .List [0 ] // safe to access only the first (unique) element of the list
61
+ rec := funcDecl .Recv .List [0 ] // safe to access only the first (unique) element of the list
88
62
if len (rec .Names ) < 1 {
89
- return nil // the receiver is anonymous: func (aType) Foo(...) ...
63
+ continue // the receiver is anonymous: func (aType) Foo(...) ...
90
64
}
91
65
92
66
recID := rec .Names [0 ]
93
67
if recID .Name == "_" {
94
- return nil // the receiver is already named _
68
+ continue // the receiver is already named _
95
69
}
96
70
97
- if w .allowRegex != nil && w .allowRegex .FindStringIndex (recID .Name ) != nil {
98
- return nil
71
+ if r .allowRegex != nil && r .allowRegex .FindStringIndex (recID .Name ) != nil {
72
+ continue
99
73
}
100
74
101
75
// inspect the func body looking for references to the receiver id
102
- fselect := func (n ast.Node ) bool {
76
+ selectReceiverUses := func (n ast.Node ) bool {
103
77
ident , isAnID := n .(* ast.Ident )
104
78
105
79
return isAnID && ident .Obj == recID .Obj
106
80
}
107
- refs2recID := pick (n .Body , fselect )
81
+ receiverUses := pick (funcDecl .Body , selectReceiverUses )
108
82
109
- if len (refs2recID ) > 0 {
110
- return nil // the receiver is referenced in the func body
83
+ if len (receiverUses ) > 0 {
84
+ continue // the receiver is referenced in the func body
111
85
}
112
86
113
- w . onFailure ( lint.Failure {
87
+ failures = append ( failures , lint.Failure {
114
88
Confidence : 1 ,
115
89
Node : recID ,
116
90
Category : "bad practice" ,
117
- Failure : fmt .Sprintf (w .failureMsg , recID .Name ),
91
+ Failure : fmt .Sprintf (r .failureMsg , recID .Name ),
118
92
})
119
-
120
- return nil // full method body already inspected
121
93
}
122
94
123
- return w
95
+ return failures
96
+ }
97
+
98
+ // Name returns the rule name.
99
+ func (* UnusedReceiverRule ) Name () string {
100
+ return "unused-receiver"
124
101
}
0 commit comments