Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/go/analysis/passes/testinggoroutine: do not ignore calls in goroutines (false negative) #63849

Open
Antonboom opened this issue Oct 31, 2023 · 5 comments
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@Antonboom
Copy link

$ go vet -testinggoroutine some_test.go
func TestSomething(t *testing.T)
	go func() {
		helper(t)       // No warning :(
	}()
}

func helper(t *testing.T) {
        t.Helper()
	t.Fatal()
}
@bcmills bcmills changed the title go/analysis/passes/testinggoroutine: do not ignore calls in goroutines (false negative) golang.org/x/tools/go/analysis/passes/testinggoroutine: do not ignore calls in goroutines (false negative) Oct 31, 2023
@bcmills bcmills added the Analysis Issues related to static analysis (vet, x/tools/go/analysis) label Oct 31, 2023
@findleyr
Copy link
Contributor

Thanks. We discussed this in triage: This could be done (via two passes over the package), but might not be worth the complexity. Seems like a low priority.

@seankhliao seankhliao changed the title golang.org/x/tools/go/analysis/passes/testinggoroutine: do not ignore calls in goroutines (false negative) x/tools/go/analysis/passes/testinggoroutine: do not ignore calls in goroutines (false negative) Oct 31, 2023
@gopherbot gopherbot added this to the Unreleased milestone Oct 31, 2023
@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 31, 2023
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/541155 mentions this issue: go/analysis/passes/testinggoroutine: report by enclosing regions

gopherbot pushed a commit to golang/tools that referenced this issue Nov 21, 2023
This adds support for t.Run() statements. The new version
performs two passes. The first pass collects all of the
`go callee` and `t.Run(name, callee)` statements. The second pass
inspects each callee FuncDecl or FuncLit for a call to a
tb.Forbidden() function.

When the enclosing callee function called from an t.Run() statement,
it reports when tb is declared outside of the body of the
enclosing function.

Fixes golang/go#63799
Updates golang/go#63849
Updates golang/go#48124

Change-Id: I10d5f2a0af9b985126dbfcc98eaab97757a7f71d
Reviewed-on: https://go-review.googlesource.com/c/tools/+/541155
TryBot-Result: Gopher Robot <gobot@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Tim King <taking@google.com>
@ccoVeille
Copy link

ccoVeille commented Sep 2, 2024

Change https://go.dev/cl/541155 mentions this issue: go/analysis/passes/testinggoroutine: report by enclosing regions

I found back this old issue, and found there was something made about this issue with 1733061

The code is still there
https://github.com/golang/tools/blob/master/go/analysis/passes/testinggoroutine/testinggoroutine.go

The commit only refers to "Updates #63849" it's not about fixing it.

So I'm curious, what did this commit was about in the context on this issue.

I'm asking because apparently the issue is still there (status open) and I can replicate the issue in the code.

Thanks

@ccoVeille
Copy link

ccoVeille commented Sep 2, 2024

Maybe here is the difference

This is detected via changes made with 1733061

func TestSomething(t *testing.T) {
	go fail(t)
}

func fail(tb *testing.T) {
	tb.Helper()

	tb.FailNow()
}

This is not detected

func TestSomething(t *testing.T) {
	go func() {
		fail(t)
	}()
}

func fail(tb *testing.T) {
	tb.Helper()
	tb.FailNow()
}

The second pattern (also the exact one reported in the example of this issue) is obviously the most frequent

@timothy-king
Copy link
Contributor

As you correctly point out, this example is not yet handled. So the issue has not yet been closed.

FWIW the first pattern is simpler to detect. If someone wants to contribute an extension to the checker to detect the second that is sufficiently accurate, performant, etc, they can contribute PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

7 participants