RS.CLIPPY.EXPECT_USED

Using `.expect()` on `Result` or `Option`, which might be better handled

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: expect_used. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for .expect() or .expect_err() calls on Results and .expect() call on Options.

Why restrict this?

Usually it is better to handle the None or Err case. Still, for a lot of quick-and-dirty code, expect is a good choice, which is why this lint is Allow by default.

result.expect() will let the thread panic on Err values. Normally, you want to implement more sophisticated error handling, and propagate errors upwards with ? operator.

Examples

option.expect("one");
result.expect("one");

Use instead:

option?;

// or

result?;

Past names

  • option_expect_used
  • result_expect_used

Configuration

  • allow-expect-in-consts: Whether expect should be allowed in code always evaluated at compile time

    (default: true)

  • allow-expect-in-tests: Whether expect should be allowed in test functions or #[cfg(test)]

    (default: false)