Back to Devexpress

CRR0021 - Subsequent else-if conditions are identical

coderushforroslyn-118167-static-code-analysis-analyzers-library-crr0021-subsequent-else-if-conditions-are-identical.md

latest1.2 KB
Original Source

CRR0021 - Subsequent else-if conditions are identical

  • Feb 28, 2025

This analyzer detects a conditional statement containing the same conditional statement in its else branch. See the example below.

csharp
if (condition) {
    DoSomething();
} 
else if(condition) {
    DoSomethingElse();
}
vb
If condition Then
    DoSomething()
ElseIf condition Then
    DoSomethingElse()
End If

In this code, the DoSomethingElse() function will never be executed because the second condition is checked only if the first condition is false. Since the conditions are similar, the program flow will never reach the body of the second conditional statement. To make it clear, look at the flowchart below on which the highlighted branch will never be reached.

To fix this, remove the second conditional statement from the else branch or change its condition expression so that it differs from the first one.

See Also

Suppress Analyzers