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: shadowed variables check doesn't catch shadowing from short assignment in if statements #14494

Closed
stevekuznetsov opened this issue Feb 24, 2016 · 4 comments

Comments

Projects
None yet
4 participants
@stevekuznetsov
Copy link

commented Feb 24, 2016

For the following example:

package main

import (
    "encoding/json"
    "io/ioutil"
    "log"
    "os"
)

func main() {
    if len(os.Args) != 2 {
        log.Fatal("Usage: jsonformat.go <filename>")
    }

    byt, err := ioutil.ReadFile(os.Args[1])
    if err != nil {
        log.Fatalf("ERROR: Unable to read file %q: %v\n", os.Args[1], err)
    }

    var dat map[string]interface{}

    if err := json.Unmarshal(byt, &dat); err != nil { // this err is not flagged as masking the err in the outer scope, incorrectly
        log.Fatalf("ERROR: Invalid JSON file  '%v': %v\n", os.Args[1], err)
    }

    if output, err := json.MarshalIndent(dat, "", "  "); err != nil { // this err is flagged as masking the err in the outer scope, correctly
        log.Fatalf("ERROR: Unable to indent JSON file: %v\n", os.Args[1])
    } else {
        os.Stdout.Write(append(output, '\n'))
    }
}

One the errors is not flagged correctly, even when using -shadowstrict. My version of go tool vet is at 108746816ddf01ad0c2dbea08a1baef08bc47313

@stevekuznetsov

This comment has been minimized.

Copy link
Author

commented Feb 24, 2016

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Feb 25, 2016

This is fixed in Go 1.6, though I do not know what fixed it.

@stevekuznetsov

This comment has been minimized.

Copy link
Author

commented Feb 25, 2016

I updated my version of go vet with a go get -u golang.org/x/tool/cmd/vet today... Should that not have picked up this change?

@kostya-sh

This comment has been minimized.

Copy link
Contributor

commented Feb 25, 2016

I believe this was fixed by 3c1712d

@stevekuznetsov, vet is part of Go tool set since Go 1.5. golang.org/x/tool/cmd/vet is no longer maintained.

I think golang.org/x/tool/cmd/vet should be deleted at some point as it creates a lot of confusion.

@golang golang locked and limited conversation to collaborators Feb 28, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.