Guard Let Shorthand
ID |
swift.guard_let_shorthand |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Swift |
Tags |
code-smell, redundant-code, swift-5-7 |
Description
Reports a guard let x = x else … binding whose initializer is a bare
identifier identical to the bound name. Swift 5.7 introduced a shorthand
spelling that drops the redundant = x:
guard let foo = foo else { return } // FLAW
guard let x = x, let y = y else { return } // FLAW (one per binding)
guard let foo else { return } // OK — shorthand
guard let foo = self.foo else { return } // OK — member access
guard let bar = bar?.advanced(by: 1) else { return } // OK — call chain
guard let value = foo else { return } // 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 (guard let name) reads
cleaner and makes the intent — "shadow this optional with its unwrapped
value" — explicit.