Skip to content

Refuse a null parameter, member or nested type before writing [patch] - #199

Merged
matt-edmondson merged 4 commits into
mainfrom
fix/null-template-entries-198
Nov 11, 2026
Merged

matt-edmondson merged 4 commits into
mainfrom
fix/null-template-entries-198

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #198

What changed

A null entry in a template collection used to throw NullReferenceException partway through the declaration. The class body's scope then closed its brace onto the half-written line (void M}, record R). The check now runs up front in Validate, before anything is written:

  • ClassTemplate.Validate rejects a null entry in Members, NestedClasses and PositionalParameters
  • MethodTemplate, ConstructorTemplate and OperatorTemplate gain a Validate that rejects a null Parameters entry. Their WriteTo calls it first, so writing a member on its own is also all-or-nothing
  • The shared helper is TemplateRendering.EnsureNoNullEntries, and its message names the template and the collection (e.g. Method 'M' has a null entry in Parameters.)

Notes for review

Tests

There are six new tests in TemplateTests: a null method parameter (inside a class, and written on its own), a null positional parameter, a null constructor parameter, a null operator parameter, and a null member or nested class. Each asserts the exception and that the writer holds nothing new.

  • With the source change reverted, all six fail (NRE, or partial output left behind)
  • With the change, the full suite passes: 335/335 on .NET 10

Generated by Claude Code

A null entry in Parameters, PositionalParameters, Members or NestedClasses
used to throw NullReferenceException partway through the declaration,
leaving `void M}` or `record R` behind. Validate now rejects them up front
with an InvalidOperationException naming the template and collection, and
method, constructor and operator WriteTo run that check before writing.

Fixes #198

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5L5j1hRifcohu2Dmb
Keeps both TemplateRendering helpers: EnsureNoNullEntries from this branch
and NonBlank from #200.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5L5j1hRifcohu2Dmb
OperatorTemplate.Validate keeps #193's base.Validate() call alongside the
null-parameter check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5L5j1hRifcohu2Dmb
Follows #186's Validate(CodeBlocker) signature in the method, constructor
and operator overrides, keeping the null-parameter check.

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

Copy link
Copy Markdown

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.

A null ParameterTemplate in Parameters/PositionalParameters throws NullReferenceException after the declaration is half-written: void M} / record R

2 participants