Skip to content

cmd/compile: unused variable is not caught if it's assigned in a function closure #16289

@songgao

Description

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions