Library code aborts the process with panic! instead of returning an error

ID

rust.correctness.panic

Severity

info

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Error Handling

Language

Rust

Description

Detects panic! invocations in library code. Panics inside fn main, inside a #[test] function and inside the conventional mod tests / mod test unit-test modules are not reported.

Rationale

A panic unwinds through every caller and, under panic = "abort" or inside an FFI boundary, terminates the process outright. A library cannot know whether its caller can afford that, so a recoverable condition reported as a panic removes the caller’s ability to handle it and can take down an unrelated part of the program.

The following code illustrates the pattern detected by this rule:

pub struct Registry {
    slots: Vec<u32>,
}

impl Registry {
    pub fn get(&self, key: &str) -> u32 {
        match key {
            "size" => self.slots.len() as u32,
            // FLAGGED: Library code aborts the process with panic! instead of returning an error
            _ => panic!("unknown key {}", key),
        }
    }
}

Remediation

Return Result<T, E> or Option<T> and let the caller decide. Reserve panic! for broken invariants that no caller could recover from, and document those in the function’s # Panics section.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved