@@ -87,54 +87,64 @@ type lintUnusedParamRule struct {
87
87
}
88
88
89
89
func (w lintUnusedParamRule ) Visit (node ast.Node ) ast.Visitor {
90
+ var (
91
+ funcType * ast.FuncType
92
+ funcBody * ast.BlockStmt
93
+ )
90
94
switch n := node .(type ) {
95
+ case * ast.FuncLit :
96
+ funcType = n .Type
97
+ funcBody = n .Body
91
98
case * ast.FuncDecl :
92
- params := retrieveNamedParams (n .Type .Params )
93
- if len (params ) < 1 {
94
- return nil // skip, func without parameters
95
- }
96
-
97
99
if n .Body == nil {
98
100
return nil // skip, is a function prototype
99
101
}
100
102
101
- // inspect the func body looking for references to parameters
102
- fselect := func (n ast.Node ) bool {
103
- ident , isAnID := n .(* ast.Ident )
103
+ funcType = n .Type
104
+ funcBody = n .Body
105
+ default :
106
+ return w // skip, not a function
107
+ }
104
108
105
- if ! isAnID {
106
- return false
107
- }
109
+ params := retrieveNamedParams (funcType .Params )
110
+ if len (params ) < 1 {
111
+ return w // skip, func without parameters
112
+ }
108
113
109
- _ , isAParam := params [ident .Obj ]
110
- if isAParam {
111
- params [ident .Obj ] = false // mark as used
112
- }
114
+ // inspect the func body looking for references to parameters
115
+ fselect := func (n ast.Node ) bool {
116
+ ident , isAnID := n .(* ast.Ident )
113
117
118
+ if ! isAnID {
114
119
return false
115
120
}
116
- _ = pick (n .Body , fselect )
117
-
118
- for _ , p := range n .Type .Params .List {
119
- for _ , n := range p .Names {
120
- if w .allowRegex .FindStringIndex (n .Name ) != nil {
121
- continue
122
- }
123
- if params [n .Obj ] {
124
- w .onFailure (lint.Failure {
125
- Confidence : 1 ,
126
- Node : n ,
127
- Category : "bad practice" ,
128
- Failure : fmt .Sprintf (w .failureMsg , n .Name ),
129
- })
130
- }
131
- }
121
+
122
+ _ , isAParam := params [ident .Obj ]
123
+ if isAParam {
124
+ params [ident .Obj ] = false // mark as used
132
125
}
133
126
134
- return nil // full method body already inspected
127
+ return false
128
+ }
129
+ _ = pick (funcBody , fselect )
130
+
131
+ for _ , p := range funcType .Params .List {
132
+ for _ , n := range p .Names {
133
+ if w .allowRegex .FindStringIndex (n .Name ) != nil {
134
+ continue
135
+ }
136
+ if params [n .Obj ] {
137
+ w .onFailure (lint.Failure {
138
+ Confidence : 1 ,
139
+ Node : n ,
140
+ Category : "bad practice" ,
141
+ Failure : fmt .Sprintf (w .failureMsg , n .Name ),
142
+ })
143
+ }
144
+ }
135
145
}
136
146
137
- return w
147
+ return w // full method body was inspected
138
148
}
139
149
140
150
func retrieveNamedParams (params * ast.FieldList ) map [* ast.Object ]bool {
0 commit comments