Skip to content

BRO1502: Chained blocks should not be preceded by a blank line

else, catch and finally directly follow the block they continue. Replaces StyleCop SA1510.

Example

Before

public class Gate
{
    public string Open(bool allowed)
    {
        if (allowed)
        {
            return "open";
        }

        else
        {
            return "closed";
        }
    }
}

After

public class Gate
{
    public string Open(bool allowed)
    {
        if (allowed)
        {
            return "open";
        }
        else
        {
            return "closed";
        }
    }
}

Why

A blank line before else makes it look like the start of a new statement instead of part of the if. The same goes for catch and finally after try.

How the fix works

The fix removes the blank lines directly above the keyword. It also applies when the if has no braces.

Not reported

  • A comment or directive directly above the keyword, for example a comment explaining the else branch.
  • The while of a do … while loop, like StyleCop.

Compared with StyleCop

Same results as StyleCop.