rust-lang / rust-lang/rust-clippy
Suggestion by unnecessary_unwrap reverses conditions
Open
@Centri3 is already working on this.
Since Jun 23, 2023.
C-bug
I-suggestion-causes-error
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
unnecessary_unwrap suggests to replace with if ... is_none() with if let Some(..) = ..., which reverses conditions.
Reproducer
I tried this code:
fn main() {
let a = Some(1);
let _b = if a.is_none() {
println!("Hello, world!");
99
} else{
a.unwrap()
};
}
I expected to see this happen:
N/A
Instead, this happened:
warning: called `unwrap` on `a` after checking its variant with `is_none`
--> src/lib.rs:7:9
|
3 | let _b = if a.is_none() {
| -------------- help: try: `if let Some(..) = a`
...
7 | a.unwrap()
| ^^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_unwrap
= note: `#[warn(clippy::unnecessary_unwrap)]` on by default
Version
rustc 1.71.0-nightly (18bfe5d8a 2023-05-14)
binary: rustc
commit-hash: 18bfe5d8a9ca0e226171e98f8f4ef071790f3352
commit-date: 2023-05-14
host: x86_64-unknown-linux-gnu
release: 1.71.0-nightly
LLVM version: 16.0.2
Additional Labels
@rustbot label +I-suggestion-causes-error
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.
Assessment
This issue has not been assessed yet.