expect() panics instead of handling the error case

ID

rust.correctness.expect_used

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Error Handling

Language

Rust

Description

expect() aborts the whole thread when the value is None or Err, turning a recoverable condition into a crash. Propagate the error with ?, or handle both outcomes with match, unwrap_or_else or ok_or.

Rationale

expect() aborts the whole thread when the value is None or Err, turning a recoverable condition into a crash. Propagate the error with ?, or handle both outcomes with match, unwrap_or_else or ok_or.

The following code illustrates the pattern detected by this rule:

use std::collections::HashMap;
fn port(env: &HashMap<String, String>) -> u16 {
    // FLAGGED: expect() panics instead of handling the error case
    let raw = env.get("PORT").expect("PORT must be set");
    // FLAGGED: expect() panics instead of handling the error case
    raw.parse::<u16>().expect("PORT must be a number")
}

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved