ptr_as_ptr: fix incorrect suggestion with pointer::cast and macros (#15514)

Fixes https://github.com/rust-lang/rust-clippy/issues/15232

changelog: [`ptr_as_ptr`]: fix incorrect suggestion with `pointer::cast`
and macros
This commit is contained in:
Samuel Tardieu 2025-08-22 17:19:44 +00:00 committed by GitHub
commit 8a8caeb99c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 20 additions and 2 deletions

View file

@ -78,7 +78,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: Msrv)
let method = snippet_with_applicability(cx, qpath_span_without_turbofish(method), "..", &mut app);
("try call directly", format!("{method}{turbofish}()"))
} else {
let cast_expr_sugg = Sugg::hir_with_applicability(cx, cast_expr, "_", &mut app);
let cast_expr_sugg = Sugg::hir_with_context(cx, cast_expr, expr.span.ctxt(), "_", &mut app);
(
"try `pointer::cast`, a safer alternative",

View file

@ -229,3 +229,9 @@ fn issue15283() {
//~^ ptr_as_ptr
}
}
fn issue15232() {
let mut b = Box::new(0i32);
let _ptr = std::ptr::addr_of_mut!(*b).cast::<()>();
//~^ ptr_as_ptr
}

View file

@ -229,3 +229,9 @@ fn issue15283() {
//~^ ptr_as_ptr
}
}
fn issue15232() {
let mut b = Box::new(0i32);
let _ptr = std::ptr::addr_of_mut!(*b) as *mut ();
//~^ ptr_as_ptr
}

View file

@ -199,5 +199,11 @@ error: `as` casting between raw pointers without changing their constness
LL | let _: fn() = std::mem::transmute(std::ptr::null::<()>() as *const u8);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `std::ptr::null::<u8>()`
error: aborting due to 33 previous errors
error: `as` casting between raw pointers without changing their constness
--> tests/ui/ptr_as_ptr.rs:235:16
|
LL | let _ptr = std::ptr::addr_of_mut!(*b) as *mut ();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `std::ptr::addr_of_mut!(*b).cast::<()>()`
error: aborting due to 34 previous errors