Редактиране

Споделяне чрез


Simplify conditional expression (IDE0075)

Property Value
Rule ID IDE0075
Title Simplify conditional expression
Category Style
Subcategory Language rules (expression-level preferences)
Applicable languages C# and Visual Basic
Options dotnet_style_prefer_simplified_boolean_expressions

Overview

This style rule concerns simplifying conditional expressions that return a constant value of true or false versus retaining conditional expressions with explicit true or false return values.

Options

Options specify the behavior that you want the rule to enforce. For information about configuring options, see Option format.

dotnet_style_prefer_simplified_boolean_expressions

Property Value Description
Option name dotnet_style_prefer_simplified_boolean_expressions
Option values true Prefer simplified conditional expressions
false Do not prefer simplified conditional expressions
Default option value true
// dotnet_style_prefer_simplified_boolean_expressions = true
var result1 = M1() && M2();
var result2 = M1() || M2();

// dotnet_style_prefer_simplified_boolean_expressions = false
var result1 = M1() && M2() ? true : false;
var result2 = M1() ? true : M2();
' dotnet_style_prefer_simplified_boolean_expressions = true
Dim result1 = M1() AndAlso M2()
Dim result2 = M1() OrElse M2()

' dotnet_style_prefer_simplified_boolean_expressions = false
Dim result1 = If (M1() AndAlso M2(), True, False)
Dim result2 = If (M1(), True, M2())

Suppress a warning

If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable IDE0075
// The code that's violating the rule is on this line.
#pragma warning restore IDE0075

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.IDE0075.severity = none

To disable all of the code-style rules, set the severity for the category Style to none in the configuration file.

[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none

For more information, see How to suppress code analysis warnings.

See also