rust-lang / rust-lang/rust-clippy
`needless_borrowed_reference` does not handle mutable references
Open
@Gri-ffin is already working on this.
Since May 12, 2026.
C-bug
C-enhancement
E-medium
I-false-negative
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
needless_borrowed_reference currently only lints on shared references (e.g. &ref x), but mutable references can be linted in some cases.
Lint Name
needless_borrowed_reference
Reproducer
This should lint:
fn f(x: Option<&mut i32>) {
if let Some(&mut ref mut x) = x {
*x = 0;
}
}
And suggest:
fn f(x: Option<&mut i32>) {
if let Some(x) = x {
*x = 0;
}
}
Note that this can only be done if the reference can be moved. None of these should lint:
fn f(x: Option<&mut &mut i32>) {
if let Some(&mut &mut ref mut x) = x {
*x = 0;
}
}
fn f(x: Option<&mut i32>) -> Option<&mut i32> {
if let Some(x) = x {
*x = 0;
}
x
}
struct WithDrop<'a>(Option<&'a mut i32>);
impl Drop for WithDrop<'_> { fn drop(&mut self) {} }
fn f(x: WithDrop) {
if let Some(&mut ref mut x) = x.0 {
*x = 0;
}
}
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.
Assessment
This issue has not been assessed yet.