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

FP: manual_unwrap_or help code is not correct #6960

Closed
ncaq opened this issue Mar 24, 2021 · 2 comments · Fixed by #7235
Closed

FP: manual_unwrap_or help code is not correct #6960

ncaq opened this issue Mar 24, 2021 · 2 comments · Fixed by #7235
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@ncaq
Copy link

ncaq commented Mar 24, 2021

Lint name: manual_unwrap_or

I tried this code:

use std::panic;
fn main() {
    let default_hook = panic::take_hook();
    panic::set_hook(Box::new(move |panic_info| {
        let message = match panic_info.payload().downcast_ref::<&str>() {
            None => "message is None",
            Some(s) => s,
        };
        // clippy suggest below code, But it is compile error.
        // let message = panic_info
        //     .payload()
        //     .downcast_ref::<&str>()
        //     .unwrap_or("message is None");
        println!("{}", message);
        default_hook(panic_info);
    }));
}

I expected to see this happen: clippy don't warn or suggest correct code.

Instead, this happened:

When don't fix clippy output.

2021-03-24T15:53:11cargo clippy
    Checking clippy-suggest-unwrap-or v0.1.0 (/home/ncaq/Downloads/clippy-suggest-unwrap-or)
warning: this pattern reimplements `Option::unwrap_or`
 --> src/main.rs:5:23
  |
5 |           let message = match panic_info.payload().downcast_ref::<&str>() {
  |  _______________________^
6 | |             None => "message is None",
7 | |             Some(s) => s,
8 | |         };
  | |_________^ help: replace with: `panic_info.payload().downcast_ref::<&str>().unwrap_or("message is None")`
  |
  = note: `#[warn(clippy::manual_unwrap_or)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_unwrap_or

warning: 1 warning emitted

    Finished dev [unoptimized + debuginfo] target(s) in 0.15s

When replace to help code.

2021-03-24T15:53:13cargo clippy
    Checking clippy-suggest-unwrap-or v0.1.0 (/home/ncaq/Downloads/clippy-suggest-unwrap-or)
error[E0308]: mismatched types
  --> src/main.rs:13:24
   |
13 |             .unwrap_or("message is None");
   |                        ^^^^^^^^^^^^^^^^^ expected `&str`, found `str`
   |
   = note: expected reference `&&str`
              found reference `&'static str`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
error: could not compile `clippy-suggest-unwrap-or`

To learn more, run the command again with --verbose.

Meta

  • cargo clippy -V: e.g. clippy 0.0.212 (e1884a8 2020-12-29)
  • rustc -Vv:
    rustc 1.49.0 (e1884a8e3 2020-12-29)
    binary: rustc
    commit-hash: e1884a8e3c3e813aada8254edfa120e85bf5ffca
    commit-date: 2020-12-29
    host: x86_64-unknown-linux-gnu
    release: 1.49.0
    
@ncaq ncaq 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 24, 2021
@TaKO8Ki
Copy link
Member

TaKO8Ki commented Mar 27, 2021

@rustbot claim

@camsteffen
Copy link
Contributor

I believe the fix is to check expr_adjustments().is_empty() on s in the match arm body.

@camsteffen camsteffen added the good-first-issue These issues are a good way to get started with Clippy label May 3, 2021
bors added a commit that referenced this issue May 17, 2021
Fix another manual_unwrap_or deref FP

changelog: none (since this just piggybacks on #7233)

Fixes #6960
bors added a commit that referenced this issue May 17, 2021
Fix another manual_unwrap_or deref FP

changelog: none (since this just piggybacks on #7233)

Fixes #6960
bors added a commit that referenced this issue May 18, 2021
Fix another manual_unwrap_or deref FP

changelog: none (since this just piggybacks on #7233)

Fixes #6960
bors added a commit that referenced this issue May 18, 2021
Fix another manual_unwrap_or deref FP

changelog: none (since this just piggybacks on #7233)

Fixes #6960
@bors bors closed this as completed in 99fb776 May 18, 2021
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 good-first-issue These issues are a good way to get started with Clippy 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.

3 participants