Skip to content

Commit 8478a24

Browse files
committed
go/analysis/passes/loopclosure: rename helper function back to visitLast, and update its function comment
1 parent 44200bf commit 8478a24

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

go/analysis/passes/loopclosure/loopclosure.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ 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-
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)
113113
}
114114

115115
// Also check for testing.T.Run (with T.Parallel).
@@ -155,12 +155,14 @@ func reportCaptured(pass *analysis.Pass, vars []types.Object, stmts ...ast.Stmt)
155155
}
156156
}
157157

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 {
164166
if len(stmts) == 0 {
165167
return nil
166168
}
@@ -171,34 +173,34 @@ func lastStmts(pass *analysis.Pass, stmts []ast.Stmt) []ast.Stmt {
171173
case *ast.IfStmt:
172174
var next *ast.IfStmt
173175
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)...)
175177
switch e := s.Else.(type) {
176178
case *ast.BlockStmt:
177-
res = append(res, lastStmts(pass, e.List)...)
179+
res = append(res, visitLast(pass, e.List)...)
178180
case *ast.IfStmt:
179181
next = e
180182
}
181183
}
182184
case *ast.ForStmt:
183-
res = append(res, lastStmts(pass, s.Body.List)...)
185+
res = append(res, visitLast(pass, s.Body.List)...)
184186
case *ast.RangeStmt:
185-
res = append(res, lastStmts(pass, s.Body.List)...)
187+
res = append(res, visitLast(pass, s.Body.List)...)
186188
case *ast.SwitchStmt:
187189
for _, c := range s.Body.List {
188190
if c, ok := c.(*ast.CaseClause); ok {
189-
res = append(res, lastStmts(pass, c.Body)...)
191+
res = append(res, visitLast(pass, c.Body)...)
190192
}
191193
}
192194
case *ast.TypeSwitchStmt:
193195
for _, c := range s.Body.List {
194196
if c, ok := c.(*ast.CaseClause); ok {
195-
res = append(res, lastStmts(pass, c.Body)...)
197+
res = append(res, visitLast(pass, c.Body)...)
196198
}
197199
}
198200
case *ast.SelectStmt:
199201
for _, c := range s.Body.List {
200202
if c, ok := c.(*ast.CommClause); ok {
201-
res = append(res, lastStmts(pass, c.Body)...)
203+
res = append(res, visitLast(pass, c.Body)...)
202204
}
203205
}
204206
case *ast.GoStmt:

0 commit comments

Comments
 (0)