swift
swift copied to clipboard
Add rule to prefer shorthand `if let` optional unwrapping syntax
Summary
This PR proposes a new rule to prefer the shorthand if let
optional unwrapping syntax added in Swift 5.7:
Omit the right-hand side of the expression when unwrapping an optional property to a non-optional property with the same name.
// WRONG
if
let galaxy = galaxy,
galaxy.name == "Milky Way"
{ … }
guard
let galaxy = galaxy,
galaxy.name == "Milky Way"
else { … }
// RIGHT
if
let galaxy,
galaxy.name == "Milky Way"
{ … }
guard
let galaxy,
galaxy.name == "Milky Way"
else { … }
Reasoning
We are adopting Swift 5.7 soon, so we should adopt guidance for its new language features.
As discussed in the Motivation section of SE-0345, the new shorthand optional unwrapping syntax removes unnecessary boilerplate while retaining clarity. Auto-formatting for this rule is implemented in https://github.com/nicklockwood/SwiftFormat/pull/1165.
Please react with 👍/👎 if you agree or disagree with this proposal.