diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2025-08-22 17:19:44 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-22 17:19:44 +0000 |
| commit | 8a8caeb99c8f4f5460520f411e910e58764ebc09 (patch) | |
| tree | 4a54cbbc45af345f2c9436d0ae21c3c2376dff8d | |
| parent | 7997f33b5d59a6c49eaa1554ab2ad7c688b3eb5d (diff) | |
| parent | 443860d1ae4310e12188e05278fc34fd01ace1c6 (diff) | |
| download | rust-8a8caeb99c8f4f5460520f411e910e58764ebc09.tar.gz rust-8a8caeb99c8f4f5460520f411e910e58764ebc09.zip | |
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
| -rw-r--r-- | clippy_lints/src/casts/ptr_as_ptr.rs | 2 | ||||
| -rw-r--r-- | tests/ui/ptr_as_ptr.fixed | 6 | ||||
| -rw-r--r-- | tests/ui/ptr_as_ptr.rs | 6 | ||||
| -rw-r--r-- | tests/ui/ptr_as_ptr.stderr | 8 |
4 files changed, 20 insertions, 2 deletions
diff --git a/clippy_lints/src/casts/ptr_as_ptr.rs b/clippy_lints/src/casts/ptr_as_ptr.rs index 89075409098..30e4adefd1f 100644 --- a/clippy_lints/src/casts/ptr_as_ptr.rs +++ b/clippy_lints/src/casts/ptr_as_ptr.rs @@ -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", diff --git a/tests/ui/ptr_as_ptr.fixed b/tests/ui/ptr_as_ptr.fixed index 78e1ceb480a..4457878b6fa 100644 --- a/tests/ui/ptr_as_ptr.fixed +++ b/tests/ui/ptr_as_ptr.fixed @@ -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 +} diff --git a/tests/ui/ptr_as_ptr.rs b/tests/ui/ptr_as_ptr.rs index 70732cf0a6c..9124fc912d2 100644 --- a/tests/ui/ptr_as_ptr.rs +++ b/tests/ui/ptr_as_ptr.rs @@ -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 +} diff --git a/tests/ui/ptr_as_ptr.stderr b/tests/ui/ptr_as_ptr.stderr index c0a2a4b6d20..af21c1e35f5 100644 --- a/tests/ui/ptr_as_ptr.stderr +++ b/tests/ui/ptr_as_ptr.stderr @@ -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 |
