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

New Go vet rule which detects overwrite of variable from the outer scope. #71555

Closed
guettli opened this issue Feb 4, 2025 · 2 comments
Closed
Labels
ToolProposal Issues describing a requested change to a Go tool or command-line program.

Comments

@guettli
Copy link

guettli commented Feb 4, 2025

I wrote that code, and I did not understand why path was empty, although findDev() returns a non-empty path:

func getDeviceFromPath(path string) (*evdev.InputDevice, error) {
	if path == "" {
		path, err := findDev()   // "path" of outer block gets overwritten
		if err != nil {
			return nil, err
		}
		fmt.Printf("Using device %q\n", path)
	}
	sourceDev, err := evdev.Open(path)

I would like to see a warning in vet, when I overwrite a variable from the outer scope.

This is a common mistake for people new to Go, but even people with experience sometimes do that mistake.

@firelizzard18 had some ideas about that new analyzer: golang/vscode-go#3674 (comment)

@gabyhelp gabyhelp added the ToolProposal Issues describing a requested change to a Go tool or command-line program. label Feb 4, 2025
@zigo101
Copy link

zigo101 commented Feb 4, 2025

related: #377

@guettli guettli changed the title New Go vet rule which detects New Go vet rule which detects overwrite of variable from the outer scope. Feb 4, 2025
@seankhliao
Copy link
Member

see #22582 and #29260

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ToolProposal Issues describing a requested change to a Go tool or command-line program.
Projects
None yet
Development

No branches or pull requests

4 participants