Skip to content

needless_borrow suggestion causes error when removing ref in match #9049

@ghost

Description

Summary

I was running clippy on rustc and ended up with this error

Lint Name

needless_borrow

Reproducer

I tried this code:

pub struct Impl<'a> {generics: &'a Generics<'a> }
pub struct Generics<'a>(&'a String);

pub enum ItemKind<'hir> {
    Impl(&'hir Impl<'hir>),
    Fn(&'hir Generics<'hir>),
}

impl ItemKind<'_> {
    pub fn generics(&self) -> &Generics<'_> {
        match *self {
            ItemKind::Fn(ref generics)
            | ItemKind::Impl(Impl { ref generics }) => generics,
        }
    }
}

fn main() {}

I saw this happen:

warning: this pattern creates a reference to a reference
  --> src/main.rs:12:26
   |
12 |             ItemKind::Fn(ref generics)
   |                          ^^^^^^^^^^^^
13 |             | ItemKind::Impl(Impl { ref generics }) => generics,
   |                                     ^^^^^^^^^^^^
   |
   = note: `#[warn(clippy::needless_borrow)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
help: try this
   |
12 ~             ItemKind::Fn(generics)
13 ~             | ItemKind::Impl(Impl { generics }) => generics,
   |

I expected to see this happen:

Nothing. You can't remove the ref here without causing a compile error. (Or, suggest the correct thing to do here, I'm not too familiar with ref patterns so there might be a correct thing here.)

Version

rustc 1.63.0-nightly (fdca237d5 2022-06-24)
binary: rustc
commit-hash: fdca237d5194bf8a1c9b437ebd2114d1c2ba6195
commit-date: 2022-06-24
host: x86_64-unknown-linux-gnu
release: 1.63.0-nightly
LLVM version: 14.0.5

Additional Labels

@rustbot label +I-suggestion-causes-error

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 haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions