Skip to content

BRO1506: Single-line comments should not be followed by a blank line

A // comment sits directly on the code it describes. Replaces StyleCop SA1512.

Example

Before

public class Shipping
{
    public decimal Cost(int zone, decimal weight)
    {
        switch (zone)
        {
            case 1:
                // Domestic parcels ship at a flat rate.

                return 5m;
            default:
                return 5m + weight * 1.5m;
        }
    }
}

After

public class Shipping
{
    public decimal Cost(int zone, decimal weight)
    {
        switch (zone)
        {
            case 1:
                // Domestic parcels ship at a flat rate.
                return 5m;
            default:
                return 5m + weight * 1.5m;
        }
    }
}

Why

A blank line below a comment makes it unclear what the comment belongs to. Without it, the comment clearly describes the code right below it. Together with BRO1504 (a blank line above such a comment), comments end up attached to their code and separated from what came before.

Not every team wants this: of the three repositories surveyed for StyleBro, two turn the rule off. Set dotnet_diagnostic.BRO1506.severity = none in .editorconfig if you use comments as free-standing section notes. bro init does that when your code mostly keeps a blank line below its comments, or mixes both.

How the fix works

The fix removes the blank lines directly below the comment.

Configuration

[*.cs]
# allow (default): a standalone comment keeps the blank line below it; remove: it goes too
stylebro_blank_line_below_standalone_comments = allow

A standalone comment has a blank line (or the line with the block's {) right above it and a blank line right below it (#region lines next to it don't count): a note on the code above, or a section header, also the first one in a block. Removing the blank line below glues it onto whatever comes next. bro migrate writes remove when the setup it migrates has SA1512 on, so code that was clean stays clean.

public void Send()
{
    Connect();

    // Retries are handled by the transport.

    Flush();
}

Not reported

  • The file header: the comments at the very top of the file, before any code.
  • A comment followed by another comment (single-line or block) after the blank line, such as two paragraphs of a longer note.
  • //// commented-out code and /// comments.
  • Trailing comments (x++; // note).
  • A comment below code: a run of // lines right below a line that ends with ;, followed by a blank line. It describes the code above it (eShop: a note below a field, then a blank line, then a method); moving the blank line above it would attach it to the code below.
  • A standalone comment (a blank line or a { line above it, a blank line below it, not right before a }), unless stylebro_blank_line_below_standalone_comments = remove.