Clippy lints / Restriction
Clippy lint · Restriction · checked with Clippy 0.1.99
clippy::unwrap_used: Checks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on Options
What it meansChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on Options. It is in the restriction group and off by default.
Does it matter?
It is better to handle the None or Err case, or at least call .expect(_) with a more helpful message. Still, for a lot of quick-and-dirty code, unwrap is a good choice, which is why this lint is Allow by default. result.unwrap() will let the thread panic on Err values. Normally, you want to implement more sophisticated error handling, and propagate errors upwards with ? operator. Even if you want to panic on errors, not all Errors implement good messages on display. Therefore, it may be beneficial to look at the places where they may get displayed. Activate this lint to do just that.
Lints that forbid language features for specific codebases (no unwrap, no indexing...). Off by default; pick them one by one, never the whole group.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
option.unwrap();
result.unwrap();
Write this instead
option.expect("more helpful message");
result.expect("more helpful message");
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
unwrap_used = "warn"In code
#![warn(clippy::unwrap_used)]One run
cargo clippy -- -W clippy::unwrap-usedThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Settings in clippy.toml
allow-unwrap-in-consts: Whether unwrap should be allowed in code always evaluated at compile timeallow-unwrap-in-tests: Whether unwrap should be allowed in test functions or #[cfg(test)]allow-unwrap-types: List of types to allow unwrap() and expect() on.
Clippy's own documentation
What it does
Checks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on Options.
Why restrict this?
It is better to handle the None or Err case,
or at least call .expect(_) with a more helpful message. Still, for a lot of
quick-and-dirty code, unwrap is a good choice, which is why this lint is
Allow by default.
result.unwrap() will let the thread panic on Err values.
Normally, you want to implement more sophisticated error handling,
and propagate errors upwards with ? operator.
Even if you want to panic on errors, not all Errors implement good
messages on display. Therefore, it may be beneficial to look at the places
where they may get displayed. Activate this lint to do just that.
Examples
option.unwrap();
result.unwrap();
Use instead:
option.expect("more helpful message");
result.expect("more helpful message");
If expect_used is enabled, instead:
option?;
// or
result?;
Past names
- option_unwrap_used
- result_unwrap_used
Configuration
-
allow-unwrap-in-consts: Whetherunwrapshould be allowed in code always evaluated at compile time(default:
true) -
allow-unwrap-in-tests: Whetherunwrapshould be allowed in test functions or#[cfg(test)](default:
false) -
allow-unwrap-types: List of types to allowunwrap()andexpect()on.
Example
allow-unwrap-types = [ "std::sync::LockResult" ]
(default: [])
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- expect_usedChecks for .expect() or .expect_err() calls on Results and .expect() call on Options
- module_name_repetitionsDetects public item names that are prefixed or suffixed by the containing public module’s name
- cognitive_complexityWe used to think it measured how hard a method is to understand
- unused_result_okChecks for calls to Result::ok() without using the returned Option
- unused_trait_namesimporting trait that is only used anonymously
- unwrap_in_result`expect` used in a function that returns a `Result`
- use_debugChecks for usage of Debug formatting
- verbose_file_readsChecks for usage of File::read_to_end and File::read_to_string
- wildcard_enum_match_armChecks for wildcard enum matches using _
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::expect_usedChecks for .expect() or .expect_err() calls on Results and .expect() call on OptionsRead it next →Did this answer your question?