BRO1101: Code should not contain empty statements¶
A stray ; does nothing and should be removed. Replaces StyleCop SA1106.
Example¶
Before¶
public class Counter
{
private int _count;
public void Increment()
{
_count++; ;
;
}
};
After¶
public class Counter
{
private int _count;
public void Increment()
{
_count++;
}
}
Why¶
An empty statement is noise at best. At worst it's a typo that hides a bug, so it's worth keeping the code free of them.
What is reported¶
- an empty statement in a block or a
casesection:M(); ;, or a line with only; - a
;after the closing brace of a type or namespace:class C { };
Configuration¶
[*.cs]
# true: the ';' after a type's or namespace's '}' stays, like Sonar's S1116 (default false)
stylebro_allow_semicolon_after_closing_brace = false
Sonar's S1116 reports empty statements only; stylebro-migrate writes true for a repository that enforces S1116, unless
the StyleCop setup has SA1106 on (which reports the ; after } too).
How the fix works¶
A line that held only the ; is removed completely; otherwise the ; goes with the space in front of it. A ; at the
start of a line takes the space after it instead, so ; // done becomes // done at the same indentation.
Not reported¶
- Embedded empty statements, such as
while (Poll()) ;orif (x) ;. Replacing them with{ }keeps the behavior but hides what is often a bug. The compiler already warns about the suspicious ones (CS0642, "Possible mistaken empty statement"), and an automatic fix would silence that warning. - Labeled empty statements (
end: ;). A label needs a statement, so removing the;can break the build when the label is the last thing in a block. - A record without a body (
record R(int X);), where the;is required.
Compared with StyleCop¶
StyleCop reports embedded and labeled empty statements too. Its fix turns while (x) ; into { } and removes the ;
of end: ;, which can break the build. For ; // done, StyleCop's fix leaves the comment with one space of
indentation.