Skip to content

Suggestion by unnecessary_unwrap reverses conditions #11006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Gowee opened this issue Jun 22, 2023 · 1 comment
Open

Suggestion by unnecessary_unwrap reverses conditions #11006

Gowee opened this issue Jun 22, 2023 · 1 comment
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@Gowee
Copy link

Gowee commented Jun 22, 2023

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

@Gowee Gowee added the C-bug Category: Clippy is not doing the correct thing label Jun 22, 2023
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Jun 22, 2023
@Centri3
Copy link
Member

Centri3 commented Jun 23, 2023

Also looks like an issue with is_err.
@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants