Skip to content

needless_borrow: do not contradict dangerous_implicit_autorefs #14810

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions clippy_lints/src/dereference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,15 @@ fn report<'tcx>(
);
},
State::DerefedBorrow(state) => {
// Do not suggest removing a non-mandatory `&` in `&*rawptr` in an `unside` context,
// as this may make rustc trigger its `dangerous_implicit_autorefs` lint.
if let ExprKind::AddrOf(BorrowKind::Ref, _, subexpr) = data.first_expr.kind
&& let ExprKind::Unary(UnOp::Deref, subsubexpr) = subexpr.kind
&& cx.typeck_results().expr_ty_adjusted(subsubexpr).is_raw_ptr()
{
return;
}

let mut app = Applicability::MachineApplicable;
let (snip, snip_is_macro) =
snippet_with_context(cx, expr.span, data.first_expr.span.ctxt(), "..", &mut app);
Expand Down
15 changes: 12 additions & 3 deletions tests/ui/needless_borrow.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,6 @@ fn main() {
let x = (1, 2);
let _ = x.0;
//~^ needless_borrow
let x = &x as *const (i32, i32);
let _ = unsafe { (*x).0 };
//~^ needless_borrow

// Issue #8367
trait Foo {
Expand Down Expand Up @@ -289,3 +286,15 @@ fn issue_12268() {

// compiler
}

fn issue_14743<T>(slice: &[T]) {
let _ = slice.len();
//~^ needless_borrow

let slice = slice as *const [T];
let _ = unsafe { (&*slice).len() };

// Check that rustc would actually warn if Clippy had suggested removing the reference
#[expect(dangerous_implicit_autorefs)]
let _ = unsafe { (*slice).len() };
}
15 changes: 12 additions & 3 deletions tests/ui/needless_borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,6 @@ fn main() {
let x = (1, 2);
let _ = (&x).0;
//~^ needless_borrow
let x = &x as *const (i32, i32);
let _ = unsafe { (&*x).0 };
//~^ needless_borrow

// Issue #8367
trait Foo {
Expand Down Expand Up @@ -289,3 +286,15 @@ fn issue_12268() {

// compiler
}

fn issue_14743<T>(slice: &[T]) {
let _ = (&slice).len();
//~^ needless_borrow

let slice = slice as *const [T];
let _ = unsafe { (&*slice).len() };

// Check that rustc would actually warn if Clippy had suggested removing the reference
#[expect(dangerous_implicit_autorefs)]
let _ = unsafe { (*slice).len() };
}
32 changes: 16 additions & 16 deletions tests/ui/needless_borrow.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -103,71 +103,71 @@ error: this expression borrows a value the compiler would automatically borrow
LL | let _ = (&x).0;
| ^^^^ help: change this to: `x`

error: this expression borrows a value the compiler would automatically borrow
--> tests/ui/needless_borrow.rs:111:22
|
LL | let _ = unsafe { (&*x).0 };
| ^^^^^ help: change this to: `(*x)`

error: this expression creates a reference which is immediately dereferenced by the compiler
--> tests/ui/needless_borrow.rs:122:5
--> tests/ui/needless_borrow.rs:119:5
|
LL | (&&()).foo();
| ^^^^^^ help: change this to: `(&())`

error: this expression creates a reference which is immediately dereferenced by the compiler
--> tests/ui/needless_borrow.rs:132:5
--> tests/ui/needless_borrow.rs:129:5
|
LL | (&&5).foo();
| ^^^^^ help: change this to: `(&5)`

error: this expression creates a reference which is immediately dereferenced by the compiler
--> tests/ui/needless_borrow.rs:159:23
--> tests/ui/needless_borrow.rs:156:23
|
LL | let x: (&str,) = (&"",);
| ^^^ help: change this to: `""`

error: this expression borrows a value the compiler would automatically borrow
--> tests/ui/needless_borrow.rs:202:13
--> tests/ui/needless_borrow.rs:199:13
|
LL | (&self.f)()
| ^^^^^^^^^ help: change this to: `(self.f)`

error: this expression borrows a value the compiler would automatically borrow
--> tests/ui/needless_borrow.rs:212:13
--> tests/ui/needless_borrow.rs:209:13
|
LL | (&mut self.f)()
| ^^^^^^^^^^^^^ help: change this to: `(self.f)`

error: this expression borrows a value the compiler would automatically borrow
--> tests/ui/needless_borrow.rs:250:22
--> tests/ui/needless_borrow.rs:247:22
|
LL | let _ = &mut (&mut { x.u }).x;
| ^^^^^^^^^^^^^^ help: change this to: `{ x.u }`

error: this expression borrows a value the compiler would automatically borrow
--> tests/ui/needless_borrow.rs:258:22
--> tests/ui/needless_borrow.rs:255:22
|
LL | let _ = &mut (&mut { x.u }).x;
| ^^^^^^^^^^^^^^ help: change this to: `{ x.u }`

error: this expression borrows a value the compiler would automatically borrow
--> tests/ui/needless_borrow.rs:263:22
--> tests/ui/needless_borrow.rs:260:22
|
LL | let _ = &mut (&mut x.u).x;
| ^^^^^^^^^^ help: change this to: `x.u`

error: this expression borrows a value the compiler would automatically borrow
--> tests/ui/needless_borrow.rs:265:22
--> tests/ui/needless_borrow.rs:262:22
|
LL | let _ = &mut (&mut { x.u }).x;
| ^^^^^^^^^^^^^^ help: change this to: `{ x.u }`

error: this expression creates a reference which is immediately dereferenced by the compiler
--> tests/ui/needless_borrow.rs:287:23
--> tests/ui/needless_borrow.rs:284:23
|
LL | option.unwrap_or((&x.0,));
| ^^^^ help: change this to: `x.0`

error: this expression creates a reference which is immediately dereferenced by the compiler
--> tests/ui/needless_borrow.rs:291:13
|
LL | let _ = (&slice).len();
| ^^^^^^^^ help: change this to: `slice`

error: aborting due to 28 previous errors