Unneeded Break in Switch

Avoid using unneeded break statements

  • Identifier: unneeded_break_in_switch
  • Enabled by default: Yes
  • Supports autocorrection: Yes
  • Kind: idiomatic
  • Analyzer rule: No
  • Minimum Swift compiler version: 5.0.0
  • Default configuration:
    KeyValue
    severity warning

Non Triggering Examples

switch foo {
case .bar:
    break
}
switch foo {
default:
    break
}
switch foo {
case .bar:
    for i in [0, 1, 2] { break }
}
switch foo {
case .bar:
    if true { break }
}
switch foo {
case .bar:
    something()
}
let items = [Int]()
for item in items {
    if bar() {
        do {
            try foo()
        } catch {
            bar()
            break
        }
    }
}

Triggering Examples

switch foo {
case .bar:
    something()
    break
}
switch foo {
case .bar:
    something()
    break // comment
}
switch foo {
default:
    something()
    break
}
switch foo {
case .foo, .foo2 where condition:
    something()
    break
}