rust-lang / rust-lang/rust-clippy

needless_borrow false negative on fn that takes AsRef<..> and gets &var

Open
#6,810 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-enhancement E-medium I-false-negative
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Lint name: needless_borrow

I tried this code:

I wonder if the lint should warn on passing &arg to a function that takes AsRef<..> where the & would not be needed

#![warn(clippy::needless_borrow)]

pub fn main() {
    fn is_hello<T: AsRef<str>>(s: T) {
        assert_eq!("hello", s.as_ref());
    }
    let s: &String = &"hello".to_string();

    is_hello(&&&&s); // warn?
    is_hello(&s); // warn?
    is_hello(s); // ok
    is_hello(&"hello"); // warn?
    is_hello("hello"); // ok
}

Meta
  • cargo clippy -V: clippy 0.1.52 (9c09c1f 2021-02-26)
  • rustc -Vv:
rustc 1.52.0-nightly (9c09c1f7c 2021-02-26)
binary: rustc
commit-hash: 9c09c1f7cfcf9de0522bcd1cfda32b552195c464
commit-date: 2021-02-26
host: x86_64-unknown-linux-gnu
release: 1.52.0-nightly
LLVM version: 11.0.1

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 Rust example with the needless_borrow lint and run Clippy against each marked call to confirm the false negatives. Trace the lint implementation and existing regression tests, then add coverage for the unnecessary borrows involving AsRef<str> and make the expected warnings match the intended behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.