Back to Devexpress

CRR0023 - Unnecessary conditional

coderushforroslyn-118219-static-code-analysis-analyzers-library-crr0023-unnecessary-conditional.md

latest1.3 KB
Original Source

CRR0023 - Unnecessary conditional

  • Feb 28, 2025

This analyzer detects conditional statements that always evaluate to true because of the conditional statement above. A possible example is shown in the code snippet below.

csharp
if(a) {
    if(b) {
        DoSomething();
    }
    else {
        if(a) {
            DoSomethingElse();
        }
    }
}
vb
If a Then
    If b Then
        DoSomething()
    Else
        If a Then
            DoSomethingElse()
        End If
    End If
End If

Consider the flowchart of the code snippet given above.

If you trace the program flow, you will see that the highlighted branch is never reached, which means the conditional statement around the DoSomethingElse function is redundant and can be removed.

csharp
if(a) {
    if(b) {
        DoSomething();
    }
    else {
        DoSomethingElse();
    }
}
vb
If a Then
    If b Then
        DoSomething()
    Else
        DoSomethingElse()
    End If
End If

See Also

Suppress Analyzers