@@ -108,8 +108,8 @@ func run(pass *analysis.Pass) (interface{}, error) {
108
108
// statement, because it's hard to prove go isn't followed by wait, or
109
109
// defer by return. "Last" is defined recursively, as described in the
110
110
// documentation string at the top of this file.
111
- for _ , stmt := range lastStmts (pass , body .List ) {
112
- reportCaptured (pass , vars , stmt )
111
+ for _ , checkStmt := range visitLast (pass , body .List ) {
112
+ reportCaptured (pass , vars , checkStmt )
113
113
}
114
114
115
115
// Also check for testing.T.Run (with T.Parallel).
@@ -155,12 +155,14 @@ func reportCaptured(pass *analysis.Pass, vars []types.Object, stmts ...ast.Stmt)
155
155
}
156
156
}
157
157
158
- // lastStmts returns the last go, defer and errgroup.Group.Go statements in stmts,
159
- // where "last" is defined recursively. For example, if the last statement in stmts
160
- // is a switch statement, then the last statements in each of the case clauses
161
- // are also visited to examine their last statements. See the documentation string
162
- // at the top of this file for an example.
163
- func lastStmts (pass * analysis.Pass , stmts []ast.Stmt ) []ast.Stmt {
158
+ // visitLast returns all the statements from the bodies of any function literals
159
+ // used as the call expression in the last go, defer and errgroup.Group.Go
160
+ // statements in stmts, where "last" is defined recursively.
161
+ //
162
+ // For example, if the last statement in stmts is a switch statement, then the
163
+ // last statements in each of the case clauses are also visited to examine their
164
+ // last statements. See the documentation string at the top of this file for an example.
165
+ func visitLast (pass * analysis.Pass , stmts []ast.Stmt ) []ast.Stmt {
164
166
if len (stmts ) == 0 {
165
167
return nil
166
168
}
@@ -171,34 +173,34 @@ func lastStmts(pass *analysis.Pass, stmts []ast.Stmt) []ast.Stmt {
171
173
case * ast.IfStmt :
172
174
var next * ast.IfStmt
173
175
for ; s != nil ; s , next = next , nil {
174
- res = append (res , lastStmts (pass , s .Body .List )... )
176
+ res = append (res , visitLast (pass , s .Body .List )... )
175
177
switch e := s .Else .(type ) {
176
178
case * ast.BlockStmt :
177
- res = append (res , lastStmts (pass , e .List )... )
179
+ res = append (res , visitLast (pass , e .List )... )
178
180
case * ast.IfStmt :
179
181
next = e
180
182
}
181
183
}
182
184
case * ast.ForStmt :
183
- res = append (res , lastStmts (pass , s .Body .List )... )
185
+ res = append (res , visitLast (pass , s .Body .List )... )
184
186
case * ast.RangeStmt :
185
- res = append (res , lastStmts (pass , s .Body .List )... )
187
+ res = append (res , visitLast (pass , s .Body .List )... )
186
188
case * ast.SwitchStmt :
187
189
for _ , c := range s .Body .List {
188
190
if c , ok := c .(* ast.CaseClause ); ok {
189
- res = append (res , lastStmts (pass , c .Body )... )
191
+ res = append (res , visitLast (pass , c .Body )... )
190
192
}
191
193
}
192
194
case * ast.TypeSwitchStmt :
193
195
for _ , c := range s .Body .List {
194
196
if c , ok := c .(* ast.CaseClause ); ok {
195
- res = append (res , lastStmts (pass , c .Body )... )
197
+ res = append (res , visitLast (pass , c .Body )... )
196
198
}
197
199
}
198
200
case * ast.SelectStmt :
199
201
for _ , c := range s .Body .List {
200
202
if c , ok := c .(* ast.CommClause ); ok {
201
- res = append (res , lastStmts (pass , c .Body )... )
203
+ res = append (res , visitLast (pass , c .Body )... )
202
204
}
203
205
}
204
206
case * ast.GoStmt :
0 commit comments