Rule Request: Prefer `for case let` when nil tuple members are skipped
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
Rule Request
We should prefer for case let when nil tuple members are skipped. For example, the general pattern
for (n1, n2, ...) in <sequence> {
guard let nX = nX else {
continue
}
// body
}
// or
for (n1, n2, ...) in <sequence> where nX != nil {
// body
}
should be corrected to:
for case let (n1, n2, nX?) in <sequence> {
// body
}
- Why should this rule be added? Share links to existing discussion about what
the community thinks about this.
Rule should be added because it makes for more readable and concise code.
- Provide several examples of what would and wouldn't trigger violations.
Here's a before/after commit from SwiftLint itself (a02d4e17ddcb35ddd2006da60ce83efa5e8f58c4)
-for (pattern, operatorString) in [toPattern, toNotPattern] {
- guard let operatorString = operatorString else {
- continue
- }
-
+for case let (pattern, operatorString?) in [toPattern, toNotPattern] {
Would trigger:
for (n1, n2, ...) in <sequence> {
guard let nX = nX else {
continue
}
// body
}
// or
for (n1, n2, ...) in <sequence> where nX != nil {
// body
}
Wouldn't trigger:
for case let (n1, n2, nX?) in <sequence> {
// body
}
// or
for (n1, n2, ...) in <sequence> {
// body, not skipping nX immediately or unconditionally
}
- Should the rule be configurable, if so what parameters should be configurable?
Should only be configurable on severity.
- Should the rule be opt-in or enabled by default? Why?
Enabled by default. I can't think of too many cases where you wouldn't want this.
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
Start by examining the referenced SwiftLint commit a02d4e17ddcb35ddd2006da60ce83efa5e8f58c4 and the repository's existing rule implementations and tests. Determine how comparable loop-pattern rules are structured and tested. Done means the requested for case let preference is detected, non-matching examples are accepted, and severity is configurable.
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
- 35/100