Skip to content

Commit ea4a7c1

Browse files
committed
fix: borrow_deref_ref suggests wrongly when coerce to mut
1 parent 18061e2 commit ea4a7c1

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

clippy_lints/src/borrow_deref_ref.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ impl<'tcx> LateLintPass<'tcx> for BorrowDerefRef {
7373
}
7474
})
7575
&& !is_from_proc_macro(cx, e)
76+
&& let e_ty = cx.typeck_results().expr_ty_adjusted(e)
77+
// check if the reference is coercing to a mutable reference
78+
&& !matches!(e_ty.kind(), ty::Ref(_, _, Mutability::Mut))
7679
&& let Some(deref_text) = deref_target.span.get_source_text(cx)
7780
{
7881
span_lint_and_then(

tests/ui/borrow_deref_ref.fixed

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,41 @@ fn issue_13584() {
8181
let p = &raw const *s;
8282
let _ = p as *const i8;
8383
}
84+
85+
mod issue_9905 {
86+
use std::{fs, io};
87+
88+
pub enum File {
89+
Stdio,
90+
File(fs::File),
91+
}
92+
93+
impl io::Read for &'_ File {
94+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
95+
match self {
96+
File::Stdio => io::stdin().read(buf),
97+
File::File(file) => (&*file).read(buf),
98+
}
99+
}
100+
}
101+
}
102+
103+
mod issue_11346 {
104+
struct Struct;
105+
106+
impl Struct {
107+
fn foo(self: &mut &Self) {}
108+
}
109+
110+
trait Trait {
111+
fn bar(&mut self) {}
112+
}
113+
114+
impl Trait for &Struct {}
115+
116+
fn bar() {
117+
let s = &Struct;
118+
(&*s).foo();
119+
(&*s).bar();
120+
}
121+
}

tests/ui/borrow_deref_ref.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,41 @@ fn issue_13584() {
8181
let p = &raw const *s;
8282
let _ = p as *const i8;
8383
}
84+
85+
mod issue_9905 {
86+
use std::{fs, io};
87+
88+
pub enum File {
89+
Stdio,
90+
File(fs::File),
91+
}
92+
93+
impl io::Read for &'_ File {
94+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
95+
match self {
96+
File::Stdio => io::stdin().read(buf),
97+
File::File(file) => (&*file).read(buf),
98+
}
99+
}
100+
}
101+
}
102+
103+
mod issue_11346 {
104+
struct Struct;
105+
106+
impl Struct {
107+
fn foo(self: &mut &Self) {}
108+
}
109+
110+
trait Trait {
111+
fn bar(&mut self) {}
112+
}
113+
114+
impl Trait for &Struct {}
115+
116+
fn bar() {
117+
let s = &Struct;
118+
(&*s).foo();
119+
(&*s).bar();
120+
}
121+
}

0 commit comments

Comments
 (0)