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

Comments

@Will2817
Copy link

@Will2817 Will2817 commented Jan 15, 2020

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
@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

@cagedmantis cagedmantis commented Jan 16, 2020

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants