Skip to main content
ai-slop/rust-non-test-unwrapmanual fix

Unwrap outside tests.

`.unwrap()` on an `Option` or `Result` in production Rust code. A `None` or `Err` becomes a panic for the caller.

Flagged shape and clean shape.

Each example shows the exact code shape the rule is looking for, then the smallest version that keeps the intent without hiding risk or adding noise.

rust-non-test-unwrap.rs
✗ Flagged
pub fn load_settings(path: &Path) -> Settings {
    let raw = fs::read_to_string(path).unwrap();
    toml::from_str(&raw).unwrap()
}
Why this trips

Deterministic line match: flags `.unwrap()` in non-test Rust files, outside `#[test]`, `#[tokio::test]`, and `#[cfg(test)]` blocks and outside block comments.

rust-non-test-unwrap.rs
✓ Clean
pub fn load_settings(path: &Path) -> Result<Settings, SettingsError> {
    let raw = fs::read_to_string(path)?;
    let settings = toml::from_str(&raw)?;
    Ok(settings)
}
What changed

Propagate with `?`, match the variant you care about, or use `.expect("...")` with a message that names the invariant.

Agents use `.unwrap()` to get past the type checker quickly, as they would in a test, and never come back to handle the failure case.

How this rule is justified.

Rule id

ai-slop/rust-non-test-unwrap

Enforcing engine

ai-slop

Detector strategy

Deterministic line match: flags `.unwrap()` in non-test Rust files, outside `#[test]`, `#[tokio::test]`, and `#[cfg(test)]` blocks and outside block comments.

Legitimate code that is NOT flagged

`.expect("...")`, `write!`/`writeln!(...).unwrap()`, unwraps with a `//` comment in the two lines above, test files and test crates, `build.rs`, and `examples/` or `benches/` are not flagged.

Evidence

Part of aislop's Rust rule set, scored in the strict tier. v0.9.3 stopped flagging unwraps inside `/*! ... */` doc-comment examples.