删除不必要的相等运算符 (IDE0100)

属性
规则 ID IDE0100
标题 删除不必要的相等运算符
类别 Style
Subcategory 不必要的代码规则(表达式级首选项)
适用的语言 C# 和 Visual Basic

概述

将非常量布尔表达式与常量 truefalse 进行比较时,此样式规则标记不必要的相等运算符。

选项

此规则没有关联的代码样式选项。

示例

// Code with violations
if (x == true) { }
if (M() != false) { }

// Fixed code
if (x) { }
if (M()) { }
' Code with violations
If x = True Then
End If

If M() <> False Then
End If

' Fixed code
If x Then
End If

If M() Then
End If

抑制警告

如果只想抑制单个冲突,请将预处理器指令添加到源文件以禁用该规则,然后重新启用该规则。

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

若要对文件、文件夹或项目禁用该规则,请在配置文件中将其严重性设置为 none

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

若要禁用所有代码样式规则,请在配置文件中将类别 Style 的严重性设置为 none

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

有关详细信息,请参阅如何禁止显示代码分析警告

请参阅