Skip to content

Commit c62d9aa

Browse files
committed
go/analysis/passes/loopclosure: rename vistLast to forEachLastStmt, and drop knowledge of go/defer/errgroup from it
1 parent 67cacea commit c62d9aa

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

go/analysis/passes/loopclosure/loopclosure.go

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
108108
// statement, because it's hard to prove go isn't followed by wait, or
109109
// defer by return. "Last" is defined recursively, as described in the
110110
// 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) {
112112
var stmts []ast.Stmt
113113
switch s := last.(type) {
114114
case *ast.GoStmt:
@@ -169,13 +169,11 @@ func reportCaptured(pass *analysis.Pass, vars []types.Object, checkStmt ast.Stmt
169169
})
170170
}
171171

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)) {
179177
if len(stmts) == 0 {
180178
return
181179
}
@@ -185,10 +183,10 @@ func visitLast(pass *analysis.Pass, stmts []ast.Stmt, f func(last ast.Stmt)) {
185183
case *ast.IfStmt:
186184
loop:
187185
for {
188-
visitLast(pass, s.Body.List, f)
186+
forEachLastStmt(s.Body.List, onLast)
189187
switch e := s.Else.(type) {
190188
case *ast.BlockStmt:
191-
visitLast(pass, e.List, f)
189+
forEachLastStmt(e.List, onLast)
192190
break loop
193191
case *ast.IfStmt:
194192
s = e
@@ -197,29 +195,26 @@ func visitLast(pass *analysis.Pass, stmts []ast.Stmt, f func(last ast.Stmt)) {
197195
}
198196
}
199197
case *ast.ForStmt:
200-
visitLast(pass, s.Body.List, f)
198+
forEachLastStmt(s.Body.List, onLast)
201199
case *ast.RangeStmt:
202-
visitLast(pass, s.Body.List, f)
200+
forEachLastStmt(s.Body.List, onLast)
203201
case *ast.SwitchStmt:
204202
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)
208205
}
209206
case *ast.TypeSwitchStmt:
210207
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)
214210
}
215211
case *ast.SelectStmt:
216212
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)
220215
}
221-
case *ast.GoStmt, *ast.DeferStmt, *ast.ExprStmt:
222-
f(s)
216+
default:
217+
onLast(s)
223218
}
224219
}
225220

0 commit comments

Comments
 (0)