| TypeName | SA1119StatementMustNotUseUnnecessaryParenthesis |
| CheckId | SA1119 |
| Category | Maintainability Rules |
A C# statement contains parenthesis which are unnecessary and should be removed.
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;Starting with C# 8.0, parenthesis may be required around switch expressions or pattern-matching expressions when they
participate in other operations (for example, casts, await, conditional/element/member access, or logical negation).
SA1119 will not report parenthesis that are necessary for correctness, but it will still flag parenthesis that are
purely redundant, such as return (value switch { ... }); or var flag = (input is { Length: 1 });.
To fix a violation of this rule, remove the unnecessary parenthesis.
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1119:StatementMustNotUseUnnecessaryParenthesis", Justification = "Reviewed.")]#pragma warning disable SA1119 // StatementMustNotUseUnnecessaryParenthesis
#pragma warning restore SA1119 // StatementMustNotUseUnnecessaryParenthesis