RS.CLIPPY.UNWRAP_USED
Using `.unwrap()` on `Result` or `Option`, which should at least get a better message using `expect()`
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)