AI Writes Dead Code? The Go Team's deadcode Tool Finds It in One Command

✍️ OpenClawRadar📅 Published: August 18, 2026🔗 Source
Ad

AI coding agents are great at generating code, but they're also great at leaving dead code behind — unused functions, unreachable branches, and orphaned helpers that bloat your codebase and confuse future readers. The Go team's deadcode tool finds it in one command.

What is deadcode?

deadcode is an official Go tool (part of golang.org/x/tools) that performs whole-program unreachability analysis. It reports functions and methods that can never be called from any entry point of your program. It's not just a linter — it does actual control-flow and pointer analysis to determine reachability.

How to find AI-written dead code

Run this from your module root:

go run golang.org/x/tools/cmd/deadcode@latest ./...

That scans your entire module and prints a list of unreachable functions, for example:

deadcode: unreachable: main/helpers.go:23:7:func helperNeverCalled()

You can also pass specific packages or flags:

  • -test: include test files in the analysis
  • -filter: only report functions matching a regex (e.g., -filter '_dead')
  • -json: output as JSON for integration with CI tools
Ad

Why your AI agent leaves dead code

LLM agents generate code probabilistically — they often write utility functions they never call, or back up a refactor with a copy of the old code. Since the code compiles and tests pass, it's easy to miss. deadcode catches it deterministically.

Removing dead code safely

After running deadcode, review each unreachable function manually. A function might be unreachable because it's used via reflection, invoked from go:linkname, or part of a public API — the tool has a -reflect flag to account for reflection. But in most cases, if it's not called from main (or tests), you can safely delete it.

Then run go test ./... to confirm nothing broke. Dead code elimination is one of the easiest wins for codebase hygiene — and it makes your agent's output cleaner for the next iteration.

📖 Read the full source: HN AI Agents

Ad

👀 See Also