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: unable to detect unreachable code #16370

Closed
ghost opened this issue Jul 14, 2016 · 4 comments

Comments

Projects
None yet
3 participants
@ghost
Copy link

commented Jul 14, 2016

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
    go version go1.7rc1 windows/amd64
  2. What operating system and processor architecture are you using (go env)?
    set GOARCH=amd64
    set GOBIN=
    set GOEXE=.exe
    set GOHOSTARCH=amd64
    set GOHOSTOS=windows
    set GOOS=windows
    set GOPATH=C:/work/go/
    set GORACE=
    set GOROOT=c:\go
    set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
    set CC=gcc
    set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=c:\temp\go-build108228690=/tmp/go-build -gno-record-gcc-switches
    set CXX=g++
    set CGO_ENABLED=1
  3. What did you do?
    go tool vet -unreachable main.go
// main.go
package main
const flag = false
func main() {
    if flag {
        println("<<unreachable code>>")
    }
}
  1. What did you expect to see?
    a warning from vet
  2. What did you see instead?
    no warnings
@josharian

This comment has been minimized.

Copy link
Contributor

commented Jul 14, 2016

I don't think we'd want vet to complain about such code. That's a fairly typical way to add debugging code, particularly in hot functions where you can't accept any runtime overhead when not debugging.

@ghost

This comment has been minimized.

Copy link
Author

commented Jul 14, 2016

It turns out that it is impossible to determine what code was eliminated?

package main
const erraneous = (1 << 0) - 1
func main() {
    if erraneous == 1 {
            println("dead code")
    }
}

Code is silently eliminated by the compiler. And no warnings from vet.
Maybe it makes sense to add -report-eliminated-code flag to the compiler or vet? Just an idea.

@josharian

This comment has been minimized.

Copy link
Contributor

commented Jul 14, 2016

We try fairly strenuously to avoid flags.

@robpike

This comment has been minimized.

Copy link
Contributor

commented Jul 14, 2016

Working as intended, as @josharian explained.

@robpike robpike closed this Jul 14, 2016

@golang golang locked and limited conversation to collaborators Jul 14, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.