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 does not take into account mutable code in the else branch #12580

Closed
orlp opened this issue Mar 28, 2024 · 0 comments · Fixed by #12579
Closed

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

orlp opened this issue Mar 28, 2024 · 0 comments · Fixed by #12579
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@orlp
Copy link

orlp commented Mar 28, 2024

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

@orlp orlp added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Mar 28, 2024
@bors bors closed this as completed in 88d842e Mar 30, 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 I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant