Duplicate Conditions

Duplicate sets of conditions in the same branch instruction should be avoided

  • Identifier: duplicate_conditions
  • Enabled by default: Yes
  • Supports autocorrection: No
  • Kind: lint
  • Analyzer rule: No
  • Minimum Swift compiler version: 5.0.0
  • Default configuration:
    KeyValue
    severity error

Non Triggering Examples

if x < 5 {
    foo()
} else if y == "s" {
    bar()
}
if x < 5 {
    foo()
}
if x < 5 {
    bar()
}
if x < 5, y == "s" {
    foo()
} else if x < 5 {
    bar()
}
switch x {
case "a":
    foo()
    bar()
}
switch x {
case "a" where y == "s":
    foo()
case "a" where y == "t":
    bar()
}
if let x = maybeAbc {
    foo()
} else if let x = maybePqr {
    bar()
}
if let x = maybeAbc, let z = x.maybeY {
    foo()
} else if let x = maybePqr, let z = x.maybeY {
    bar()
}
if case .p = x {
    foo()
} else if case .q = x {
    bar()
}
if true {
    if true { foo() }
}

Triggering Examples

if x < 5 {
    foo()
} else if y == "s" {
    bar()
} else if x < 5 {
    baz()
}
if z {
    if x < 5 {
        foo()
    } else if y == "s" {
        bar()
    } else if x < 5 {
        baz()
    }
}
if x < 5, y == "s" {
    foo()
} else if x < 10 {
    bar()
} else if y == "s", x < 5 {
    baz()
}
switch x {
case "a", "b":
    foo()
case "c", "a":
    bar()
}
switch x {
case "a" where y == "s":
    foo()
case "a" where y == "s":
    bar()
}
if let xyz = maybeXyz {
    foo()
} else if let xyz = maybeXyz {
    bar()
}
if let x = maybeAbc, let z = x.maybeY {
    foo()
} else if let x = maybeAbc, let z = x.maybeY {
    bar()
}
if #available(macOS 10.15, *) {
    foo()
} else if #available(macOS 10.15, *) {
    bar()
}
if case .p = x {
    foo()
} else if case .p = x {
    bar()
}
if x < 5 {}
else if x < 5 {}
else if x < 5 {}