Skip to content

fix: borrow_deref_ref suggests wrongly when coerce to mut #14403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion clippy_lints/src/borrow_deref_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::reference::DEREF_ADDROF;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::SpanRangeExt;
use clippy_utils::ty::implements_trait;
use clippy_utils::{get_parent_expr, is_from_proc_macro, is_lint_allowed};
use clippy_utils::{get_parent_expr, is_from_proc_macro, is_lint_allowed, is_mutable};
use rustc_errors::Applicability;
use rustc_hir::{BorrowKind, ExprKind, UnOp};
use rustc_lint::{LateContext, LateLintPass};
Expand Down Expand Up @@ -73,6 +73,9 @@ impl<'tcx> LateLintPass<'tcx> for BorrowDerefRef {
}
})
&& !is_from_proc_macro(cx, e)
&& let e_ty = cx.typeck_results().expr_ty_adjusted(e)
// check if the reference is coercing to a mutable reference
&& (!matches!(e_ty.kind(), ty::Ref(_, _, Mutability::Mut)) || is_mutable(cx, deref_target))
&& let Some(deref_text) = deref_target.span.get_source_text(cx)
{
span_lint_and_then(
Expand Down
43 changes: 43 additions & 0 deletions tests/ui/borrow_deref_ref.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,46 @@ fn issue_13584() {
let p = &raw const *s;
let _ = p as *const i8;
}

mod issue_9905 {
use std::{fs, io};

pub enum File {
Stdio,
File(fs::File),
}

impl io::Read for &'_ File {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self {
File::Stdio => io::stdin().read(buf),
File::File(file) => (&*file).read(buf),
}
}
}
}

mod issue_11346 {
struct Struct;

impl Struct {
fn foo(self: &mut &Self) {}
}

trait Trait {
fn bar(&mut self) {}
}

impl Trait for &Struct {}

fn bar() {
let s = &Struct;
(&*s).foo();
(&*s).bar();

let mut s = &Struct;
s.foo(); // To avoid a warning about `s` not needing to be mutable
s.foo();
//~^ borrow_deref_ref
}
}
43 changes: 43 additions & 0 deletions tests/ui/borrow_deref_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,46 @@ fn issue_13584() {
let p = &raw const *s;
let _ = p as *const i8;
}

mod issue_9905 {
use std::{fs, io};

pub enum File {
Stdio,
File(fs::File),
}

impl io::Read for &'_ File {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self {
File::Stdio => io::stdin().read(buf),
File::File(file) => (&*file).read(buf),
}
}
}
}

mod issue_11346 {
struct Struct;

impl Struct {
fn foo(self: &mut &Self) {}
}

trait Trait {
fn bar(&mut self) {}
}

impl Trait for &Struct {}

fn bar() {
let s = &Struct;
(&*s).foo();
(&*s).bar();

let mut s = &Struct;
s.foo(); // To avoid a warning about `s` not needing to be mutable
(&*s).foo();
//~^ borrow_deref_ref
}
}
8 changes: 7 additions & 1 deletion tests/ui/borrow_deref_ref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@ error: deref on an immutable reference
LL | let addr_y = &&*x as *const _ as usize; // assert ok
| ^^^ help: if you would like to reborrow, try removing `&*`: `x`

error: aborting due to 3 previous errors
error: deref on an immutable reference
--> tests/ui/borrow_deref_ref.rs:123:9
|
LL | (&*s).foo();
| ^^^^^ help: if you would like to reborrow, try removing `&*`: `s`

error: aborting due to 4 previous errors