Skip to content

manual_unwrap_or_default does not take into account mutable code in the else branch #12580

@orlp

Description

@orlp

Summary

Nightly clippy with manual_unwrap_or_default seems to ignore mutable code in the else branch.

Lint Name

manual_unwrap_or_default

Reproducer

I tried this code:

fn foo(num_missing: &mut u32, opt: Option<u32>) -> u32 {
    if let Some(r) = opt {
        r
    } else {
        *num_missing += 1;
        0
    }
}

I saw this happen:

warning: if let can be simplified with `.unwrap_or_default()`
 --> src/lib.rs:2:5
  |
2 | /     if let Some(r) = opt {
3 | |         r
4 | |     } else {
5 | |         *num_missing += 1;
6 | |         0
7 | |     }
  | |_____^ help: replace it with: `opt.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:

No lint emitted as it's not applicable.

Version

rustc 1.79.0-nightly (c9f8f3438 2024-03-27)
binary: rustc
commit-hash: c9f8f3438a8134a413aa5d4903e0196e44e37bbc
commit-date: 2024-03-27
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.2

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions