Skip to content

An expression body that writes only a comment or directives still renders int P => ;// TODO (CS1525): the #175 guard checks only for blank lines #184

Open

Description

@matt-edmondson

What happens

#178 (for #175) refuses an expression body whose callback writes nothing. A callback that writes only a comment, or only preprocessor directives, gets through and produces the same => ;:

Input Output
ExpressionBodyFactory = b => b.WriteLine("// TODO") int P => ;// TODO
Getter = AccessorTemplate.Expression(b => b.WriteLine("// TODO")) get => ;// TODO
ExpressionBodyFactory = b => { b.WriteLine("#if DEBUG"); b.WriteLine("#endif"); } int P =>
#if DEBUG
#endif
;

All three fail with CS1525 (invalid expression term ;). Reproduced against main @ aa7ca53, and the generated code was compiled to confirm the errors.

Why

RenderExpression (CodeBlocker/Templates/TemplateRendering.cs:341) throws only when every line is whitespace (lines.All(string.IsNullOrWhiteSpace)). When no line holds code, FindTerminatedLine (:429-446) stops at the first line, and Terminate (:527-537) puts the semicolon in front of the comment.

A placeholder like // TODO as the whole body is a common pattern while scaffolding generated code, so this is easy to hit.

Suggested fix

In RenderExpression, also throw when !HasCode(lines), using the existing HasCode / FindCodeEnds helpers. That gives a body made only of comments or directives the same InvalidOperationException and message as an empty one.

Acceptance criteria

Activity

  1. matt-edmondson commented on Nov 5, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions