rust-lang / rust-lang/rust-clippy

needless_borrow seems to be too eager in beta 1.66

Open
#9,774 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

Since beta 1.66, the needless_borrow lint is warning that "the borrowed expression implements the required traits" when passing a parameter by reference.

This can have an issue because let's say in the example below, the function needs_to_reuse_string will cause a version of very_large_function that takes _s: &String to be instantiated. Since does_not_need_to_reuse_string calls very_large_function with the same parameter type, no extra version is instantiated. If the clippy suggestion is obeyed, does_not_need_to_reuse_string will cause an extra instantiation of very_large_function which takes _s: String.

In summary, I don't get why the needless_borrow lint was broadened to warn about pass-by-reference when both pass-by-value and pass-by-reference are accepted. And even if it would be desirable to some, I don't agree with lumping that behaviour with needless_borrow which I find useful to catch stuff like &already_a_reference.

Lint Name

needless_borrow

Reproducer

I tried this code:

fn very_large_function<S: AsRef<[u8]>>(_s: S) {
    unimplemented!();
}

pub fn does_not_need_to_reuse_string() {
    let s = String::from("hello");
    very_large_function(&s);
}

pub fn needs_to_reuse_string() {
    let s = String::from("hello");
    very_large_function(&s);
    drop(s);
}

I saw this happen:

warning: the borrowed expression implements the required traits
 --> src/lib.rs:7:25
  |
7 |     very_large_function(&s);
  |                         ^^ help: change this to: `s`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
  = note: `#[warn(clippy::needless_borrow)]` on by default

I expected to see no warnings.

Version
rustc 1.66.0-beta.1 (e080cc5a6 2022-11-01)
binary: rustc
commit-hash: e080cc5a659fb760c0bc561b722a790dad35b5e1
commit-date: 2022-11-01
host: x86_64-unknown-linux-gnu
release: 1.66.0-beta.1
LLVM version: 15.0.2
Additional Labels

No response

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

Reproduce the needless_borrow warning from the example in src/lib.rs using the reported beta version, then inspect the lint's implementation and tests. Done means the pass-by-reference call in does_not_need_to_reuse_string no longer receives this warning while genuinely redundant borrows, such as borrowing an already borrowed value, remain covered.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.