rust-lang / rust-lang/rust-clippy

needless_borrow track through function call

Open
#10,464 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

Currently, the needless_borrow check is described as:

What it does
Checks for address of operations (&) that are going to be dereferenced immediately by the compiler.

This misses cases where we have borrows earlier in a function that aren't used. For example,

pub struct S;

pub fn f(_: &S) {}

pub fn g() {
    let s = &&S;
    f(s);
}

pub fn h() {
    let s = S;
    f(&&s);
}

In the above example, needless_borrow is triggered on function h, but not on function g.

Lint Name

needless_borrow

Reproducer

Can use same example code block given above and verify that the needless_borrow warning is triggered on function h but not on function g:

pub struct S;

pub fn f(_: &S) {}

pub fn g() {
    let s = &&S;
    f(s);
}

pub fn h() {
    let s = S;
    f(&&s);
}
Version
rustc 1.67.1 (d5a82bbd2 2023-02-07)
cargo 1.67.1 (8ecd4f20a 2023-01-10)

System information:
 Server Version: 20.10.18
 Storage Driver: overlay2
  Backing Filesystem: xfs
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Kernel Version: 5.15.0-1030-aws
 Operating System: Ubuntu 20.04.5 LTS
 OSType: linux
 Architecture: x86_64

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 with the needless_borrow lint and the supplied Rust reproducer. Trace how the borrow is followed through the f(s) call, then verify completion by checking that g is diagnosed as well as h.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.