Skip to content

fix: only call GetChildren in generated code when children are used - #1460

Closed
dannyshafer wants to merge 1 commit into
a-h:mainfrom
dannyshafer:fix-1393-children-nil-check
Closed

dannyshafer wants to merge 1 commit into
a-h:mainfrom
dannyshafer:fix-1393-children-nil-check

Conversation

@dannyshafer

Copy link
Copy Markdown
Contributor

Generated components currently write:

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

CodeQL reports the fallback as go/useless-assignment-to-local in every component that doesn't render { children... }, as described in the issue.

Removing only the nil check isn't enough: in components that don't use { children... }, the check is the only use of the variable, so the generated code would fail with "declared and not used". This change:

  • Emits templ.GetChildren(ctx) only when the template contains a { children... } expression (found with the existing parser/v2/visitor), and drops the nil check. The variable name is still allocated, so the numbering of the other generated variables doesn't change.
  • Makes GetChildren return NopComponent when WithChildren was called with a nil component. That was the one case where GetChildren could return nil, so a hand-written templ.WithChildren(ctx, nil) keeps rendering nothing instead of panicking. That covers the old runtime/generator mismatch concern raised in the issue too.
  • Regenerates the checked-in *_templ.go files with go run ./cmd/templ generate -include-version=false.

Fixes #1393

Testing

  • Added TestGetChildren (root package): no children, cleared children, and WithChildren(ctx, nil) all return a non-nil component that renders nothing. The nil case fails without the runtime.go change.
  • Added TestGeneratorChildren (generator): GetChildren is emitted only when { children... } is used, including inside a nested if and @Wrapper() { ... } block, and the NopComponent fallback is never emitted.
  • go vet ./... and go test -short ./... pass; gofmt -l . is clean.

This change was prepared with AI assistance (Claude Code).

…ixes a-h#1393

GetChildren never returns nil, so the generated nil check and
NopComponent fallback were dead code that CodeQL reports as
go/useless-assignment-to-local. GetChildren now also returns
NopComponent when WithChildren was given a nil component.
@dannyshafer
dannyshafer force-pushed the fix-1393-children-nil-check branch 2 times, most recently from 164a62a to b6957eb Compare October 21, 2026 14:28
@a-h

a-h commented Oct 23, 2026

Copy link
Copy Markdown
Owner

Thanks, there were some conflicts, so I replaced it with #1472 but ensured you were listed as committer.

@a-h a-h closed this Oct 23, 2026
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.

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

3 participants