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/vet: Accidental shadowing suppressed due to different types #7258

Closed
gopherbot opened this issue Feb 5, 2014 · 4 comments
Closed

cmd/vet: Accidental shadowing suppressed due to different types #7258

gopherbot opened this issue Feb 5, 2014 · 4 comments

Comments

@gopherbot
Copy link

by marko@trustly.com:

Hi,

This simple program doesn't trigger shadowing warnings from go vet:

package main

func f() (string, error) {
    return "bar", nil
}

func main() {
    var c int
    var b int
    if true {
        c, _ := f()
        _ = c
        var b string
        _ = b
    }
    _ = c
    _ = b
}

However, I think it would be useful to complain about the variable  c  shadowing the
previous declaration; the fact that they are of different types isn't obvious when using
:=.  Attached is a patch to do that.  After applying, vet complains about  c, but not  b.

Any thoughts?

Attachments:

  1. shadow.patch (1449 bytes)
@cznic
Copy link
Contributor

cznic commented Feb 5, 2014

Comment 1:

Why should the declaration of c in the inner scope trigger a complain?

@adg
Copy link
Contributor

adg commented Feb 5, 2014

Comment 2:

It seems more useful to report shadowing of variables when the types *are* the same.
Have you run your patch against the standard library? How noisy are the reports?

@gopherbot
Copy link
Author

Comment 3 by marko@trustly.com:

If my understanding is correct, the check for different types is a blunt heuristic for
determining whether the shadowed variable would have worked in place of the shadowing
one or not.  I personally think it's way too blunt, but now after a night's sleep this
might not be good enough either.
I'll run it against the std lib and see what happens.

@rsc
Copy link
Contributor

rsc commented Mar 3, 2014

Comment 4:

Status changed to TimedOut.

@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
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