realm / realm/SwiftLint

Rule Request: Prefer `for case let` when nil tuple members are skipped

Open
#1,977 0 comments 1 reaction 0 assignees View on GitHub

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

New Issue Checklist
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
}
  1. 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.

  1. 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
}
  1. Should the rule be configurable, if so what parameters should be configurable?

Should only be configurable on severity.

  1. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.