Skip to content

Latest commit

 

History

History
60 lines (46 loc) · 1.79 KB

SA1119.md

File metadata and controls

60 lines (46 loc) · 1.79 KB

SA1119

TypeName SA1119StatementMustNotUseUnnecessaryParenthesis
CheckId SA1119
Category Maintainability Rules

Cause

A C# statement contains parenthesis which are unnecessary and should be removed.

Rule description

It is possible in C# to insert parenthesis around virtually any type of expression, statement, or clause, and in many situations use of parenthesis can greatly improve the readability of the code. However, excessive use of parenthesis can have the opposite effect, making it more difficult to read and maintain the code.

A violation of this rule occurs when parenthesis are used in situations where they provide no practical value. Typically, this happens anytime the parenthesis surround an expression which does not strictly require the use of parenthesis, and the parenthesis expression is located at the root of a statement. For example, the following lines of code all contain unnecessary parenthesis which will result in violations of this rule:

int x = (5 + b);
string y = (this.Method()).ToString();
return (x.Value);

In each of these statements, the extra parenthesis can be removed without sacrificing the readability of the code:

int x = 5 + b;
string y = this.Method().ToString();
return x.Value;

How to fix violations

To fix a violation of this rule, remove the unnecessary parenthesis.

How to suppress violations

[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1119:StatementMustNotUseUnnecessaryParenthesis", Justification = "Reviewed.")]
#pragma warning disable SA1119 // StatementMustNotUseUnnecessaryParenthesis
#pragma warning restore SA1119 // StatementMustNotUseUnnecessaryParenthesis