Skip to content

manual_unwrap_or_default generates invalid code for nested pattern match #13161

@kluevi

Description

@kluevi

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions