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

proposal: cmd/vet: Better unused variable handling #59660

Closed
xbt573 opened this issue Apr 16, 2023 · 1 comment
Closed

proposal: cmd/vet: Better unused variable handling #59660

xbt573 opened this issue Apr 16, 2023 · 1 comment

Comments

@xbt573
Copy link

xbt573 commented Apr 16, 2023

The fact that Go compiler treats unused variables as error is cool, but it's annoying, so many developers uses code like this:

x := dummy() // get variable
_ = x        // assign to void to suppress error

If it's a temporary solution it is OK, but if variable really not used anywhere, it can lead to unnecessary resource or time consumption. For example:

func dummy() int { // dummy simulates long running operation
    time.Sleep(time.Second * 5)
    return random.Int()
}

func main() {
    x := dummy()
    _ = x

    fmt.Println("Hello World!")
}
xbt573 $ go run main.go
... waits 5 seconds ...
Hello World!

Therefore, i propose some changes to go vet:

  1. Warn user if variable used or assigned only to void register
  2. Warn user if variable used, but there is a unnecessary void register assign
  3. Warn user if all of function return values are assigned to void register

It will help remove really unused variables and unnecessary void register assigns

@gopherbot gopherbot added this to the Proposal milestone Apr 16, 2023
@seankhliao seankhliao changed the title proposal: all: Better unused variable handling proposal: cmd/vet: Better unused variable handling Apr 16, 2023
@seankhliao
Copy link
Member

The blank identifier is the standard way of indicating that a value is not to be used. It seems counter productive to make vet warn on it.
1 is intentional, 2 does not seem to a frequent issue, nor does it affect correctness. 3 will cause serious churn for projects that use things like https://github.com/kisielk/errcheck

Given the current state of the ecosystem, I don't think this meets the bar for frequency or precision for cmd/vet.
It may make sense as an external linter.

See also #20803

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Apr 16, 2023
@golang golang locked and limited conversation to collaborators Apr 15, 2024
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

3 participants