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 false positive with deref coercion #7228

Closed
Xiretza opened this issue May 16, 2021 · 0 comments · Fixed by #7233
Closed

manual_unwrap_or false positive with deref coercion #7228

Xiretza opened this issue May 16, 2021 · 0 comments · Fixed by #7233
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

@Xiretza
Copy link
Contributor

Xiretza commented May 16, 2021

Lint name: manual_unwrap_or

I tried this code:

use std::rc::Rc;

pub fn format_name(name: Option<&Rc<str>>) -> &str {
    match name {
        None => "<anon>",
        Some(name) => name,
    }
}

I expected to see this happen: clippy either doesn't complain, or offers a working alternative.

Instead, this happened: Clippy suggests the following:

warning: this pattern reimplements `Option::unwrap_or`
 --> src/lib.rs:4:5
  |
4 | /     match name {
5 | |         None => "<anon>",
6 | |         Some(name) => name,
7 | |     }
  | |_____^ help: replace with: `name.unwrap_or("<anon>")`
  |
  = 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

If the suggestion is followed, compilation fails:

error[E0308]: mismatched types
 --> src/lib.rs:4:20
  |
4 |     name.unwrap_or("<anon>")
  |                    ^^^^^^^^ expected struct `std::rc::Rc`, found `str`
  |
  = note: expected reference `&std::rc::Rc<str>`
             found reference `&'static str`

Meta

  • cargo clippy -V: e.g. clippy 0.1.54 (5c02926 2021-05-11)
  • rustc -Vv:
    rustc 1.54.0-nightly (5c0292654 2021-05-11)
    binary: rustc
    commit-hash: 5c029265465301fe9cb3960ce2a5da6c99b8dcf2
    commit-date: 2021-05-11
    host: x86_64-unknown-linux-gnu
    release: 1.54.0-nightly
    LLVM version: 12.0.1
    
@Xiretza Xiretza 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 May 16, 2021
bors added a commit that referenced this issue May 17, 2021
…ercion, r=flip1995

Fix a `manual_unwrap_or` FP with deref coercion

Fix #7228.

changelog: Fix a [`manual_unwrap_or`] FP with deref coercion
@bors bors closed this as completed in cec8d95 May 17, 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 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