Skip to content

cmd/vet: shadowed variables check doesn't catch shadowing from short assignment in if statements #14494

Closed
@stevekuznetsov

Description

@stevekuznetsov

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions