slop/forced-unwraps
No forced unwraps outside tests
| Severity | info |
| Tiers | all |
| Stacks | any |
| Scope | root |
| Kind | expr |
| Source | library v0.6.0 |
| Selected by | fleetlint-slop@v0.6.0 |
Requirement
Non-test code does not force a value out of an optional or a result: .unwrap() in Rust, !! in Kotlin, try! in Swift. .expect("why") states the invariant and is not read. This rule lists them for review.
Why
A forced unwrap turns a missing value or an error into a crash with no context, at the place least likely to be tested.
Check
For each item in:
codegrep("nontest", r'\.unwrap\(\)').filter(m, m.path.endsWith(".rs")) + codegrep("nontest", r'[\w)\]]!!').filter(m, m.path.matches(r'\.(kt|kts)$')) + codegrep("nontest", r'\btry!\s').filter(m, m.path.endsWith(".swift"))falseFix
Handle the empty or error case, or propagate it (? in Rust, ?:/requireNotNull in Kotlin, try in Swift).
Agent instruction: Propagate the error or handle the empty case; where the value cannot be absent, state the invariant (.expect("...") in Rust, requireNotNull(x) { "..." } in Kotlin).
Sources
- https://rust-lang.github.io/rust-clippy/master/index.html (check, unwrap_used)
- https://detekt.dev/docs/rules/potential-bugs/ (check, UnsafeCallOnNullableType)
- https://fleetlint.org/baseline/#slop (convention)
- https://fleetlint.org/slop/#errors-types-and-robustness
Generated by fleetlint docs from catalog v0.6.0.