rust-lang / rust-lang/rust-clippy

`needless_borrow` suggestion causes error when removing `ref` in match

Open
#9,049 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue from the provided example in src/main.rs with Clippy and the needless_borrow lint. Then locate the lint implementation and its existing tests; done means the lint no longer suggests removing ref when that suggestion causes a compile error, with a regression test covering the match patterns.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.