@@ -121,7 +121,20 @@ func run(cfg WrapcheckConfig) func(*analysis.Pass) (interface{}, error) {
121
121
}
122
122
123
123
for _ , file := range pass .Files {
124
+ // Keep track of parents so that can can traverse upwards to check for
125
+ // FuncDecls and FuncLits.
126
+ var parents []ast.Node
127
+
124
128
ast .Inspect (file , func (n ast.Node ) bool {
129
+ if n == nil {
130
+ // Pop, since we're done with this node and its children.
131
+ parents = parents [:len (parents )- 1 ]
132
+ } else {
133
+ // Push this node on the stack, since its children will be visited
134
+ // next.
135
+ parents = append (parents , n )
136
+ }
137
+
125
138
ret , ok := n .(* ast.ReturnStmt )
126
139
if ! ok {
127
140
return true
@@ -137,6 +150,17 @@ func run(cfg WrapcheckConfig) func(*analysis.Pass) (interface{}, error) {
137
150
// to handle it by checking the return params of the function.
138
151
retFn , ok := expr .(* ast.CallExpr )
139
152
if ok {
153
+ // If you go up, and the parent is a FuncLit, then don't report an
154
+ // error as you are in an anonymous function. If you are inside a
155
+ // FuncDecl, then continue as normal.
156
+ for i := len (parents ) - 1 ; i > 0 ; i -- {
157
+ if _ , ok := parents [i ].(* ast.FuncLit ); ok {
158
+ return true
159
+ } else if _ , ok := parents [i ].(* ast.FuncDecl ); ok {
160
+ break
161
+ }
162
+ }
163
+
140
164
// If the return type of the function is a single error. This will not
141
165
// match an error within multiple return values, for that, the below
142
166
// tuple check is required.
0 commit comments