Table of Contents

SA1501: Statement should not be on a single line

Cause

A C# statement containing opening and closing braces is written completely on a single line.

Rule description

A violation of this rule occurs when a statement that is wrapped in opening and closing braces is written on a single line. For example:

public object Method()
{
    lock (this) { return this.value; }
}

When StyleCop checks this code, a violation of this rule will occur because the entire lock statement is written on one line. The statement should be written across multiple lines, with the opening and closing braces each on their own line, as follows:

public object Method()
{
    lock (this) 
    {
        return this.value; 
    }
}

Empty catch and finally blocks are allowed on a single line. This exception also applies to blocks containing only comments; blocks with statements continue to follow this rule.

How to fix violations

The optional layoutRules.allowBracelessIfStatements setting defaults to "off". With "sameLine", a standalone if may have a single-line jump statement without braces on the same line as its closing ). With "nextLine", the jump must be on the immediately following line. This does not apply to any branch of an if/else if/else chain, or to other statement bodies. Existing braced statements are unaffected. The entire if header must fit on one line for either option; "sameLine" requires the whole if statement to fit on one line. Multiline conditions require braces. See Configuration.md for supported jumps and examples.

To fix a violation of this rule, rewrite the statement so that it expands across multiple lines.

How to suppress violations

[SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1501:StatementMustNotBeOnSingleLine", Justification = "Reviewed.")]
#pragma warning disable SA1501 // StatementMustNotBeOnSingleLine
#pragma warning restore SA1501 // StatementMustNotBeOnSingleLine