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 false positive with a nested pattern #13031

Closed
magicant opened this issue Jul 2, 2024 · 2 comments
Closed

manual_unwrap_or_default false positive with a nested pattern #13031

magicant opened this issue Jul 2, 2024 · 2 comments
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@magicant
Copy link

magicant commented Jul 2, 2024

Summary

The manual_unwrap_or_default lint seems to incorrectly suggest replacing a pattern match with an unwrap_or_default call if the branch returning the non-default value has a nested pattern like Some(Some(x)) or Some(MyEnum::MyVariant(x)).

Lint Name

manual_unwrap_or_default

Reproducer

I tried this code:

fn main() {
    let v = match Some(Some(42)) {
        Some(Some(x)) => x,
        _ => Default::default(),
    };
    println!("{v}");
}

I saw this happen:

warning: failed to automatically apply fixes suggested by rustc to crate `clippy_tmp`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0277]: `std::option::Option<{integer}>` doesn't implement `std::fmt::Display`
 --> src/main.rs:3:15
  |
3 |     println!("{v}");
  |               ^^^ `std::option::Option<{integer}>` cannot be formatted with the default formatter
  |
  = help: the trait `std::fmt::Display` is not implemented for `std::option::Option<{integer}>`
  = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
  = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
Original diagnostics will follow.

warning: match can be simplified with `.unwrap_or_default()`
 --> src/main.rs:2:13
  |
2 |       let v = match Some(Some(42)) {
  |  _____________^
3 | |         Some(Some(x)) => x,
4 | |         _ => Default::default(),
5 | |     };
  | |_____^ help: replace it with: `Some(Some(42)).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

warning: `clippy-tmp` (bin "clippy-tmp" test) generated 1 warning (run `cargo clippy --fix --bin "clippy-tmp" --tests` to apply 1 suggestion)

I expected to see this happen:

No suggestions should be produced.

Version

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

Additional Labels

@rustbot label +I-suggestion-causes-error

@magicant magicant 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 Jul 2, 2024
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Jul 2, 2024
@y21
Copy link
Member

y21 commented Jul 2, 2024

This bug was fixed in #12961 (duplicate of #12928). Thanks for reporting this regardless!

@y21 y21 closed this as completed Jul 2, 2024
@magicant
Copy link
Author

magicant commented Jul 2, 2024

Great, thank you for the quick attention!

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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

3 participants