Skip to content

manual_unwrap_or false positive with deref coercion #7228

@Xiretza

Description

@Xiretza

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
    

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