Rule Request: Prefer `if` and `switch` expressions
Open
Nobody has claimed this yet.
rule-request
- Dominant language
- Swift
- Stars
- 19.7k
- Forks
- 2.3k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 11
Description
In version 5.9, Swift got support for if and switch expressions. A rule to enforce them where allowed should be possible.
In the first implementation, the rule does not need to be configurable. It should be opt-in and might support automatic rewriting.
Triggering:
func f(cond: Bool) -> Int {
if cond {
return 1
} else {
return 2
}
}
func f(cond: Bool) {
let r: Int
if cond {
r = 1
} else {
r = 2
}
}
Non-triggering:
func f(cond: Bool) -> Int {
if cond {
// Nothing
} else {
return 2
}
return 1
}
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
Review SwiftLint's existing rule implementations and how opt-in rules express triggering and non-triggering cases. Use the Swift 5.9 examples in the issue as the initial acceptance criteria, including both return expressions and assigned values, and verify that the empty-branch example remains non-triggering.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100