Skip to content
Latchkey

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 code

Common 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

  1. Look at the statement vet points to and the terminating statement above it.
  2. Delete the dead code, or move the early return so the later code runs when intended.
  3. Re-run go vet ./....
handler.go
if err != nil {
	return err
}
log.Println("done") // now reachable

Make 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →