Switch with multiple conditions in a case should not trigger swift:S1301

While I generally agree with swift:S1301 that if-then-else is easier than a switch for one or two cases, when I have multiple cases that require the same action, I think a switch is easier to work with than if-then-else. I recently had this function (currently returning dummy values) where I have 4 conditions in 2 cases (1 case and a default) and am taking 2 actions based on those conditions. I think this was flagged incorrectly as 2 cases.

This is a simple playground that I think demonstrates the switch is easier to read than the if-than-else in this situation:

import CoreLocation

// CLAuthorizationStatus can be the following on all Apple platforms except as noted
// .denied
// .notDetermined
// .restricted
// .authorized // macOS only
// .authorizedAlways // all but tvOS
// .authorizedWhenInUse // only Mac Catalyst on macOS
// see https://developer.apple.com/documentation/corelocation/clauthorizationstatus

var authorized: CLAuthorizationStatus = .denied

switch authorized {
case .denied, .notDetermined, .restricted:
    // do something when unable to get location
    // do something with location

if authorized == .denied || authorized == .notDetermined || authorized == .restricted {
    // do something when unable to get location
} else {
    // do something with location

Therefor I think it might be better to have the checker count the number of cases in a single case statement instead of the number of instances of the word case.


Thanks for reporting this case, I created the ticket to improve it: [SONARSWIFT-491] FP S1301: case with several conditions - SonarSource