fleetlint Policy for a fleet of repositories
v0.9
Getting started

slop/forced-unwraps

No forced unwraps outside tests

Severityinfo
Tiersall
Stacksany
Scoperoot
Kindexpr
Sourcelibrary v0.6.0
Selected byfleetlint-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"))
false

Fix

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


Generated by fleetlint docs from catalog v0.6.0.