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(decimal weight)
    {
        // Parcels up to 2 kg ship at a flat rate.

        if (weight <= 2)
        {
            return 5m;
        }

        return 5m + (weight - 2) * 1.5m;
    }
}

After

public class Shipping
{
    public decimal Cost(decimal weight)
    {
        // Parcels up to 2 kg ship at a flat rate.
        if (weight <= 2)
        {
            return 5m;
        }

        return 5m + (weight - 2) * 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 StyleCop repos surveyed for StyleBro, two turn SA1512 off. Set dotnet_diagnostic.BRO1506.severity = none in .editorconfig if you use comments as free-standing section notes. stylebro-migrate 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, like StyleCop's SA1512
stylebro_blank_line_below_standalone_comments = allow

A standalone comment has a blank line right above it and right below it (#region lines next to it don't count): a note on the code above, or a section header. Removing the blank line below glues it onto whatever comes next. stylebro-migrate writes remove for StyleCop setups with SA1512 on, so a StyleCop-clean repo stays clean.

public void Send()
{
    Connect();

    // Retries are handled by the transport.

    Flush();
}

Not reported

Like StyleCop:

  • 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).

Unlike StyleCop:

  • 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. StyleCop reports it.
  • A standalone comment (a blank line above and below it, not right before a }), unless stylebro_blank_line_below_standalone_comments = remove. StyleCop reports it.

Compared with StyleCop

Same results as StyleCop with stylebro_blank_line_below_standalone_comments = remove (what stylebro-migrate writes when SA1512 is on), apart from the comment below code above.