Skip to content

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

Closed

Description

@matt-edmondson

What's wrong

Go refuses any local variable that nothing reads. GoGenerator.GenerateVariableDeclaration (Coder/Languages/GoGenerator.cs ~L1327 onward) writes var t int = 3 or t := … and nothing else.

A body that declares a local and never reads it, or only assigns to it, is valid in the other six targets. Its Go output doesn't compile:

func unused() int {
    var t int = 3      // declared and not used: t
    return 1
}

The generator already handles this rule in one place. GenerateEntryPoint (~L1937-L1980) writes _ = args, "since Go refuses a local nothing reads and a body is free to ignore its arguments". The same applies to every local that comes from a VariableDeclaration.

Reproduction

var f = new FunctionDeclaration("unused") { ReturnType = "int" };
f.Body.Add(new VariableDeclaration("t", "int", Literal.Number(3)));
f.Body.Add(new ReturnStatement(Literal.Number(1)));

Generate as Go, wrap the output in package main, and run go build. It fails with declared and not used: t.

Suggested fix

Follow a local's declaration with _ = <name>. There are two ways to do it:

  • Simplest: add the line after every local.
  • Tidier: add it only when nothing later in the enclosing body reads the name. The StaticTypeScope walk in StandardLanguageGenerator (~L249-L258) already visits a body's locals and could host that check.

Either way, the output has to stay gofmt-clean.

Acceptance criteria

  • The function above compiles under GoGeneratedSourceCompilesTests, which still checks the output against gofmt.
  • If the tidier version is chosen, a local that is read later gets no _ = line.

Activity

  1. added
    bug
    on Oct 19, 2026
  2. matt-edmondson commented on Oct 19, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    • Category: Bug
    • Priority: Medium. Any body with a local that is never read produces Go that doesn't compile. The failure is loud, but unused or write-only locals are common in real input, so it will be hit often.
    • Area / suggested assignee: GoGenerator.GenerateVariableDeclaration; the existing _ = args in GenerateEntryPoint is the precedent. Owner: @matt-edmondson
    • Duplicates / in progress: not a duplicate, and no open PR touches Go local declarations (Map fixed-width integer types and apply variable member defaults in constructors #266 maps fixed-width integer types and constructor defaults).
    • Next step: start with the simple version (_ = name after every local) to get GoGeneratedSourceCompilesTests green; move to the read-analysis version only if the extra lines are judged too noisy.

    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions