Go version
go1.24.4
Output of go env
in your module/workspace:
AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/apocalypse555/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/apocalypse555/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1252593989=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/home/apocalypse555/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/apocalypse555/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/apocalypse555/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24.4'
GOWORK=''
PKG_CONFIG='pkg-config'
What did you do?
I run go vet
on this code :
func ok(a int) bool {
if a < 10 {
//panic(`with that, it's well detected`)
os.Exit(1)
return false
}
return true
}
(https://go.dev/play/p/CHtVr0fs-yw)
What did you see happen?
No warning like with panic()
.
What did you expect to see?
A warning like with panic()
.
It seems odd that an unreachable code is detected after a panic()
but not after an os.Exit()
.
Related issue
https://github.com/golang/go/issues/53968
Comment From: randall77
Adding return false
is required in many cases to adhere to the language spec on terminating statements.
func f(b bool) int {
if b {
return 5
} else {
os.Exit(1)
return 0 // doesn't compile without this line
}
}
The language spec doesn't say that os.Exit
is a terminating statement. I'm not sure it should, as the spec doesn't mention specific packages very often. And changing the definition of terminating statement now would break existing programs.
I suppose vet could find code like the code in your case, where removing it doesn't invalidate the program. That's an extra complication, and I'm not sure how often such a constrained vet rule would trigger.
Comment From: cagedmantis
Comment From: adonovan
I did an experiment a long time ago to make cmd/vet smarter about dead code following calls to "no-return" functions. The ctrlflow analyzer already knows about such functions, so basing unreachable
atop ctrflow
would do most of the heavy lifting. I just shared some old prototype code in https://go.dev/cl/687635, but it needs some care to make it production-worthy.
I think the ideal behavior would be for the analyzer to offer a fix to turn any statement following a noreturn call into panic("unreachable")
.
Comment From: gopherbot
Change https://go.dev/cl/687635 mentions this issue: go/analysis/passes/unreachable: use ctrlflow analyzer