Skip to content

manual_unwrap_or_default does not consider type changes and suggests wrong code #12928

@sdroege

Description

@sdroege

Summary

See code below

Lint Name

manual_unwrap_or_default

Reproducer

I tried this code:

fn main() {
    let x = Some((1, 2));
    let y = if let Some((a, _)) = x {
        a
    } else {
        0
    };
    
    println!("{y}");
}

I saw this happen:

warning: if let can be simplified with `.unwrap_or_default()`
 --> src/main.rs:3:13
  |
3 |       let y = if let Some((a, _)) = x {
  |  _____________^
4 | |         a
5 | |     } else {
6 | |         0
7 | |     };
  | |_____^ help: replace it with: `x.unwrap_or_default()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_unwrap_or_default
  = note: `#[warn(clippy::manual_unwrap_or_default)]` on by default

I expected to see this happen: [nothing]

Version

rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7

Additional Labels

@rustbot label +I-suggestion-causes-error

Activity

added
C-bugCategory: Clippy is not doing the correct thing
I-false-positiveIssue: The lint was triggered on code it shouldn't have
on Jun 14, 2024
added
I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied
on Jun 14, 2024
GuillaumeGomez

GuillaumeGomez commented on Jun 19, 2024

@GuillaumeGomez
Member

Taking a look.

added a commit that references this issue on Jun 21, 2024
0ce07f6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @sdroege@GuillaumeGomez@rustbot

    Issue actions

      `manual_unwrap_or_default` does not consider type changes and suggests wrong code · Issue #12928 · rust-lang/rust-clippy