C#

C#

By DeepSource

Redundant else due to return CS-R1044

Anti-pattern

The block under else-if is executed only when the preceding if condition evaluates to false. However, if the last statement of the block under the preceding if statement is a return statement, else in else-if becomes redundant as the controller never reaches it, and, it can be written as a separate if statement.

Examples

Bad Practice

if (x == 1) {
    // Do something
    return;
} else if (x == 2) {
    // Do something
}

Recommended

if (x == 1) {
    // Do something
    return;
}

if (x == 2) {
    // Do something
}