Skip to content

Commit 7a7b699

Browse files
thepuddsgopherbot
authored andcommitted
go/analysis/passes/loopclosure: avoid panic in new parallel subtest check when t.Run has single argument
The new Go 1.20 parallel subtest check in the loopclosure pass can panic in the rare case of t.Run with a single argument: fn := func() (string, func(t *testing.T)) { return "", nil } t.Run(fn()) A real-world example: https://github.com/go-spatial/geom/blob/3cd2f5a9a082dd4f827c9f9b69ba5d736d2dcb12/planar/planar_test.go#L118 This has been present for multiple months without seeming to be reported, so presumably this is a rare pattern. Avoid that panic by checking t.Run has an expected number of arguments. Fixes golang/go#57908 Change-Id: I5cde60edf624e16bb9f534e435bcd55e63a15647 GitHub-Last-Rev: 24e65a4 GitHub-Pull-Request: #424 Reviewed-on: https://go-review.googlesource.com/c/tools/+/462282 Run-TryBot: thepudds <[email protected]> Reviewed-by: Russ Cox <[email protected]> Auto-Submit: Russ Cox <[email protected]> TryBot-Result: Gopher Robot <[email protected]> gopls-CI: kokoro <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent 3e6f71b commit 7a7b699

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

go/analysis/passes/loopclosure/loopclosure.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@ func parallelSubtest(info *types.Info, call *ast.CallExpr) []ast.Stmt {
303303
return nil
304304
}
305305

306+
if len(call.Args) != 2 {
307+
// Ignore calls such as t.Run(fn()).
308+
return nil
309+
}
310+
306311
lit, _ := call.Args[1].(*ast.FuncLit)
307312
if lit == nil {
308313
return nil

go/analysis/passes/loopclosure/testdata/src/subtests/subtest.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ func _(t *testing.T) {
140140
t.Parallel()
141141
println(test) // want "loop variable test captured by func literal"
142142
})
143+
144+
// Check that we do not have problems when t.Run has a single argument.
145+
fn := func() (string, func(t *testing.T)) { return "", nil }
146+
t.Run(fn())
143147
}
144148
}
145149

0 commit comments

Comments
 (0)