rust-lang / rust-lang/rust-clippy
False positive on unnecessary_unwrap where more than one value gets checked then unwrapped
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
As the title states, #unnecessary_unwrap seems to result in a false positive in cases where 2 or more values get checked / unwrapped
Though this:
if a.is_none() || b.is_none() {
handle_error()
} else {
handle(a.unwrap(), b.unwrap())
}
Could be written to avoid this lint as:
if let Some(va) = a {
if let Some(vb) = b {
handle(va, vb)
} else {
handle_error()
}
} else {
handle_error()
}
In a perfect world I'd write if let (l) = left && let (r) = right, but that is not valid today
That version is far more verbose, and results in duplicating error handling.
I think if the same block triggers this lint more than once, it should be ignored all together
Lint Name
unnecessary_unwrap
Reproducer
I tried this code:
if left.is_none() || left.is_none() {
// Handle the error
} else {
// Call handler
handler(left.unwrap(), left.unwrap())
}
I saw this happen:
warning: called `unwrap` on `left` after checking its variant with `is_none`
--> src\handlers\utils.rs:65:27
|
61 | if left.is_none() || right.is_none() {
| ------------ the check is happening here
...
65 | match handler(left.unwrap(), right.unwrap()) {
| ^^^^^^^^
|
= help: try using `if let` or `match`
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_unwrap
warning: called `unwrap` on `right` after checking its variant with `is_none`
--> src\handlers\utils.rs:65:40
|
61 | if left.is_none() || right.is_none() {
| ------------ the check is happening here
...
65 | match handler(left.unwrap(), right.unwrap()) {
| ^^^^^^^^^
|
= help: try using `if let` or `match`
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_unwrap
I expected to see this happen:
both if let and match would result in a larger, more redundant implementation - if there are 2 or more conditions being checked, this one shouldn't pop
Version
$ rustc -Vv
rustc 1.72.0 (5680fa18f 2023-08-23)
binary: rustc
commit-hash: 5680fa18feaa87f3ff04063800aec256c3d4b4be
commit-date: 2023-08-23
host: x86_64-pc-windows-msvc
release: 1.72.0
LLVM version: 16.0.5
$ cargo clippy --version
clippy 0.1.72 (5680fa18 2023-08-23)
Additional Labels
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the unnecessary_unwrap lint implementation and reproduce the issue with the Rust examples in this report. Determine how multiple checked-and-unwrapped values are handled, then add coverage showing the intended behavior and confirm the lint no longer reports this case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100