Skip to content

cmd/vet: shadow false positive when using exported function #11843

Open
@tamird

Description

@tamird

go tool vet --shadow returns a false positive when the variable being shadowed is declared from the return of an exported user function from another package. That's a mouthful, so here's some code:

./other/other.go:

package other

func Foo() (int, error) {
    return 0, nil
}

./shadow_test.go:

package shadow

import (
    "errors"
    "testing"

    "github.com/tamird/shadow/other"
)

func foo() (int, error) {
    return 0, nil
}

func TestVetShadow(t *testing.T) {
    // Local varibles: this passes.
    // a, err := "a", errors.New("Foo")

    // Function from same package: this passes.
    // a, err := foo()

    // Function from standard library: this passes.
    // r := &bytes.Buffer{}
    // a, err := r.Read(nil)

    // Function from different package: this triggers shadowing warning.
    a, err := other.Foo()

    if err != nil {

    }

    if _, err := other.Foo(); err != nil {
    }

    b, err := "b", errors.New("Foo")
    if err != nil {
    }

    _, _ = a, b
}

The comments in the code describe the problem. Use other.Foo() and you get a false positive; use any of the others (local function, local variables, exported method from the stdlib) and the warning goes away. Weird!

All the code is also available here: https://github.com/tamird/shadow

cc @mberhault

Metadata

Metadata

Assignees

No one assigned

    Labels

    AnalysisIssues related to static analysis (vet, x/tools/go/analysis)

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions