-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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
cmd/vet: should check unreachable code after t.FailNow or related functions #53968
Comments
Worth noting that I believe @dominikh's staticcheck has done this for a while. |
Only as a side-effect in other checks, and in the case of Skip, much to my users' dismay. Doing this for SkipNow is definitely a bad idea. Skip is used to temporarily skip tests. The code following it is intentionally unreachable. |
You mean SA2002 Called testing.T.FailNow or SkipNow in a goroutine, which isn’t allowed?
Thanks @dominikh You are right, I just updated the description above. Skip should be ignored and the point of discussion should be FailNow. A practical example comes from the kubernetes tests. kubernetes project has used govet and golangci-lint for static detection. |
The unreachable checker needs to know the "noreturned"-ness of function/method calls. To have this the checker either needs to special case all of the std library cases ( If we do summarize, we should consider rewriting "unreachable" using ctrlflow. There is an old TODO to do this. |
Analyzing functions runs the risk of causing false positives in the presence of build tags. For example when a function is implemented as a panicking stub only under some build tags. This has been a real source of false positives in Staticcheck, which uses the There are ways to work around it, but they all introduce additional complexity. |
Interesting. I would have guessed these are true positives for the current build tag though (with false negatives for other build tags.) Maybe I am just not putting the pieces together. Do you have any examples? |
In the strictest sense they are true positives, yes. But I'd argue that code shouldn't be flagged as unreachable unless it's unreachable for all build tags. If you have
then telling the user that And if we make use of the information in ctrlflow or SSA then we can get follow-on effects. Consider a check that flags unnecessary variable assignments and the following code:
the assignment is useless for some build tags, but not for all build tags, and we can again not remove it. That last example is derived from dominikh/go-tools#787. When it comes to flagging dead/ineffective code, we IMO have to determine liveness by considering the union of all possible builds. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
should check unreachable code after t.SkipNow or t.FailNow or related functions.
I'm not quite sure if this is a bug or if it's designed that way.
If it's a bug, I'd be happy to fix it and make it my first commit to the golang language.
example:
A practical example comes from the kubernetes tests. kubernetes project has used govet and golangci-lint for static detection.
https://github.com/kubernetes/kubernetes/blob/8b16a4a4df64d13c0e01015820de0245f493e90c/staging/src/k8s.io/apiserver/pkg/endpoints/apiserver_test.go#L2841-L2848
What did you expect to see?
What did you see instead?
The text was updated successfully, but these errors were encountered: