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

x/tools/go/analysis/passes/shadow: shadowing not detected on deferred usage #36581

Open
Will2817 opened this issue Jan 15, 2020 · 1 comment
Open
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@Will2817
Copy link

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

go version go1.13.4 darwin/amd64

What did you do?

ran

go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow
go vet -vettool=$(which shadow) main.go

on

package main

import "fmt"

func main() {
	err := fmt.Errorf("one")
	defer func() { fmt.Printf("deferred err is %v\n", err) }()
	{
		err := fmt.Errorf("two")
		fmt.Printf("err is %v\n", err)
	}
}

What did you expect to see?

./main.go:9:3: declaration of "err" shadows declaration at line 6

This is a simplified example but the intention of the defer is to print whatever the value of err is before returning. Since the err is redeclared in the new scope it shadows the initial declaration err. The defer then prints "deferred err is one" instead of the intended "deferred error is two".

What did you see instead?

No vet error

@cagedmantis cagedmantis changed the title cmd/vet shadowing not detected on deferred usage cmd/vet: shadowing not detected on deferred usage Jan 16, 2020
@cagedmantis cagedmantis changed the title cmd/vet: shadowing not detected on deferred usage x/tools/go/analysis/passes/shadow/cmd/shadow: shadowing not detected on deferred usage Jan 16, 2020
@gopherbot gopherbot added this to the Unreleased milestone Jan 16, 2020
@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Jan 16, 2020
@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 16, 2020
@cagedmantis cagedmantis changed the title x/tools/go/analysis/passes/shadow/cmd/shadow: shadowing not detected on deferred usage x/tools/go/analysis/passes/shadow: shadowing not detected on deferred usage Jan 16, 2020
@cagedmantis
Copy link
Contributor

/cc @ianthehat @matloob

@adonovan adonovan added the Analysis Issues related to static analysis (vet, x/tools/go/analysis) label Apr 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants