@@ -97,18 +97,21 @@ func (w lintDeferRule) Visit(node ast.Node) ast.Visitor {
97
97
w .newFailure ("return in a defer function has no effect" , n , 1.0 , "logic" , "return" )
98
98
}
99
99
case * ast.CallExpr :
100
- if ! w .inADefer && isIdent (n .Fun , "recover" ) {
100
+ isCallToRecover := isIdent (n .Fun , "recover" )
101
+ switch {
102
+ case ! w .inADefer && isCallToRecover :
101
103
// func fn() { recover() }
102
104
//
103
105
// confidence is not 1 because recover can be in a function that is deferred elsewhere
104
106
w .newFailure ("recover must be called inside a deferred function" , n , 0.8 , "logic" , "recover" )
105
- } else if w .inADefer && ! w .inAFuncLit && isIdent ( n . Fun , "recover" ) {
107
+ case w .inADefer && ! w .inAFuncLit && isCallToRecover :
106
108
// defer helper(recover())
107
109
//
108
110
// confidence is not truly 1 because this could be in a correctly-deferred func,
109
111
// but it is very likely to be a misunderstanding of defer's behavior around arguments.
110
112
w .newFailure ("recover must be called inside a deferred function, this is executing recover immediately" , n , 1 , "logic" , "immediate-recover" )
111
113
}
114
+
112
115
case * ast.DeferStmt :
113
116
if isIdent (n .Call .Fun , "recover" ) {
114
117
// defer recover()
@@ -119,7 +122,12 @@ func (w lintDeferRule) Visit(node ast.Node) ast.Visitor {
119
122
}
120
123
w .visitSubtree (n .Call .Fun , true , false , false )
121
124
for _ , a := range n .Call .Args {
122
- w .visitSubtree (a , true , false , false ) // check arguments, they should not contain recover()
125
+ switch a .(type ) {
126
+ case * ast.FuncLit :
127
+ continue // too hard to analyze deferred calls with func literals args
128
+ default :
129
+ w .visitSubtree (a , true , false , false ) // check arguments, they should not contain recover()
130
+ }
123
131
}
124
132
125
133
if w .inALoop {
@@ -136,6 +144,7 @@ func (w lintDeferRule) Visit(node ast.Node) ast.Visitor {
136
144
w .newFailure ("be careful when deferring calls to methods without pointer receiver" , fn , 0.8 , "bad practice" , "method-call" )
137
145
}
138
146
}
147
+
139
148
}
140
149
return nil
141
150
}
0 commit comments