-
Notifications
You must be signed in to change notification settings - Fork 17.3k
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
all: redundant or missing nil checks #30208
Comments
Please go ahead ! Thanks. |
Local variable err is defined with nil value and almost immediately compared with nil. This comparison is redundant as always evaluates to true. Updates golang#30208
Comparing err variable to be not nil is redundant in this case. The code above ensures that it is always not nil. Updates golang#30208
The variable err could have nil value when we call err.Error(), because after we check it for nil above we continue the test (t.Errorf doesn't stop the test execution). Updates golang#30208
The variable err could have nil value when we call err.Error(), because after we check it for nil above we continue the test (t.Errorf doesn't stop the test execution). Updates golang#30208
Change https://golang.org/cl/162438 mentions this issue: |
Change https://golang.org/cl/162439 mentions this issue: |
Change https://golang.org/cl/162477 mentions this issue: |
Change https://golang.org/cl/162457 mentions this issue: |
The new data-flow based nilness check in
but I'm not aware of a tool that handles the third one. Nifty! |
@octomarat please send a single PR for all these changes. Separate commits aren't necessary and just clutter the git history, in particular since most of the changes only touch test code. |
Change https://golang.org/cl/162657 mentions this issue: |
The variable err could have nil value when we call err.Error(), because after we check it for nil above we continue the test (t.Errorf doesn't stop the test execution). Updates #30208 Change-Id: Ibcf38698326c69c06068989510311e37806995c6 GitHub-Last-Rev: 3ab20f6 GitHub-Pull-Request: #30214 Reviewed-on: https://go-review.googlesource.com/c/162457 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
Comparing err variable to be not nil is redundant in this case. The code above ensures that it is always not nil. Updates #30208 Change-Id: I0a41601273de36a05d22270a743c0bdedeb1d0bf GitHub-Last-Rev: 372e0fd GitHub-Pull-Request: #30213 Reviewed-on: https://go-review.googlesource.com/c/162439 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
The variable err could have nil value when we call err.Error(), because after we check it for nil above we continue the test (t.Errorf doesn't stop the test execution). Updates #30208 Change-Id: I6f7a8609f2453f622a1fa94a50c99d2e04d5fbcd GitHub-Last-Rev: 3a5d9b1 GitHub-Pull-Request: #30215 Reviewed-on: https://go-review.googlesource.com/c/162477 Reviewed-by: Bryan C. Mills <bcmills@google.com> Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
@octomarat - I think only the cmd/go/internal fix is remaining. Would you like to push ahead and finish this one out ? |
Oh. I haven't noticed that some of the commits have been accepted and merged to the master already. Probably I am missing something, but what should I do to make the last one accepted and merged? |
Hi! I am writing a static analysis tool to detect problems with
nil
values in Go programs. I have executed it on the source code of the Go itself and found some minor issues.Redundant
err == nil
check:https://github.com/golang/go/blob/master/src/cmd/go/internal/work/exec.go#L106
err
is a local variable created withnil
value and it is not updated before the check (making it redundant).Redundant
err != nil
check:https://github.com/golang/go/blob/master/src/crypto/x509/name_constraints_test.go#L2223
err
is always notnil
because of the continue statement a couple of lines above.Potential 'nil pointer dereference':
https://github.com/golang/go/blob/master/src/net/textproto/reader_test.go#L335
https://github.com/golang/go/blob/master/src/html/template/escape_test.go#L1873
In both cases,
err
can benil
makingerr.Error()
call dangerous.err == nil
check above doesn't stop the execution of the test.I already have a fork with these issues fixed, will be happy to submit a pull request.
The text was updated successfully, but these errors were encountered: