-
Notifications
You must be signed in to change notification settings - Fork 22
SA1503
TypeName |
CurlyBracketsMustNotBeOmitted |
CheckId |
SA1503 |
Category |
Layout Rules |
The opening and closing curly brackets for a C# statement have been omitted.
A violation of this rule occurs when the opening and closing curly brackets for a statement have been omitted. In C#, some types of statements may optionally include curly brackets. Examples include if, while, and for statements. For example, an if-statement may be written without curly brackets:
if (true)
return this.value;
Although this is legal in C#, StyleCop always requires the curly brackets to be present, to increase the readability and maintainability of the code.
When the curly brackets are omitted, it is possible to introduce an error in the code by inserting an additional statement beneath the if-statement. For example:
if (true)
this.value = 2;
return this.value;
Glancing at this code, it appears as if both the assignment statement and the return statement are children of the if-statement. In fact, this is not true. Only the assignment statement is a child of the if-statement, and the return statement will always execute regardless of the outcome of the if-statement.
StyleCop always requires the opening and closing curly brackets to be present, to prevent these kinds of errors:
if (true)
{
this.value = 2;
return this.value;
}
To fix a violation of this rule, wrap the body of the statement in curly brackets.
[SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1503:CurlyBracketsMustNotBeOmitted", Justification = "Reviewed.")]
- - SA0102 - Clean Install
- - Download
- - Documentation Rules - Layout Rules - Maintainability Rules - Naming Rules - Ordering Rules - Readability Rules - Spacing Rules - Suppressions
- - Adding a custom StyleCop settings page - Adding custom rule settings - Authoring a custom styleCop rule - Authoring rules metadata - Custom CSharp Language Service - Custom MSBuild Integration - Hosting StyleCop in a Custom Environment - Installing a Custom Rule - Integrating StyleCop Into Build Environments - Integrating StyleCop into MSBuild - Writing Custom Rules for StyleCop