Back to Devexpress

CRR0012 - Logical OR expression has opposite operands

coderushforroslyn-118112-static-code-analysis-analyzers-library-crr0012-logical-or-expression-has-opposite-operands.md

latest1.6 KB
Original Source

CRR0012 - Logical OR expression has opposite operands

  • Feb 28, 2025
  • 2 minutes to read

This analyzer detects expressions that contain logically opposite parts separated by the ‘||’ (OR) statement.

csharp
if (str == null || str != null && str == ""){ // CRR0012
    // ...
}
vb
If str Is Nothing OrElse (str IsNot Nothing AndAlso str = "") Then ' CRR0012
    ' ...
End If

Such expressions can be simplified. Refer to the following chain of transformations.

csharp
str == null || (str != null && str == "");
(str == null || str != null) && (str == null || str == ""); // Distributive property
true && (str == null || str == ""); // (a || !a) == true
str == null || str == ""; // true && b == b
vb
str Is Nothing OrElse (str IsNot Nothing AndAlso str = "")
(str Is Nothing OrElse str IsNot Nothing) AndAlso (str Is Nothing OrElse str = "") ' Distributive property
True AndAlso (str Is Nothing OrElse str = "") ' (a OR NOT a) == True
str Is Nothing OrElse str = "" ' True && b == b

Thus, the str != null ( str IsNot Nothing ) expression is redundant and can be omitted.

csharp
if (str == null || str == ""){
    // ...
}
vb
If str Is Nothing OrElse str = "" Then
    ' ...
End If

See Also

Suppress Analyzers