rust-lang / rust-lang/rust-clippy
clippy::needless_borrow suggestion/autofix breaks code
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
"the borrowed expression implements the required traits" does not mean that replacing one with another is correct, because non-borrowed implementation may delegate to borrowed.
Reproducer
Repro:
trait Allocatable {
fn allocate(self);
}
fn do_allocate(a: impl Allocatable) {
a.allocate()
}
impl Allocatable for &'_ u32 {
fn allocate(self) {
}
}
impl Allocatable for u32 {
fn allocate(self) {
do_allocate(&self)
}
}
fn main() {
0u32.allocate();
}
cargo clippy --fix --allow-no-vcs
warning: the borrowed expression implements the required traits
--> src/main.rs:14:21
|
14 | do_allocate(&self)
| ^^^^^ 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
Replacing &self with self makes code incorrect: instead of terminating it is now stack overflow or infinite loop depending on optimization level.
Version
rustc 1.72.0-nightly (871b59520 2023-05-31)
binary: rustc
commit-hash: 871b5952023139738f72eba235063575062bc2e9
commit-date: 2023-05-31
host: aarch64-apple-darwin
release: 1.72.0-nightly
LLVM version: 16.0.4
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 reproducing the issue with the supplied Rust example and cargo clippy --fix --allow-no-vcs, then inspect the clippy::needless_borrow lint and its suggestion handling. Done means the lint no longer applies an autofix that changes the example's behavior, while its warning remains correct.
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