rust-lang / rust-lang/rust-clippy
needless_borrow + explicit_auto_deref suggested fix fails to compile
Open
Nobody has claimed this yet.
C-bug
I-suggestion-causes-error
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
warning: failed to automatically apply fixes suggested by rustc to crate `testclippy`
after fixes were automatically applied the compiler reported errors within these files:
* src/lib.rs
This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag
The following errors were reported:
error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
--> src/lib.rs:9:27
|
9 | mem::replace(&mut (&mut self).f, 0)
| ^^^^^^^^^^^ cannot borrow as mutable
|
note: the binding is already a mutable borrow
--> src/lib.rs:8:18
|
8 | pub fn clear(&mut self) -> usize {
| ^^^^^^^^^
help: try removing `&mut` here
--> src/lib.rs:9:27
|
9 | mem::replace(&mut (&mut self).f, 0)
| ^^^^^^^^^^^
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0596`.
Original diagnostics will follow.
warning: this expression borrows a value the compiler would automatically borrow
--> src/lib.rs:9:27
|
9 | mem::replace(&mut (&mut *self).f, 0)
| ^^^^^^^^^^^^ 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: deref which would be done by auto-deref
--> src/lib.rs:9:33
|
9 | mem::replace(&mut (&mut *self).f, 0)
| ^^^^^ help: try: `self`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#explicit_auto_deref
= note: `#[warn(clippy::explicit_auto_deref)]` on by default
Reproducer
I tried this code:
use std::mem;
pub struct A {
f: usize,
}
impl A {
pub fn clear(&mut self) -> usize {
mem::replace(&mut (&mut *self).f, 0)
}
}
(I think it came from rust-analyzer "Inline Function" or "Inline Variable")
Version
rustc 1.89.0-nightly (bc8215286 2025-05-20)
binary: rustc
commit-hash: bc821528634632b4ff8dee5ac1ea4ad90d1b3eb5
commit-date: 2025-05-20
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5
Additional Labels
@rustbot label I-suggestion-causes-error
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
Use the reproducer in src/lib.rs as the starting point and run the needless_borrow and explicit_auto_deref suggestions against it. Trace how those lint suggestions are combined, then verify that applying both suggestions leaves the example compiling without the reported E0596 error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100