Skip to content

Put an expression body's semicolon before a trailing line comment [patch] - #149

Merged
matt-edmondson merged 1 commit into
mainfrom
claude/issue-144-semicolon-before-line-comment
Oct 30, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
claude/issue-144-semicolon-before-line-comment

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #144

What changed

  • TemplateRendering.WriteExpressionBody used to append the terminating ; to the last line that had content, without checking for a line comment. items.Length // cached upstream therefore rendered as => items.Length // cached upstream; and failed to compile (CS1002). The semicolon now goes after the code and before the comment: => items.Length; // cached upstream. If the last content line holds only a comment, the semicolon goes on the last line that has code. The change covers PropertyTemplate.ExpressionBodyFactory and AccessorTemplate.Expression, which both use this path.
  • VerbatimStringScanner.FindLineComments is new, as the issue suggested. It reports where each top-level // comment starts. It reuses the scanner's existing walk, so a // inside a string, character, verbatim or raw literal, a block comment, or an interpolation hole is not reported. The lines are scanned together, so a // on a line that an earlier line's verbatim string continues onto is not taken for a comment.
  • A line with no comment is terminated exactly as before, so existing output is unchanged.

Tests

There are six new tests in TemplateTests, one per acceptance criterion, plus a verbatim-string case:

  • x // c renders => x; // c.
  • A switch expression ending in } // c renders }; // c.
  • A trailing comment-only line leaves the semicolon on the last code line.
  • "http://x" is not treated as containing a comment.
  • A // inside a verbatim string opened on an earlier line is not treated as a comment.
  • AccessorTemplate.Expression with x // c renders get => x; // c.

With the fix reverted, the four behaviour tests fail. The two string-literal tests pass either way, because they guard against the new comment detection matching inside a literal. With the fix in place, the full suite passes (229/229, net10.0). The local Sonar build (.sonarlint/sonar-local.props) reports only the five existing S2699 findings that CLAUDE.md lists.


Generated by Claude Code

…tch]

WriteExpressionBody appended the semicolon to the last line with
content, so `x // note` became `x // note;` and the expression was never
terminated (CS1002). VerbatimStringScanner now also reports where each
top-level line comment starts, ignoring `//` inside literals, block
comments and interpolation holes. The semicolon goes after the code and
before the comment, and a comment-only last line leaves it on the last
line of code.

Fixes #144

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

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 20b00c3 into main Oct 30, 2026
15 checks passed
@matt-edmondson
matt-edmondson deleted the claude/issue-144-semicolon-before-line-comment branch October 30, 2026 02:48
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.

An expression body whose last line ends in a // comment gets its semicolon inside the comment: public int P => x // note; does not compile (CS1002)

2 participants