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

needless_borrow suggestion causes error when removing ref in match #9049

Open
5225225 opened this issue Jun 25, 2022 · 2 comments
Open

needless_borrow suggestion causes error when removing ref in match #9049

5225225 opened this issue Jun 25, 2022 · 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

@5225225
Copy link
Contributor

5225225 commented Jun 25, 2022

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

@5225225 5225225 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 Jun 25, 2022
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Jun 25, 2022
@Jarcho
Copy link
Contributor

Jarcho commented Jun 30, 2022

Either matching on self or using ItemKind::Impl(&Impl { generics }) the second pattern would work. The suggestion should include one of those.

@kraktus
Copy link
Contributor

kraktus commented Sep 17, 2022

Strangely (to my eyes), this work too:

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

Which I'd have assumed is the same as an Or match

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

4 participants