Skip to content

Render a body of only blank lines as an empty block - #133

Merged
matt-edmondson merged 1 commit into
mainfrom
fix-blank-body
Oct 7, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix-blank-body

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #119

What changed

TemplateRendering.WriteBody chooses between an empty block, an expression body and a braced block by counting lines, and it counted blank lines too. A factory that wrote only a NewLine() therefore produced public void M() with no body (CS0501). This hits methods, constructors and operators.

WriteBody now chooses the shape from the lines that have content:

  • No content lines gives { }.
  • One content line goes on the declaration line, even if blank lines surround it.
  • Two or more content lines keep the existing braced path. The fragment is spliced as written, so blank lines inside a braced body stay where they are.

Tests

  • New: ABodyThatWritesOnlyBlankLinesRendersAsAnEmptyBlock covers a method body with one and with two NewLine() calls.
  • New: AConstructorBodyThatWritesOnlyBlankLinesIsAnEmptyBlock covers the same for a constructor.
  • New: AnExpressionBodyBetweenBlankLinesStaysOnTheDeclarationLine.
  • New: ABlankLineInsideABracedBodyIsKept, which pins the unchanged interior-blank behaviour.
  • With the fix reverted, 5 of the new cases fail. The full suite passes with the fix (193/193).

🤖 Generated with Claude Code

https://claude.ai/code/session_01634kz4hrbkEJcGHKJWQjtA


Generated by Claude Code

WriteBody counted blank lines when choosing between an empty block, an
expression body and a braced block, so a factory that only wrote a
NewLine left the declaration with no body (CS0501). Blank lines are now
ignored when choosing the shape; interior blank lines in a braced body
are still spliced as written.

Fixes #119

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

sonarqubecloud Bot commented Oct 7, 2026

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 BodyFactory that writes only blank lines emits a declaration with no body: public void M() (CS0501)

2 participants