-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: cmd/vet: detect values assigned but not used #23142
Comments
Please show complete code. Is that a new err variable or an existing err? Maybe only body is new in that snippet. If so, that's a different feature request. |
|
Hello @bradfitz thanks for the quick reply. The second to the last line of the function has err redeclared. Was expecting that running go vet will flag it when there are no usage of err var after it got redeclared. |
The That is, your bug report or feature request is equivalent to: package vettest
func foo() {
a := 1
if a == 2 {
// ...
}
a = 3 // no vet warning here
} |
FTR, staticcheck as well as ineffassign catch this. |
Moved this into the proposal process to replace #40286.
but it does not flag the first assignment in:
nor the second in:
Vet could flag both of those cases (for all types of variables, not just errors). |
What's needed here is a simple implementation and then some info about whether there are false positives or just too many reports on existing Go code. |
@spf13 points out that some people like to write
instead of
and that we probably should not flag lack-of-use for a := declaration+assignment that is assigning a zero value. |
I initialize a slice index to
|
Does anyone want to try writing this check and see how many false positives it has? |
In theory this seems like a good idea, but there are many possible implementations with subtle differences. |
There are already two different implementations: staticcheck and ineffassign. In my experience, they have almost no false positives, or no worse than anything reported by |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?go1.9 darwin/amd64
Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
on MacOS X Sierra
What did you do?
I wrote this code below and then run go vet. err is never used or checked.
What did you expect to see?
I expect that when I run $> go vet then I should see some error
notification that this err variable is not used or something.
What did you see instead?
None.
The text was updated successfully, but these errors were encountered: