Unnecessary else after a returning if branch

ID

objectivec.maintainability.else_after_return

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Control Flow

Language

Objective-C

Description

Reports an else that follows an if branch whose last statement is a return, whether the branch is braced or not. An else if continuing a chain is not reported, and neither is a branch whose return sits inside a nested loop or block rather than closing the branch.

Rationale

The if branch already leaves the method, so nothing after it can run unless the condition was false — the else states a second time what the return has established. The cost is that the whole tail of the method sits one level deeper than it needs to, and every guard added later nests it deeper again, until the part that does the work is the most indented part of the method rather than the plainest.

The following code illustrates the pattern detected by this rule:

@implementation Validator
- (NSString *)describe:(NSInteger)code {
    if (code == 0) {
        [self log];
        // FLAGGED: Unnecessary else after a returning if branch
        return @"idle";
    } else {
        return @"busy";
    }
}

- (void)log { }
- (void)reset { }
@end

Remediation

Drop the else and dedent its body: the if becomes a guard clause and the rest of the method reads at one level. Where several conditions each return, a run of guard clauses is usually clearer than the chain they replace.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved