-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: cmd/vet: do not warn about nil comparisons after for loop #28871
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
Comments
How often will such a mistake make it through testing? If a developer adds a |
(CC @alandonovan @josharian @mvdan for |
I would argue that this is the checker working as intended. Much more often than not, a non-constant condition that is always true or false represents a misunderstanding about the local control flow. When a programmer adds a break statement, they're changing the control flow, and it's upon to them to evaluate the consequences of that change. |
One can also argue that the redundant form is much more resistant to accidental breakage. If Go had |
I haven't made up my mind about this issue, but I lean towards Dominik's opinion that vet shouldn't fight programmers that are being explicit with their invariants and pre/post conditions. Also, this issue reminds me of #28446. That was about a different check, but the underlying point is similar. Vet complains about an expression that is technically pointless, but in practice it serves a purpose - to help future contributors not break an invariant. |
Another step down the slippery slope: |
Per discussion with @golang/proposal-review, we agree the vet tool is working as intended with its current behavior. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
I see this issue with vet installed via:
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Run
vet .
on the following program:What did you expect to see?
No output
What did you see instead?
Discussion
Following the output of this tool, one might rewrite the program as:
And for now, the program remains correct.
However, if a developer were to add a break statement, such as:
Now, the program will incorrectly process an error, because the nil check was removed.
In my opinion, break is a first class feature of the language, and because of this, developers should not assume that the for loop condition is satisfied, just because control flow has proceeded past it. Following the advice of the vet tool in this instance has made the code more brittle.
NOTE: this example is silly, but it came up in real code here: blevesearch/bleve#1051
CC: @dgryski
The text was updated successfully, but these errors were encountered: