Skip to content

BRO1501: Opening braces should not be preceded by a blank line

An opening brace on its own line directly follows the line it belongs to. Replaces StyleCop SA1509.

Example

Before

public class Parser

{
    public int Parse(string text)

    {
        return int.Parse(text);
    }
}

After

public class Parser
{
    public int Parse(string text)
    {
        return int.Parse(text);
    }
}

Why

A blank line between a declaration and its brace visually separates things that belong together. This covers every kind of opening brace: types, members, statements, initializers and case blocks.

How the fix works

The fix removes the blank lines directly above the brace. A comment directly above the brace is kept; blank lines above that comment are left alone.

Not reported

  • A brace whose previous token is a }, such as a block following another block, even with a comment in between. A standalone block after an ordinary statement is reported:
var path = GetPath();

{            // BRO1501, like SA1509
    Use(path);
}

{            // not reported: follows a '}'
}
  • A comment or directive directly above the brace.
  • The brace of an entry in an initializer ({ "ssh", 22 } in a dictionary initializer, an inner { 1, 2 } of a multi-dimensional array): a blank line between entries groups them.

var ports = new Dictionary<string, int>
{
    { "http", 80 },
    { "https", 443 },

    { "ssh", 22 },   // not reported: the blank line above groups the entries
};
- Braces inside strings, such as {x} in a multi-line interpolated string. Only whitespace outside code is checked, never string contents.

Compared with StyleCop

Same results as StyleCop, including the exception for a brace after a }, except the brace of an initializer's entry: SA1509 reports it and its fix removes the grouping blank line (#2832, open).