Skip to content
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

manual_unwrap_or_default generates invalid code for nested pattern match #13161

Closed
kluevi opened this issue Jul 26, 2024 · 2 comments
Closed
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@kluevi
Copy link

kluevi commented Jul 26, 2024

Summary

clippy fails to understand nested pattern matches. It treats if let Some(Some(a)) the same as if let Some(a), resulting in invalid code when --fix-ing.

Reproducer

I tried this code:

fn main() {
    let foo = Some(Some(5));
    let num: i32 = if let Some(Some(num)) = foo {
        num
    } else {
        0
    };
    println!("{num}");
}

I expected to see this happen:

Nothing, really.

Instead, this happened:

The following errors were reported:
error[E0308]: mismatched types
 --> src/main.rs:3:20
  |
3 |     let num: i32 = foo.unwrap_or_default();
  |              ---   ^^^^^^^^^^^^^^^^^^^^^^^ expected `i32`, found `Option<{integer}>`
  |              |
  |              expected due to this
  |
  = note: expected type `i32`
             found enum `std::option::Option<{integer}>`
help: consider using `Option::expect` to unwrap the `std::option::Option<{integer}>` value, panicking if the value is an `Option::None`
  |
3 |     let num: i32 = foo.unwrap_or_default().expect("REASON");
  |                                           +++++++++++++++++

Version

No response

Additional Labels

No response

@kluevi kluevi added the C-bug Category: Clippy is not doing the correct thing label Jul 26, 2024
@y21
Copy link
Member

y21 commented Jul 26, 2024

This was fixed in #12961. Since that was backported I believe it should be fixed on all channels now (stable, beta, nightly). Do you still see this after updating to a newer version?

@kluevi
Copy link
Author

kluevi commented Jul 26, 2024

Indeed, it was gone after a rustup update.

@kluevi kluevi closed this as completed Jul 26, 2024
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
Projects
None yet
Development

No branches or pull requests

2 participants