Skip to content

New optional_map rule #6343

@rgoldberg

Description

@rgoldberg

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    rule-requestRequests for a new rules.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions