Skip to content

Follow a Go local with _ = name so an unread one compiles [patch] - #275

Merged
matt-edmondson merged 1 commit into
mainfrom
claude/coder-272-go-unused-local
Oct 19, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
claude/coder-272-go-unused-local

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #272

What changed

Go refuses a local that nothing reads ("declared and not used"). The other six targets accept a body that declares a local and never reads it, or only assigns to it, but the Go output for that body didn't compile.

  • GoGenerator.GenerateVariableDeclaration now writes _ = name after every local. This follows the precedent of the entry point's _ = args.
  • I took the simple version the triage recommended. Skipping the line for locals that are read later would need a read analysis, so every local gets it.
  • A package-level declaration doesn't get the line. Go allows an unread package variable and refuses a statement outside a function. A body-depth counter in WriteBody tells the two apart.

Tests

  • New test GoGeneratedSourceCompilesTests.UnreadLocals_Compile builds, with go build, a function with an unread typed local, one with an inferred local that is only assigned to plus a zero-valued local, and one whose local is read. It also checks the output against gofmt -l. With the generator change reverted, it fails with declared and not used: t, w and z.
  • The full suite passes: 1317/1317.

Generated by Claude Code

Go refuses a local nothing reads ("declared and not used"), and a body is
free to declare one it never reads or only assigns to, which the other six
targets accept. GoGenerator now writes `_ = name` after every local, the
same as the entry point's `_ = args`. A package-level declaration gets
none, since Go allows an unread one there and refuses a statement outside
a function; a body depth counter tells the two apart.

Fixes #272

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CFV1ukfTLyVhjxAWRuEzGD
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 9d12466 into main Oct 19, 2026
16 checks passed
@matt-edmondson
matt-edmondson deleted the claude/coder-272-go-unused-local branch October 19, 2026 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Go: a local that is declared but never read fails to compile ("declared and not used")

2 participants