Skip to content

Generated code triggers CodeQL go/useless-assignment-to-local for GetChildren fallback #1393

Description

@andreas-ekman-wcar

What happened?

Generated *_templ.go files contain a child-component fallback assignment that CodeQL reports as go/useless-assignment-to-local:

ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
    templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)

CodeQL reports the assignment to templ_7745c5c3_Var1 as useless when the generated component does not read the children variable later. This creates noisy code scanning alerts for generated templ output.

This seems related to #1295, but the scanner here is GitHub CodeQL rather than Staticcheck.

Why it looks removable

templ.GetChildren(ctx) already appears to return templ.NopComponent when no children are present:

func GetChildren(ctx context.Context) Component {
    _, v := getContext(ctx)
    if v.children == nil {
        return NopComponent
    }
    return *v.children
}

So the generated nil check/fallback appears redundant, and when the children variable is not subsequently used, it triggers a dead-store warning.

Reproduction

Generate a templ component that does not use children, then scan the generated Go with CodeQL security-and-quality queries. In our case the generated file header is currently from v0.3.977, with the module pinned to github.com/a-h/templ v0.3.1001.

I also tested generation with v0.3.1020; the same GetChildren/NopComponent pattern is still emitted.

Expected behavior

Ideally generated code should avoid the redundant nil fallback, or avoid declaring the children variable when the component body does not use children, so generated files do not trigger go/useless-assignment-to-local.

templ version

Observed with generated output from v0.3.977; still reproducible when generating with v0.3.1001 and v0.3.1020.

CodeQL rule

go/useless-assignment-to-local

https://codeql.github.com/codeql-query-help/go/go-useless-assignment-to-local/

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions