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/compile: unused variable is not caught if it's assigned in a function closure #16289

Closed
songgao opened this issue Jul 7, 2016 · 3 comments

Comments

@songgao
Copy link

songgao commented Jul 7, 2016

  1. What version of Go are you using (go version)?
go version go1.6.2 linux/amd64
  1. What operating system and processor architecture are you using (go env)?
GOARCH="amd64"
GOBIN="/home/songgao/gopath/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/songgao/gopath"
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT="1"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
  1. What did you do?
songgao at Song-ThinkPad in /tmp 
$ cat main.go
package main

func main() {
    var a int
    a = 2
}

songgao at Song-ThinkPad in /tmp 
$ go run main.go
# command-line-arguments
./main.go:4: a declared and not used

songgao at Song-ThinkPad in /tmp 
$ vim main.go

songgao at Song-ThinkPad in /tmp 
$ cat main.go
package main

func main() {
    var a int
    func() {
        a = 2
    }()
}

songgao at Song-ThinkPad in /tmp 
$ go run main.go

songgao at Song-ThinkPad in /tmp 
$ echo $?
0
  1. What did you expect to see?

The declared but unused variable gets caught even if it's assigned in a function closure.

  1. What did you see instead?

Not caught.

P.S. Interestingly, the same error was successfully caught by :GoRename command (which uses gorename) in vim.

@bradfitz bradfitz added this to the Go1.8Maybe milestone Jul 7, 2016
@bradfitz bradfitz changed the title Unused variable is not caught if it's assigned in a function closure cmd/compile: unused variable is not caught if it's assigned in a function closure Jul 7, 2016
@bradfitz
Copy link
Contributor

bradfitz commented Jul 7, 2016

Technically this isn't a violation of the spec, which only says:

Implementation restriction: A compiler may make it illegal to declare a variable inside a function body if the variable is never used.

But I agree it'd be nice if our various tools had consistent behavior.

@griesemer
Copy link
Contributor

Duplicate of #3059.

@griesemer
Copy link
Contributor

gorename uses go/types which correctly (in my mind) reports this as an error.

@golang golang locked and limited conversation to collaborators Jul 7, 2017
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

4 participants