Repository navigation
fix: only call GetChildren in generated code when children are used - #1460
Closed
dannyshafer wants to merge 1 commit into
Closed
dannyshafer wants to merge 1 commit into
dannyshafer wants to merge 1 commit into
Conversation
…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
force-pushed
the
fix-1393-children-nil-check
branch
2 times, most recently
from
October 21, 2026 14:28
164a62a to
b6957eb
Compare
Owner
|
Thanks, there were some conflicts, so I replaced it with #1472 but ensured you were listed as committer. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Generated components currently write:
CodeQL reports the fallback as
go/useless-assignment-to-localin 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:templ.GetChildren(ctx)only when the template contains a{ children... }expression (found with the existingparser/v2/visitor), and drops the nil check. The variable name is still allocated, so the numbering of the other generated variables doesn't change.GetChildrenreturnNopComponentwhenWithChildrenwas called with a nil component. That was the one case whereGetChildrencould return nil, so a hand-writtentempl.WithChildren(ctx, nil)keeps rendering nothing instead of panicking. That covers the old runtime/generator mismatch concern raised in the issue too.*_templ.gofiles withgo run ./cmd/templ generate -include-version=false.Fixes #1393
Testing
TestGetChildren(root package): no children, cleared children, andWithChildren(ctx, nil)all return a non-nil component that renders nothing. The nil case fails without theruntime.gochange.TestGeneratorChildren(generator):GetChildrenis emitted only when{ children... }is used, including inside a nestedifand@Wrapper() { ... }block, and theNopComponentfallback is never emitted.go vet ./...andgo test -short ./...pass;gofmt -l .is clean.This change was prepared with AI assistance (Claude Code).