-
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: missing error assignment check #19727
Comments
In this example:
there are two arguable problems:
The discussion on this issue focuses on detecting (1), which is pretty tricky. An alternative would be to detect (2), namely code that throws errors away without making that clear (without using |
It's definitely tricky to be certain, but I have seen very few instances where code like the example was intended. Perhaps a solution to (2) would find a superset of these problems, in which case is it worth adding a simpler check in the meantime? |
Spotted it thanks to a proposal in golang/go#19727. Change-Id: I389a3fc0db3cf64fba41c3ecd70a236917ea8fa3 Reviewed-on: https://go-review.googlesource.com/41698 Run-TryBot: Sam Whited <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Sam Whited <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
See #20148. |
On hold for #20148. |
Spotted it thanks to a proposal in golang/go#19727. Change-Id: I389a3fc0db3cf64fba41c3ecd70a236917ea8fa3 Reviewed-on: https://go-review.googlesource.com/41698 Run-TryBot: Sam Whited <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Sam Whited <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
Spotted it thanks to a proposal in golang/go#19727. Change-Id: I389a3fc0db3cf64fba41c3ecd70a236917ea8fa3 Reviewed-on: https://go-review.googlesource.com/41698 Run-TryBot: Sam Whited <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Sam Whited <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
Spotted it thanks to a proposal in golang/go#19727. Change-Id: I389a3fc0db3cf64fba41c3ecd70a236917ea8fa3 Reviewed-on: https://go-review.googlesource.com/41698 Run-TryBot: Sam Whited <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Sam Whited <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
Spotted it thanks to a proposal in golang/go#19727. Change-Id: I389a3fc0db3cf64fba41c3ecd70a236917ea8fa3 Reviewed-on: https://go-review.googlesource.com/41698 Run-TryBot: Sam Whited <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Sam Whited <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
Spotted it thanks to a proposal in golang/go#19727. Change-Id: I389a3fc0db3cf64fba41c3ecd70a236917ea8fa3 Reviewed-on: https://go-review.googlesource.com/41698 Run-TryBot: Sam Whited <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Sam Whited <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
I noticed a mistake during a code review that I think could be automatically detected. The idea is to detect code which looks like:
which should instead assign the return value of f()
For the code to compile at all, err must be declared in the outer scope.
Correctness:
This check detects code that the programmer didn't intend to write. In some instances the mistake is caused by what I personally might call a readability issue, such as when f() is a multi-line func literal that obscures the if statement. Nevertheless, this reports on the missing assignment, not the style.
Frequency:
A number of these have been detected at Google. The check is lightweight. I did not do a large review of open-source Go, but examples can be seen at:
https://github.com/golang/crypto/blob/459e26527287adbc2adcc5d0d49abff9a5f315a7/acme/acme.go#L157
https://github.com/tensorflow/tensorflow/blob/c44601f5411408c324c81dd38bc8686cbd2568aa/tensorflow/go/tensor.go#L101
Precision:
It is difficult to define a false negative for this test, but for my particular implementation perhaps errors that are not called "err", and are not checked against nil. Others implementations may be able to increase the scope.
False positives seem rare, though it is possible to write code like:
I do not think it would any less clear for f() to return err in that case though.
A false positive of my initial implementation is
https://github.com/ziutek/mymysql/blob/0582bcf675f52c0c2045c027fd135bd726048f45/autorc/autorecon.go#L124
this could be eliminated by looking for err (or &err) being passed as a parameter, though that function could avoid modifying its input parameter and instead return an error.
A more thorough check for if conditions that are always true/false would detect some of these situations, especially if a previous check for nil caused a return. But it would miss some, so I think a simple check is worth introducing. Some mistakes occur in tests:
whilst others aggregate errors rather than returning:
An initial implementation is here: https://go-review.googlesource.com/c/38631/
The text was updated successfully, but these errors were encountered: