Rule Request: Prevent using `default:` in a switch
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 19.7k
- Forks
- 2.3k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 11
Description
New Issue Checklist
- Updated SwiftLint to the latest version
- I searched for existing GitHub issues
New rule request
Why should this rule be added?
When iterating over cases in a switch, all cases should be handled to prevent undefined/unexpected behaviour if a new case is added.
Provide several examples of what would and wouldn't trigger violations.
enum SomeEnum {
case foo, bar, baz
}
let value: SomeEnum = .foo
...
switch value {
case .foo:
return "Foo"
default:↓
return ""
}
// OR
switch value {
default:↓
return ""
}
Exception would be @unknown default because new cases are specified as, well, unknown. Swift compiler will show a warning if new case is added, whereas in case of SomeEnum from the example above it will not.
switch CLLocationManager.authorizationStatus() {
case .authorizedAlways, .authorizedWhenInUse, .notDetermined:
//authorized!
case .denied, .restricted:
//denied!
@unknown default:
fatalError("Unhandled case!")
}
Non-triggering examples:
enum SomeEnum {
case one, two, `default`
}
let value: SomeEnum = .two
...
switch value {
case .one:
return "One"
case .two:
return "Two"
case .default:
return "Default"
}
//OR
if case .default = value {
return "Default"
}
It would be nice, but not required, to also not trigger a warning/error on switches where not all cases can be covered, for example on numeric or String value.
let number: Int = 5
...
switch number {
case 1...4:
return "Number is less than 5"
case 5:
return "Number is 5"
default:
return "Number is greater than 5"
}
let text: String = "Welcome"
...
switch text {
case "Welcome":
return true
case "Goodbye":
return false
default:
return nil
}
Should the rule be configurable, if so what parameters should be configurable?
I don't see any configuration options.
Should the rule be opt-in or enabled by default? Why?
I would say opt-in if the rule would not be triggered on String or numeric switches, otherwise it would be good to have it enabled by default, as it prevents undefined behaviour if a new case to an enum is added.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No implementation file, entry point, or test is named in the issue. Review the requested enum-switch behavior and the numeric, String, @unknown default, and backticked-case examples first; the work is done when the rule's triggering, exceptions, configuration, and opt-in or default status are resolved and covered by tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100