Ternary whose condition is a literal constant

ID

objectivec.correctness.constant_conditional_operator

Severity

low

Remediation Complexity

medium

Remediation Risk

medium

Remediation Effort

low

Resource

Dead Code

Language

Objective-C

Description

Reports a ? : conditional expression whose condition is a bare literal — 0, 1, YES, NO, true, false, TRUE or FALSE — so the same operand is chosen every time. A comparison against a constant, such as x == 1 ? a : b, is a real runtime decision and is not reported. The sibling constructs are covered separately: a constant condition on an if is reported by constant-condition, and a ternary with a real condition but pointless operands by redundant-conditional-operator.

Rationale

A ternary exists to choose between two values at run time, and this one does not choose: one operand is always taken and the other never is. The unreachable operand still compiles and still reads as live, so a call in it keeps getting maintained, renamed and reviewed while never running, and the reader has to evaluate the condition by hand to discover which half of the line matters. The usual origin is a toggle hard-wired during debugging, which then ships.

The following code illustrates the pattern detected by this rule:

@implementation Chooser
- (NSInteger)pickOne {
    // FLAGGED: Ternary whose condition is a literal constant
    NSInteger v = 1 ? 10 : 20;
    return v;
}

- (id)primary {
    return self;
}

- (id)fallback {
    return nil;
}

- (NSInteger)scale:(NSInteger)v {
    return v;
}
@end

Remediation

Replace the whole expression with the operand that is always selected, and delete the other. If the choice is meant to be configurable, give the condition a named flag or constant so the line says what decides it.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved