realm / realm/SwiftLint

New `optional_map` rule

Open
#6,343 0 comments 2 reactions 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
New Rule Request

Please add an optional_map rule.

It would change:

  • if let nonOptional = optional {a} else {b} to optional.map {a} ?? b
  • if let nonOptional = optional {a} to optional.map {a}.
  • if let optional {a} else {b} to optional.map {a} ?? b
  • if let optional {a} to optional.map {a}.

(same for all variants of if var …)

If a can be nil, then flatMap (instead of map) would be used.

It could also apply to switches that are the equivalent of if-else, but I assume those are rare.

Maybe a rule already exists to convert from such a switch to an equivalent if-else

Maybe this rule could be limited to assignments / returns / etc., or have parameters to limit its scope.

I imagine that it should be opt-in, because maybe not everyone will agree with my preference for the concision of optional.map, but if there was a consensus that optional.map should be used (at least in certain circumstances), then it could be a default rule.

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

No files, tests, or entry points are named. Start by locating SwiftLint's existing rule implementations and tests, then resolve the scope and opt-in behavior for the listed if-let and if-var forms, including when to use map versus flatMap; done means the agreed forms are detected and covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.