Skip to content

Latest commit

 

History

History
59 lines (47 loc) · 1.38 KB

SA1520.md

File metadata and controls

59 lines (47 loc) · 1.38 KB

SA1520

TypeName SA1520UseBracesConsistently
CheckId SA1520
Category Layout Rules

📝 This rule is new for StyleCop Analyzers, and was not present in StyleCop Classic.

Cause

The opening and closing braces of a chained if/else if/else construct were included for some clauses, but omitted for others.

Rule description

A violation of this rule occurs when the opening and closing braces for a chained statement have been included for some clauses but omitted for others. In C#, some types of statements may optionally include braces. For example, an if-statement may be written with inconsistent braces:

if (true)
    return this.value;
else
{
    return that.value.
}

Although this is legal in C#, StyleCop requires the braces to be present for all clauses of a chained if/else if/else construct when braces are included for any clause, to increase the readability and maintainability of the code.

How to fix violations

To fix a violation of this rule, the violating statement will be converted to a block statement.

How to suppress violations

if (true)
#pragma warning disable SA1520 // Use braces consistently
    return this.value;
#pragma warning restore SA1520 // Use braces consistently
else
{
    return that.value.
}