Skip to content
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/go: 'go vet --shadow' not detecting shadowed variable #21638

Closed
leonklingele opened this issue Aug 26, 2017 · 3 comments
Closed

cmd/go: 'go vet --shadow' not detecting shadowed variable #21638

leonklingele opened this issue Aug 26, 2017 · 3 comments

Comments

@leonklingele
Copy link
Contributor

What version of Go are you using (go version)?

1.9

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

darwin/amd64, linux/amd64

Steps to reproduce

$ git clone https://github.com/leonklingele/pick -b fix-var-shadowing
$ cd pick
$ go vet --shadow ./... 2>&1 | grep "safe/note.go"

Actual & expect result

go vet correctly reports three shadowed vars (in line 134, 142, 145, the ones marked with // nolint: vetshadow), but misses one in line 152: https://github.com/leonklingele/pick/blob/fix-var-shadowing/safe/note.go#L152

I tried to create a smaller reproducible, but wasn't able to achieve this.

@robpike
Copy link
Contributor

robpike commented Aug 27, 2017

The -shadow option on vet is documented to be poor; that's why it's not enabled by default. There are many outstanding bugs against it and maybe someday someone will redo it with a better algorithm to make it more robust.

Regardless, line 152 isn't really shadowing, since the variable it creates doesn't hide one earlier in any semantically detectable way. In other words, although it has the same name as the outer err, renaming it (or using '=' instead of ':=') would have no effect on the program.

@leonklingele
Copy link
Contributor Author

How is line 152 any different than 134, 142 or 145 which are detected by --shadow? There is at least some inconsistency.

@ianlancetaylor
Copy link
Contributor

The -shadow warning is documented to only be issued if the shadowed variable is used after the shadowing variable is declared. That does not happen at line 152.

Closing because the tool is acting as documented.

@golang golang locked and limited conversation to collaborators Aug 28, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants