Skip to content

Commit 4d2571b

Browse files
committed
go/analysis/passes/testinggoroutine: update for generics
Update the testinggoroutine analyzer to find bad calls via instantiated function calls. For golang/go#48704 Change-Id: I39ef43090a0e5dd04dfe094376517ac25441ad27 Reviewed-on: https://go-review.googlesource.com/c/tools/+/359401 Trust: Robert Findley <[email protected]> Trust: Zvonimir Pavlinovic <[email protected]> Run-TryBot: Robert Findley <[email protected]> Run-TryBot: Zvonimir Pavlinovic <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Zvonimir Pavlinovic <[email protected]>
1 parent 96715ad commit 4d2571b

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package typeparams
6+
7+
import (
8+
"testing"
9+
)
10+
11+
func f[P any](t *testing.T) {
12+
t.Fatal("failed")
13+
}
14+
15+
func TestBadFatalf[P any](t *testing.T) {
16+
go f[int](t) // want "call to .+T.+Fatal from a non-test goroutine"
17+
}

go/analysis/passes/testinggoroutine/testinggoroutine.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"golang.org/x/tools/go/analysis/passes/inspect"
1212
"golang.org/x/tools/go/analysis/passes/internal/analysisutil"
1313
"golang.org/x/tools/go/ast/inspector"
14+
"golang.org/x/tools/internal/typeparams"
1415
)
1516

1617
const Doc = `report calls to (*testing.T).Fatal from goroutines started by a test.
@@ -124,16 +125,30 @@ func typeIsTestingDotTOrB(expr ast.Expr) (string, bool) {
124125
// function literals declared in the same function, and
125126
// static calls within the same package are supported.
126127
func goStmtFun(goStmt *ast.GoStmt) ast.Node {
127-
switch goStmt.Call.Fun.(type) {
128-
case *ast.Ident:
129-
id := goStmt.Call.Fun.(*ast.Ident)
130-
// TODO(cuonglm): improve this once golang/go#48141 resolved.
128+
switch fun := goStmt.Call.Fun.(type) {
129+
case *ast.IndexExpr, *typeparams.IndexListExpr:
130+
ix := typeparams.GetIndexExprData(fun)
131+
if ix == nil {
132+
break
133+
}
134+
id, _ := ix.X.(*ast.Ident)
135+
if id == nil {
136+
break
137+
}
131138
if id.Obj == nil {
132139
break
133140
}
134141
if funDecl, ok := id.Obj.Decl.(ast.Node); ok {
135142
return funDecl
136143
}
144+
case *ast.Ident:
145+
// TODO(cuonglm): improve this once golang/go#48141 resolved.
146+
if fun.Obj == nil {
147+
break
148+
}
149+
if funDecl, ok := fun.Obj.Decl.(ast.Node); ok {
150+
return funDecl
151+
}
137152
case *ast.FuncLit:
138153
return goStmt.Call.Fun
139154
}

go/analysis/passes/testinggoroutine/testinggoroutine_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ import (
99

1010
"golang.org/x/tools/go/analysis/analysistest"
1111
"golang.org/x/tools/go/analysis/passes/testinggoroutine"
12+
"golang.org/x/tools/internal/typeparams"
1213
)
1314

1415
func Test(t *testing.T) {
1516
testdata := analysistest.TestData()
16-
analysistest.Run(t, testdata, testinggoroutine.Analyzer, "a")
17+
pkgs := []string{"a"}
18+
if typeparams.Enabled {
19+
pkgs = append(pkgs, "typeparams")
20+
}
21+
analysistest.Run(t, testdata, testinggoroutine.Analyzer, pkgs...)
1722
}

0 commit comments

Comments
 (0)