Clippy lints / Complexity
Clippy lint · Complexity · checked with Clippy 0.1.99
clippy::unnecessary_unwrap: Checks for calls of unwrap[_err]() that cannot fail
What it meansChecks for calls of unwrap[_err]() that cannot fail. It is in the complexity group and warns by default.
Does it matter?
Using if let or match is more idiomatic.
Code that does something simple in a complicated way. Warns by default.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
if option.is_some() {
do_something_with(option.unwrap())
}
Write this instead
if let Some(value) = option {
do_something_with(value)
}
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::unnecessary_unwrap)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
unnecessary_unwrap = "allow"One run
cargo clippy -- -A clippy::unnecessary-unwrapThe [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
msrv: The minimum rust version that the project supports. Defaults to the rust-version field in Cargo.toml
Clippy's own documentation
What it does
Checks for calls of unwrap[_err]() that cannot fail.
Why is this bad?
Using if let or match is more idiomatic.
Example
if option.is_some() {
do_something_with(option.unwrap())
}
Could be written:
if let Some(value) = option {
do_something_with(value)
}
Configuration
-
msrv: The minimum rust version that the project supports. Defaults to therust-versionfield inCargo.toml(default:
current version)
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- too_many_argumentsChecks for functions with too many parameters
- clone_on_copyusing `clone` on type `u64` which implements the `Copy` trait
- type_complexityChecks for types used in structs, parameters and let declarations above a certain complexity threshold
- unnecessary_min_or_max`0` is never greater than `7_u32` and has therefore no effect
- unnecessary_operationChecks for expression statements that can be reduced to a sub-expression
- unnecessary_sort_byChecks for usage of Vec::sort_by passing in a closure which compares the two arguments, either directly or indirectly
- unnecessary_unwrap_uncheckedusage of `unwrap_unchecked` when an `_unchecked` variant of the function exists
- unneeded_wildcard_patternthis pattern is unneeded as the `..` pattern can match that element
- unused_format_specsformat specifiers have no effect on `format_args!()`
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::too_many_argumentsChecks for functions with too many parametersRead it next →Did this answer your question?