Skip to content

Commit 443860d

Browse files
committed
ptr_as_ptr: use correct span context for pointer::cast sugg
1 parent 9a2076e commit 443860d

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

clippy_lints/src/casts/ptr_as_ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: Msrv)
7878
let method = snippet_with_applicability(cx, qpath_span_without_turbofish(method), "..", &mut app);
7979
("try call directly", format!("{method}{turbofish}()"))
8080
} else {
81-
let cast_expr_sugg = Sugg::hir_with_applicability(cx, cast_expr, "_", &mut app);
81+
let cast_expr_sugg = Sugg::hir_with_context(cx, cast_expr, expr.span.ctxt(), "_", &mut app);
8282

8383
(
8484
"try `pointer::cast`, a safer alternative",

tests/ui/ptr_as_ptr.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,9 @@ fn issue15283() {
229229
//~^ ptr_as_ptr
230230
}
231231
}
232+
233+
fn issue15232() {
234+
let mut b = Box::new(0i32);
235+
let _ptr = std::ptr::addr_of_mut!(*b).cast::<()>();
236+
//~^ ptr_as_ptr
237+
}

tests/ui/ptr_as_ptr.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,9 @@ fn issue15283() {
229229
//~^ ptr_as_ptr
230230
}
231231
}
232+
233+
fn issue15232() {
234+
let mut b = Box::new(0i32);
235+
let _ptr = std::ptr::addr_of_mut!(*b) as *mut ();
236+
//~^ ptr_as_ptr
237+
}

tests/ui/ptr_as_ptr.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,5 +199,11 @@ error: `as` casting between raw pointers without changing their constness
199199
LL | let _: fn() = std::mem::transmute(std::ptr::null::<()>() as *const u8);
200200
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `std::ptr::null::<u8>()`
201201

202-
error: aborting due to 33 previous errors
202+
error: `as` casting between raw pointers without changing their constness
203+
--> tests/ui/ptr_as_ptr.rs:235:16
204+
|
205+
LL | let _ptr = std::ptr::addr_of_mut!(*b) as *mut ();
206+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `std::ptr::addr_of_mut!(*b).cast::<()>()`
207+
208+
error: aborting due to 34 previous errors
203209

0 commit comments

Comments
 (0)