Skip to content

Commit

Permalink
go/analysis/passes/testinggoroutine: fix panic in goStmtFun
Browse files Browse the repository at this point in the history
For function declared in other files, the identifier denoted function
will be nil, cause the analysis panics. To fix this, we just skip that
identifier for now, until golang/go#48141 is resolved.

Fixes golang/go#48124

Change-Id: I87876505ee5964639ed3d1772d541c00d091ceb6
Reviewed-on: https://go-review.googlesource.com/c/tools/+/347089
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Tim King <taking@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
  • Loading branch information
Cuong Manh Le authored and cuonglm committed Sep 4, 2021
1 parent d39bbca commit 3604566
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions go/analysis/passes/testinggoroutine/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func f(t *testing.T, _ string) {
t.Fatal("TestFailed")
}

func g() {}

func TestBadFatalIssue47470(t *testing.T) {
go f(t, "failed test 1") // want "call to .+T.+Fatal from a non-test goroutine"

Expand Down Expand Up @@ -272,3 +274,7 @@ func TestWithCustomType(t *testing.T) {
}(i)
}
}

func TestIssue48124(t *testing.T) {
go h()
}
7 changes: 7 additions & 0 deletions go/analysis/passes/testinggoroutine/testdata/src/a/b.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package a

func h() {}
4 changes: 4 additions & 0 deletions go/analysis/passes/testinggoroutine/testinggoroutine.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func goStmtFun(goStmt *ast.GoStmt) ast.Node {
switch goStmt.Call.Fun.(type) {
case *ast.Ident:
id := goStmt.Call.Fun.(*ast.Ident)
// TODO(cuonglm): improve this once golang/go#48141 resolved.
if id.Obj == nil {
break
}
if funDecl, ok := id.Obj.Decl.(ast.Node); ok {
return funDecl
}
Expand Down

0 comments on commit 3604566

Please sign in to comment.