rust-lang / rust-lang/rust-clippy
needless_borrow false positives when borrowing a struct before calling a function on that struct
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
needless_borrow seems to not notice cases where taking a borrow of a struct can change which function is called. This can occur when a struct has an impl block declaring some function foo and also impl some trait which declares foo, potentially with a different signature.
Attached is a toy repro but I just hit this in a production codebase.
Lint Name
needless_borrow
Reproducer
I tried this code:
struct Yeah {
pub val: i32
}
impl Yeah {
const fn neg(&self) -> Self {
Self { val: -self.val }
}
}
impl std::ops::Neg for Yeah {
type Output = Self;
fn neg(self) -> Self {
(&self).neg()
}
}
fn main() {
let y = Yeah { val: 5 };
println!("{}", (&y).neg().val);
use std::ops::Neg;
println!("{}", y.neg().val);
}
Clippy complains:
warning: this expression borrows a value the compiler would automatically borrow
--> src/main.rs:17:9
|
17 | (&self).neg()
| ^^^^^^^ help: change this to: `self`
|
= 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
warning: this expression borrows a value the compiler would automatically borrow
--> src/main.rs:23:20
|
23 | println!("{}", (&y).neg().val);
| ^^^^ help: change this to: `y`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
But removing either of these borrows breaks the program!
In the first, removing it causes a stack overflow at runtime due to infinite recusion - std::ops::Neg::neg will call itself, instead of Yeah::neg.
In the second, removing it causes compilation failure because the wrong neg is invoked, which means y is moved from instead of used by reference.
Version
rustc 1.77.1 (7cf61ebde 2024-03-27)
binary: rustc
commit-hash: 7cf61ebde7b22796c69757901dd346d0fe70bd97
commit-date: 2024-03-27
host: x86_64-unknown-linux-gnu
release: 1.77.1
LLVM version: 17.0.6
Additional Labels
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the Rust reproducer from the issue and inspect the needless_borrow lint behavior for the two neg calls. Confirm that removing either borrow changes method resolution, then add coverage showing the lint does not suggest either change while preserving its existing diagnostics.
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