Skip to content

no-comment-before-block

C#styleno fixdefault: offcsharp/stylecop

Reports a comment between the head of an if, else, while, do, for, foreach, lock, try, catch, finally, switch, checked or unchecked and its {.

A comment there is attached to neither the head nor the block, and a reader may take it for a part of the condition. It is also easy to separate from its statement when the code is moved.

To fix, put the comment inside the block or above the statement.

Reported
class A { void F(bool x, int y) {
;
...
while (x) /* c */ { }
switch (y) { case 1: ; break; default: ; }
L: ;
int z = 1;;
...
csharp:
lint:
rules:
no-comment-before-block: warn # off (default), warn, error

Enabled by the presets: csharp/stylecop.

Suppress one line with sudsy-disable-next-line no-comment-before-block in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
StyleCop SA1108 full