Skip to content

Brace a member body that holds only a block comment [patch] - #187

Open
matt-edmondson wants to merge 1 commit into
mainfrom
fix/block-comment-only-body-185
Open

matt-edmondson wants to merge 1 commit into
mainfrom
fix/block-comment-only-body-185

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #185

Problem

TemplateRendering.HasCode treated only // comments and # directives as "not code". A BodyFactory that wrote only /* nothing to do */ therefore counted as a one-line body of code and was placed on the declaration line:

void M() /* nothing to do */   // CS1002

The // form has been braced since #157/#161.

Change

  • VerbatimStringScanner.FindCommentText flags every character inside a top-level comment, line or block, delimiters included. It reuses the existing lexer walk, so a /* or // inside a string or character literal starts no comment, and an unterminated block comment runs to the end.
  • HasCode now looks for non-whitespace outside those comments on any line that is not a directive. The lines are scanned together, so a block comment spanning several lines is not code on any of them.
  • Expression-body termination (FindCodeEnds/IsCode) is unchanged.

Once #186 merges, RenderExpression will also check HasCode. A /* */-only expression body is then refused too, which closes the block-comment variant of #184. The two PRs touch different methods and each stands alone on main.

Tests

Five new tests in TemplateTests:

  • Braced, all three failing on main: a single-line block comment, a multi-line block comment, and a block comment followed by a line comment (on a constructor)
  • Unchanged, passing both before and after: /* note */ => 1; still counts as code, and "/* not a comment */" inside a string is code

The full suite passes: 334 tests. The local Sonar run reports nothing beyond the existing S2699 findings. An S3776 complexity hit on the first draft was fixed by moving the comment marking into TrySkipCommentOrLiteral.


Generated by Claude Code

HasCode treated only // comments and directives as "not code", so a BodyFactory that wrote only a
/* ... */ comment took the single-line branch and was placed on the declaration line:
"void M() /* nothing to do */" (CS1002). The scanner now reports the characters inside every
top-level comment, line or block, honouring string and character literals, and HasCode looks for
code outside them, so a block-comment-only body is braced like a //-only one has been since #161.

Fixes #185

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

sonarqubecloud Bot commented Nov 5, 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 body made only of a /* block comment */ is written as no body: void M() /* nothing to do */ (CS1002), unlike the // form fixed in #157

2 participants