Skip to content

Brace a member body that holds only comments or directives [patch] - #161

Open
matt-edmondson wants to merge 2 commits into
mainfrom
claude/issue-157-comment-only-body
Open

matt-edmondson wants to merge 2 commits into
mainfrom
claude/issue-157-comment-only-body

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #157

What changed

TemplateRendering.WriteBody decided a body's shape by counting non-blank lines. A line counted even when it was only a // comment or a #pragma directive. So // nothing to reset became void M() // nothing to reset, and two or more such lines were spliced below the declaration with no braces. In both cases the member had no body, which gives CS1002.

Now, when no line of the body holds code, the body is written as a braced block on the lines after the declaration, with the comments and directives kept inside it:

void M()
{
	// nothing to reset
}

A line holds code when its text before any line comment is non-empty and does not start with #. The comment boundary comes from the existing FindCodeEnds, the helper WriteExpressionBody already uses, so a // inside a string literal is not taken for a comment. A one-line body that does hold code, such as => x;, { } or => x; // trailing, still stays on the declaration line.

Tests

  • New tests cover the four inputs from the issue: one line comment, two line comments, a comment-only ConstructorTemplate body, and an empty PragmaScope.
  • AOneLineBodyWithCodeStaysOnTheDeclarationLine pins the unchanged one-line shape.
  • With the fix reverted, the four new brace tests fail.
  • The full suite passes: 256 tests on net10.0.

Generated by Claude Code

WriteBody chose a body's shape by counting non-blank lines, so a body of
one line comment became an expression body ("void M() // note") and
two or more comment or #pragma lines were spliced below the declaration
with no braces. Either way the member had no body (CS1002). A body with
no line of code is now wrapped in a braced block; one-line bodies with
code still stay on the declaration line.

Fixes #157

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

sonarqubecloud Bot commented Nov 2, 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 method or constructor whose BodyFactory writes only comments or #pragma lines gets no body, so the output fails with CS1002

2 participants