Unused Optional Binding
ID |
swift.unused_optional_binding |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Swift |
Tags |
code-smell, optionals |
Description
Reports an optional binding whose pattern is the wildcard _.
Constructs like if let _ = x or guard let _ = x else are just
existence checks and read better as x != nil.
if let _ = x { ... } // FLAW
guard let _ = x else { ... } // FLAW
if x != nil { ... } // OK
if let value = x { ... } // OK
Rationale
The let _ = … form was idiomatic in early Swift versions, but
the language now offers a direct nil-check, which expresses the
intent more clearly and avoids introducing a no-op binding.