If Let Shorthand
ID |
swift.if_let_shorthand |
Severity |
info |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Swift |
Tags |
code-smell, redundant-code, swift-5-7 |
Description
Reports an if let x = x { … } binding whose initializer is a bare
identifier identical to the bound name. Swift 5.7 introduced a
shorthand that drops the redundant = x:
if let foo = foo { ... } // FLAW
if let x = x, let y = y { ... } // FLAW (one per binding)
if let foo { ... } // OK — shorthand
if let foo = self.foo { ... } // OK — member access
if let bar = bar?.advanced(by: 1) { ... } // OK — chain
if let value = foo { ... } // OK — renamed binding
Rationale
When the initializer is a bare reference to the same name being
unwrapped, the = name is pure noise. The shorthand form
(if let name) reads cleaner and makes the intent — "shadow this
optional with its unwrapped value" — explicit. This is the sibling
rule of swift.guard_let_shorthand.