Unconditional Jump

ID

swift.unconditional_jump

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Swift

Tags

loop, reliability

Description

Reports a loop whose first body statement is an unconditional terminator (break, return, throw, continue). The loop body cannot execute past the first iteration — the loop is effectively a one-shot if.

for i in 0..<10 {                // FLAW — return on first iteration
    return i
}

while true {                     // FLAW — break on first iteration
    break
}

for i in 0..<10 {                // OK — terminator is guarded
    if i == 5 { return i }
}

Rationale

Loops are a control structure for repeated execution. A loop body that unconditionally terminates is either a typo (missing condition) or a disguised straight-line expression.

Remediation

Either:

  • Guard the terminator with a condition (if x == target { break }).

  • Replace the loop with a direct expression.

// Guarded:
for i in 0..<10 {
    if i == target { return i }
}

// Or just use first(where:):
return (0..<10).first(where: { $0 == target })