Skip to content

go build does not reject unreachable code after return keyword #72837

@chrbsg

Description

@chrbsg

Go version

1.23.2

Output of go env in your module/workspace:

GO111MODULE='on'
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/chrb/.cache/go-build'
GOENV='/home/chrb/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/chrb/go/pkg/mod'
GONOPROXY='github.com/SpeechGraphics/*'
GONOSUMDB='github.com/SpeechGraphics/*'
GOOS='linux'
GOPATH='/home/chrb/go'
GOPRIVATE='github.com/SpeechGraphics/*'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.2'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/chrb/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/chrb/go/src/test/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build175631584=/tmp/go-build -gno-record-gcc-switches'

What did you do?

go build accepts the following code without any warning or error:

package main

import (
        "log"
)

func f() int {
        return 1
        return 0
        x := 1
        log.Printf("%d", x)
        return 10
}

func main() {
        log.Printf("%d", f())
}

go vet does catch the unreachable code, but imho go build should also reject it.

Even go vet does not catch this unreachable code:

func main() {
        if false {
                log.Printf("hi")
        }
}

What did you see happen?

First example: go build compiled the unreachable code without any warnings or error

Second example: neither go build or go vet complained about the unreachable code

What did you expect to see?

Errors or warnings due to unreachable code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions