switch has only one or two labels

ID

objectivec.maintainability.too_few_switch_branches

Severity

info

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

low

Resource

Control Flow

Language

Objective-C

Description

Reports a switch carrying one or two labels in total, counting default as one of them. A switch with three or more labels, and an empty switch with none at all, are not reported.

Rationale

A switch earns its verbosity by lining up many values against one expression. With one or two arms it costs a header, a brace, a label and a break to say what if / else says in two lines, and it brings the fall-through hazard along for nothing. Two-arm switches are also what remains after arms are deleted, so the shape usually marks dispatch that has outlived its reason to exist rather than dispatch that was designed this way.

The following code illustrates the pattern detected by this rule:

@implementation Selector
- (void)oneCaseOnly:(XYState)state {
    // FLAGGED: switch has only one or two labels
    switch (state) {
        case XYStateIdle:
            [self idle];
            break;
    }
}

- (XYState)mode { return XYStateIdle; }
- (void)idle { }
- (void)run { }
- (void)done { }
- (void)unknown { }
@end

Remediation

Rewrite the statement as if / else, or as the conditional operator where each arm just produces a value. Keep the switch if arms are about to be added back, or if the compiler exhaustiveness warning over an NS_ENUM is the point of writing it this way.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved