BRO1526: Blank line between switch sections¶
The sections of a switch statement are separated by a blank line, or (if you configure it) not. Not a StyleCop rule:
from Roslynator's RCS0061.
Example¶
Before¶
public static class Shipping
{
public static decimal Cost(string zone, decimal weight)
{
switch (zone)
{
case "local":
return 5m;
case "national":
case "island":
return 5m + weight;
default:
return 20m + (2 * weight);
}
}
}
After¶
public static class Shipping
{
public static decimal Cost(string zone, decimal weight)
{
switch (zone)
{
case "local":
return 5m;
case "national":
case "island":
return 5m + weight;
default:
return 20m + (2 * weight);
}
}
}
Why¶
A long switch reads like a list of small blocks. A blank line between them shows where one case ends and the next
begins, the way BRO1505 separates members. Some teams prefer compact sections; the option is for them.
Configuration¶
[*.cs]
stylebro_blank_line_between_switch_sections = include
include(default): one blank line between sections.omit: no blank line between sections.omit_after_block: a blank line, except after a section whose last statement is a block (case 1: { ... }).
Labels of one section (case 2: and case 3: above) belong together and are never separated. Several blank lines
between sections are BRO1517's, which leaves one.
How the fix works¶
It adds a line break before the next section's first label, or removes the blank lines there.
After a multi-line block (a section that ends in { ... }, or in an if/else whose last } closes the section)
the setting decides too: where it wants no blank line (omit, or omit_after_block after a { ... } section),
BRO1519 doesn't ask for one after the }, and BRO1504 doesn't ask for one above a comment
that starts the next section. Where it wants one (include), BRO1519 wants the same and this rule leaves that gap to it.
Not reported¶
- A comment or directive between the sections: the blank line may belong to the comment (BRO1504), and
an
#ifcan hide sections. A trailing comment after the previous section's last statement is fine. - Sections on one line (
case 1: return 1; case 2: return 2;). - Switch expressions: their arms aren't sections.
- Code with syntax errors in the
switch.
Compared with StyleCop¶
StyleCop has no such rule; its SA1513 (StyleBro's BRO1519) only asks for a blank line after a }. stylebro-migrate
turns this rule off, so migrating doesn't change a StyleCop-clean repository.