go vet "unreachable code" in CI
The unreachable analyzer flags statements that follow a terminating statement (return, break, continue, panic, or an os.Exit call pattern) and therefore can never run.
What this error means
go vet reports "unreachable code" at a statement that sits after a return or panic.
go vet
./handler.go:20:2: unreachable codeCommon causes
Code after a return or panic
A statement was left below an unconditional return or panic, so control never reaches it.
A misplaced early return during a refactor
An early return was added or moved, stranding the code that followed it.
How to fix it
Remove or reorder the dead statement
- Look at the statement vet points to and the terminating statement above it.
- Delete the dead code, or move the early return so the later code runs when intended.
- Re-run
go vet ./....
handler.go
if err != nil {
return err
}
log.Println("done") // now reachableMake the prior statement conditional
If the earlier return should only happen sometimes, guard it with a condition so the following code is reachable.
How to prevent it
- Run
go vet ./...in CI to catch dead code. - Watch for stranded code when adding early returns.
- Keep functions small so control flow stays obvious.
Related guides
go vet "Printf arg of wrong type" in CIFix go vet "Printf format %d has arg of wrong type string" in CI - a format verb does not match the type of t…
go vet "the cancel function is not used on all paths" in CIFix go vet "the cancel function returned by context.WithCancel should be called, not discarded, to avoid a co…
go vet "struct field tag not compatible with reflect.StructTag.Get" in CIFix go vet "struct field tag not compatible with reflect.StructTag.Get: bad syntax for struct tag value" in C…