Cases on Newline
Cases inside a switch should always be on a newline
- Identifier:
switch_case_on_newline
- Enabled by default: No
- Supports autocorrection: No
- Kind: style
- Analyzer rule: No
- Minimum Swift compiler version: 5.0.0
- Default configuration:
Key Value severity warning
Non Triggering Examples
/*case 1: */return true
//case 1:
return true
let x = [caseKey: value]
let x = [key: .default]
if case let .someEnum(value) = aFunction([key: 2]) { }
guard case let .someEnum(value) = aFunction([key: 2]) { }
for case let .someEnum(value) = aFunction([key: 2]) { }
enum Environment {
case development
}
enum Environment {
case development(url: URL)
}
enum Environment {
case development(url: URL) // staging
}
switch foo {
case 1:
return true
}
switch foo {
default:
return true
}
switch foo {
case let value:
return true
}
switch foo {
case .myCase: // error from network
return true
}
switch foo {
case let .myCase(value) where value > 10:
return false
}
switch foo {
case let .myCase(value)
where value > 10:
return false
}
switch foo {
case let .myCase(code: lhsErrorCode, description: _)
where lhsErrorCode > 10:
return false
}
switch foo {
case #selector(aFunction(_:)):
return false
}
do {
let loadedToken = try tokenManager.decodeToken(from: response)
return loadedToken
} catch { throw error }
Triggering Examples
switch foo {
↓case 1: return true
}
switch foo {
↓case let value: return true
}
switch foo {
↓default: return true
}
switch foo {
↓case "a string": return false
}
switch foo {
↓case .myCase: return false // error from network
}
switch foo {
↓case let .myCase(value) where value > 10: return false
}
switch foo {
↓case #selector(aFunction(_:)): return false
}
switch foo {
↓case let .myCase(value)
where value > 10: return false
}
switch foo {
↓case .first,
.second: return false
}