Skip to content

Commit 39ee603

Browse files
committed
needless_borrow: do not contradict dangerous_implicit_autorefs
Rust 1.88 introduces the `dangerous_implicit_autorefs` lint which warns about using implicit autorefs on a place obtained from a raw pointer, as the place may be misaligned for example. This prevents `clippy::needless_borrow` from trigerring in this case, by identifying this case right before emitting the lint. There might be a better way for doing this in the long run, but this will prevent Clippy from contradicting the compiler in the meantime.
1 parent 40bead0 commit 39ee603

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

clippy_lints/src/dereference.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,17 @@ fn report<'tcx>(
997997
);
998998
},
999999
State::DerefedBorrow(state) => {
1000+
// Do not suggest removing a non-mandatory `&` in `&*rawptr` for a method call receiver,
1001+
// as this will make rustc trigger its `dangerous_implicit_autorefs` lint.
1002+
if let ExprKind::AddrOf(BorrowKind::Ref, _, subexpr) = data.first_expr.kind
1003+
&& let ExprKind::Unary(UnOp::Deref, subsubexpr) = subexpr.kind
1004+
&& cx.typeck_results().expr_ty_adjusted(subsubexpr).is_raw_ptr()
1005+
&& let Some(parent_expr) = get_parent_expr(cx, data.first_expr)
1006+
&& matches!(parent_expr.kind, ExprKind::MethodCall(..))
1007+
{
1008+
return;
1009+
}
1010+
10001011
let mut app = Applicability::MachineApplicable;
10011012
let (snip, snip_is_macro) =
10021013
snippet_with_context(cx, expr.span, data.first_expr.span.ctxt(), "..", &mut app);

tests/ui/needless_borrow.fixed

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,15 @@ fn issue_12268() {
289289

290290
// compiler
291291
}
292+
293+
fn issue_14743<T>(slice: &[T]) {
294+
let _ = slice.len();
295+
//~^ needless_borrow
296+
297+
let slice = slice as *const [T];
298+
let _ = unsafe { (&*slice).len() };
299+
300+
// Check that rustc would actually warn if Clippy had suggested removing the reference
301+
#[expect(dangerous_implicit_autorefs)]
302+
let _ = unsafe { (*slice).len() };
303+
}

tests/ui/needless_borrow.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,15 @@ fn issue_12268() {
289289

290290
// compiler
291291
}
292+
293+
fn issue_14743<T>(slice: &[T]) {
294+
let _ = (&slice).len();
295+
//~^ needless_borrow
296+
297+
let slice = slice as *const [T];
298+
let _ = unsafe { (&*slice).len() };
299+
300+
// Check that rustc would actually warn if Clippy had suggested removing the reference
301+
#[expect(dangerous_implicit_autorefs)]
302+
let _ = unsafe { (*slice).len() };
303+
}

tests/ui/needless_borrow.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,11 @@ error: this expression creates a reference which is immediately dereferenced by
169169
LL | option.unwrap_or((&x.0,));
170170
| ^^^^ help: change this to: `x.0`
171171

172-
error: aborting due to 28 previous errors
172+
error: this expression creates a reference which is immediately dereferenced by the compiler
173+
--> tests/ui/needless_borrow.rs:294:13
174+
|
175+
LL | let _ = (&slice).len();
176+
| ^^^^^^^^ help: change this to: `slice`
177+
178+
error: aborting due to 29 previous errors
173179

0 commit comments

Comments
 (0)