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: new shadow false positive in 1.8 when using range iteration #18613

Closed
tamird opened this issue Jan 11, 2017 · 7 comments
Closed

cmd/vet: new shadow false positive in 1.8 when using range iteration #18613

tamird opened this issue Jan 11, 2017 · 7 comments

Comments

@tamird
Copy link
Contributor

tamird commented Jan 11, 2017

Given the following program:

package main

import (
	"context"
	"fmt"
	"strings"
)

func main() {
	ctx := context.Background()
	{
		// go1.7.4, go1.8rc1: declaration of "ctx" shadows declaration at shadow.go:9
		ctx, cancel := context.WithCancel(ctx)
		cancel()
	}
	fmt.Println(ctx)

	var strs []string
	for _, str := range strs {
		// go1.8rc1: declaration of "str" shadows declaration at shadow.go:19
		str := strings.Replace(str, "1", "2", -1)
	}
}

There are two go tool vet -shadow false positives present, but one of them is new in go1.8.

This code is also at https://github.com/tamird/vet-bugs. cc @petermattis

@bradfitz
Copy link
Contributor

/cc @josharian @robpike

@bradfitz
Copy link
Contributor

(I don't follow vet, but will conservatively mark this as Go1.8Maybe until owners say otherwise)

@bradfitz bradfitz added this to the Go1.8Maybe milestone Jan 11, 2017
@ianlancetaylor
Copy link
Contributor

I'm sure I'm missing something, but why is that a false positive? There is a str variable declared in the for/range statement, and it is shadowed by the str variable defined using := within the loop.

@robpike
Copy link
Contributor

robpike commented Jan 11, 2017

Both of those messages from vet are correct.

@robpike robpike closed this as completed Jan 11, 2017
@tamird
Copy link
Contributor Author

tamird commented Jan 11, 2017

I've updated the program below to demonstrate why this is a false positive. Note that the block

	var strs []string
	for _, str := range strs {
		_ = str

		str := "foo"
		_ = str
	}

does not produce a vet warning.

package main

import (
	"context"
	"strings"
)

func main() {
	ctx := context.Background()
	{
		// go1.7.4, go1.8rc1: declaration of "ctx" shadows declaration at shadow.go:9
		ctx, cancel := context.WithCancel(ctx)
		_ = ctx
		cancel()
	}

	var strs []string
	for _, str := range strs {
		_ = str

		// no warning
		str := "foo"
		_ = str
	}
	for _, str := range strs {
		// go1.8rc1: declaration of "str" shadows declaration at shadow.go:25
		str := strings.Replace(str, "1", "2", -1)
		_ = str
	}
}

Please reopen this issue. Either this is a false positive, or the behaviour is inconsistent.

@robpike
Copy link
Contributor

robpike commented Jan 11, 2017

The behavior is inconsistent but the documentation also says the check is experimental. Honestly, I'd rather delete it than fix it; the approach is all wrong.

@tamird
Copy link
Contributor Author

tamird commented Jan 11, 2017

I'd be OK with deleting it, for what it's worth. I believe tools like https://github.com/dominikh/go-staticcheck can fill in the gaps.

@golang golang locked and limited conversation to collaborators Jan 11, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants