Duplicate Conditions

ID

swift.duplicate_conditions

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Swift

Tags

dead-code, reliability, suspicious-code

Description

Reports if / else if / …​ chains in which two branches share the same boolean condition. The later occurrence is unreachable — anything matching the duplicated condition fires the earlier branch — and almost always a copy-paste error.

Rationale

Duplicate conditions are dead code that masks a bug:

  • The developer probably intended to change the operator or comparison value in one of the branches.

  • Tests typically do not catch the duplicate because the data falls into the first branch as designed.

  • The bug stays latent until a different code path or a refactor exposes it.

// Bad — third branch is unreachable.
if a == 1 {
    return "one"
} else if a == 2 {
    return "two"
} else if a == 1 {
    return "unreachable"
}

Notes

  • Branches whose conditions are if let, if case or #available are not flagged: optional bindings introduce a fresh variable on each branch and #available is a runtime platform-version check, so textually-equal copies are not necessarily dead code.

Remediation

Fix the duplicated condition — usually the second one was meant to test a different value — or drop the redundant branch.

// Good.
if a == 1 {
    return "one"
} else if a == 2 {
    return "two"
} else if a == 3 {
    return "three"
}