Switch and Case Statement Alignment

Case statements should vertically align with their closing brace, or indented if configured otherwise.

  • Identifier: switch_case_alignment
  • Enabled by default: Yes
  • Supports autocorrection: No
  • Kind: style
  • Analyzer rule: No
  • Minimum Swift compiler version: 5.0.0
  • Default configuration:
    KeyValue
    severity warning
    indented_cases false
    ignore_one_liners false

Non Triggering Examples

switch someBool {
case true: // case 1
    print('red')
case false:
    /*
    case 2
    */
    if case let .someEnum(val) = someFunc() {
        print('blue')
    }
}
enum SomeEnum {
    case innocent
}
if aBool {
    switch someBool {
    case true:
        print('red')
    case false:
        print('blue')
    }
}
switch someInt {
// comments ignored
case 0:
    // zero case
    print('Zero')
case 1:
    print('One')
default:
    print('Some other number')
}
func f() -> Int {
    return switch i {
    case 1: 1
    default: 2
    }
}
switch i { case .x: 1 default: 0 }
let a = switch i { case .x: 1 default: 0 }

Triggering Examples

switch someBool {
    case true:
        print("red")
    case false:
        print("blue")
}
if aBool {
    switch someBool {
        case true:
            print('red')
        case false:
            print('blue')
    }
}
switch someInt {
    case 0:
        print('Zero')
    case 1:
        print('One')
    default:
        print('Some other number')
}
let a = switch i {
    case 1: 1
    default: 2
}
switch someBool {
case true:
    print('red')
    case false:
        print('blue')
}
if aBool {
    switch someBool {
        case true:
        print('red')
    case false:
    print('blue')
    }
}
let a = switch i {
case 1: 1
    default: 2
}
switch i { case .x: 1 default: 0 }
switch i {
case .x: 1 default: 0 }
switch i { case .x: 1 default: 0
}
switch i
{ case .x: 1 default: 0 }
let a = switch i {
case .x: 1 default: 0
}
let a = switch i {
case .x: 1 default: 0 }