@@ -108,7 +108,7 @@ 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
- visitLast ( pass , body .List , func (last ast.Stmt ) {
111
+ forEachLastStmt ( body .List , func (last ast.Stmt ) {
112
112
var stmts []ast.Stmt
113
113
switch s := last .(type ) {
114
114
case * ast.GoStmt :
@@ -169,13 +169,11 @@ func reportCaptured(pass *analysis.Pass, vars []types.Object, checkStmt ast.Stmt
169
169
})
170
170
}
171
171
172
- // visitLast calls f on last go, defer and errgroup.Group.Go
173
- // statements in stmts, where "last" is defined recursively.
174
- //
175
- // For example, if the last statement in stmts is a switch statement, then the
176
- // last statements in each of the case clauses are also visited to examine their
177
- // last statements. See the documentation string at the top of this file for an example.
178
- func visitLast (pass * analysis.Pass , stmts []ast.Stmt , f func (last ast.Stmt )) {
172
+ // forEachLastStmt calls onLast on each "last" statement in a list of statements.
173
+ // "Last" is defined recursively so, for example, if the last statement is
174
+ // a switch statement, then each switch case is also visited to examine
175
+ // its last statements.
176
+ func forEachLastStmt (stmts []ast.Stmt , onLast func (last ast.Stmt )) {
179
177
if len (stmts ) == 0 {
180
178
return
181
179
}
@@ -185,10 +183,10 @@ func visitLast(pass *analysis.Pass, stmts []ast.Stmt, f func(last ast.Stmt)) {
185
183
case * ast.IfStmt :
186
184
loop:
187
185
for {
188
- visitLast ( pass , s .Body .List , f )
186
+ forEachLastStmt ( s .Body .List , onLast )
189
187
switch e := s .Else .(type ) {
190
188
case * ast.BlockStmt :
191
- visitLast ( pass , e .List , f )
189
+ forEachLastStmt ( e .List , onLast )
192
190
break loop
193
191
case * ast.IfStmt :
194
192
s = e
@@ -197,29 +195,26 @@ func visitLast(pass *analysis.Pass, stmts []ast.Stmt, f func(last ast.Stmt)) {
197
195
}
198
196
}
199
197
case * ast.ForStmt :
200
- visitLast ( pass , s .Body .List , f )
198
+ forEachLastStmt ( s .Body .List , onLast )
201
199
case * ast.RangeStmt :
202
- visitLast ( pass , s .Body .List , f )
200
+ forEachLastStmt ( s .Body .List , onLast )
203
201
case * ast.SwitchStmt :
204
202
for _ , c := range s .Body .List {
205
- if c , ok := c .(* ast.CaseClause ); ok {
206
- visitLast (pass , c .Body , f )
207
- }
203
+ cc := c .(* ast.CaseClause )
204
+ forEachLastStmt (cc .Body , onLast )
208
205
}
209
206
case * ast.TypeSwitchStmt :
210
207
for _ , c := range s .Body .List {
211
- if c , ok := c .(* ast.CaseClause ); ok {
212
- visitLast (pass , c .Body , f )
213
- }
208
+ cc := c .(* ast.CaseClause )
209
+ forEachLastStmt (cc .Body , onLast )
214
210
}
215
211
case * ast.SelectStmt :
216
212
for _ , c := range s .Body .List {
217
- if c , ok := c .(* ast.CommClause ); ok {
218
- visitLast (pass , c .Body , f )
219
- }
213
+ cc := c .(* ast.CommClause )
214
+ forEachLastStmt (cc .Body , onLast )
220
215
}
221
- case * ast. GoStmt , * ast. DeferStmt , * ast. ExprStmt :
222
- f (s )
216
+ default :
217
+ onLast (s )
223
218
}
224
219
}
225
220
0 commit comments