Skip to content

Commit b31831e

Browse files
committed
Remove many as-casts of pointers
1 parent de87ddf commit b31831e

File tree

17 files changed

+58
-44
lines changed

17 files changed

+58
-44
lines changed

crates/line-tables-only/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mod tests {
1313

1414
extern "C" fn store_backtrace(data: *mut c_void) {
1515
let bt = backtrace::Backtrace::new();
16-
unsafe { *(data as *mut Option<Backtrace>) = Some(bt) };
16+
unsafe { *data.cast::<Option<Backtrace>>() = Some(bt) };
1717
}
1818

1919
fn assert_contains(
@@ -50,7 +50,7 @@ mod tests {
5050
#[cfg_attr(windows, ignore)]
5151
fn backtrace_works_with_line_tables_only() {
5252
let mut backtrace: Option<Backtrace> = None;
53-
unsafe { foo(store_backtrace, addr_of_mut!(backtrace) as *mut c_void) };
53+
unsafe { foo(store_backtrace, addr_of_mut!(backtrace).cast::<c_void>()) };
5454
let backtrace = backtrace.expect("backtrace");
5555
assert_contains(&backtrace, "foo", "src/callback.c", 13);
5656
assert_contains(&backtrace, "bar", "src/callback.c", 9);

src/backtrace/dbghelp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
141141

142142
let frame = super::Frame {
143143
inner: Frame {
144-
base_address: fn_entry as *mut c_void,
144+
base_address: fn_entry.cast::<c_void>(),
145145
ip: context.ip() as *mut c_void,
146146
sp: context.sp() as *mut c_void,
147147
#[cfg(not(target_env = "gnu"))]
@@ -162,7 +162,7 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
162162
context.ip(),
163163
fn_entry,
164164
&mut context.0,
165-
ptr::addr_of_mut!(handler_data) as *mut PVOID,
165+
ptr::addr_of_mut!(handler_data).cast::<PVOID>(),
166166
&mut establisher_frame,
167167
ptr::null_mut(),
168168
);

src/backtrace/libunwind.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ impl Clone for Frame {
102102

103103
#[inline(always)]
104104
pub unsafe fn trace(mut cb: &mut dyn FnMut(&super::Frame) -> bool) {
105-
uw::_Unwind_Backtrace(trace_fn, addr_of_mut!(cb) as *mut _);
105+
uw::_Unwind_Backtrace(trace_fn, addr_of_mut!(cb).cast());
106106

107107
extern "C" fn trace_fn(
108108
ctx: *mut uw::_Unwind_Context,
109109
arg: *mut c_void,
110110
) -> uw::_Unwind_Reason_Code {
111-
let cb = unsafe { &mut *(arg as *mut &mut dyn FnMut(&super::Frame) -> bool) };
111+
let cb = unsafe { &mut *arg.cast::<&mut dyn FnMut(&super::Frame) -> bool>() };
112112
let cx = super::Frame {
113113
inner: Frame::Raw(ctx),
114114
};
@@ -251,7 +251,7 @@ mod uw {
251251
_Unwind_VRS_RegClass::_UVRSC_CORE,
252252
15,
253253
_Unwind_VRS_DataRepresentation::_UVRSD_UINT32,
254-
ptr as *mut c_void,
254+
ptr.cast::<c_void>(),
255255
);
256256
(val & !1) as libc::uintptr_t
257257
}
@@ -267,7 +267,7 @@ mod uw {
267267
_Unwind_VRS_RegClass::_UVRSC_CORE,
268268
SP,
269269
_Unwind_VRS_DataRepresentation::_UVRSD_UINT32,
270-
ptr as *mut c_void,
270+
ptr.cast::<c_void>(),
271271
);
272272
val as libc::uintptr_t
273273
}

src/backtrace/miri.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,20 @@ pub fn trace<F: FnMut(&super::Frame) -> bool>(cb: F) {
6565
pub fn resolve_addr(ptr: *mut c_void) -> Frame {
6666
// SAFETY: Miri will stop execution with an error if this pointer
6767
// is invalid.
68-
let frame = unsafe { miri_resolve_frame(ptr as *mut (), 1) };
68+
let frame = unsafe { miri_resolve_frame(ptr.cast::<()>(), 1) };
6969

7070
let mut name = Vec::with_capacity(frame.name_len);
7171
let mut filename = Vec::with_capacity(frame.filename_len);
7272

7373
// SAFETY: name and filename have been allocated with the amount
7474
// of memory miri has asked for, and miri guarantees it will initialize it
7575
unsafe {
76-
miri_resolve_frame_names(ptr as *mut (), 0, name.as_mut_ptr(), filename.as_mut_ptr());
76+
miri_resolve_frame_names(
77+
ptr.cast::<()>(),
78+
0,
79+
name.as_mut_ptr(),
80+
filename.as_mut_ptr(),
81+
);
7782

7883
name.set_len(frame.name_len);
7984
filename.set_len(frame.filename_len);
@@ -101,7 +106,7 @@ unsafe fn trace_unsynchronized<F: FnMut(&super::Frame) -> bool>(mut cb: F) {
101106
frames.set_len(len);
102107

103108
for ptr in frames.iter() {
104-
let frame = resolve_addr(*ptr as *mut c_void);
109+
let frame = resolve_addr((*ptr).cast::<c_void>());
105110
if !cb(&super::Frame { inner: frame }) {
106111
return;
107112
}

src/backtrace/noop.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! appropriate.
33
44
use core::ffi::c_void;
5+
use core::ptr::null_mut;
56

67
#[inline(always)]
78
pub fn trace(_cb: &mut dyn FnMut(&super::Frame) -> bool) {}
@@ -11,15 +12,15 @@ pub struct Frame;
1112

1213
impl Frame {
1314
pub fn ip(&self) -> *mut c_void {
14-
0 as *mut _
15+
null_mut()
1516
}
1617

1718
pub fn sp(&self) -> *mut c_void {
18-
0 as *mut _
19+
null_mut()
1920
}
2021

2122
pub fn symbol_address(&self) -> *mut c_void {
22-
0 as *mut _
23+
null_mut()
2324
}
2425

2526
pub fn module_base_address(&self) -> Option<*mut c_void> {

src/capture.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ impl BacktraceFrame {
272272
/// This function requires the `std` feature of the `backtrace` crate to be
273273
/// enabled, and the `std` feature is enabled by default.
274274
pub fn ip(&self) -> *mut c_void {
275-
self.frame.ip() as *mut c_void
275+
self.frame.ip()
276276
}
277277

278278
/// Same as `Frame::symbol_address`
@@ -282,7 +282,7 @@ impl BacktraceFrame {
282282
/// This function requires the `std` feature of the `backtrace` crate to be
283283
/// enabled, and the `std` feature is enabled by default.
284284
pub fn symbol_address(&self) -> *mut c_void {
285-
self.frame.symbol_address() as *mut c_void
285+
self.frame.symbol_address()
286286
}
287287

288288
/// Same as `Frame::module_base_address`
@@ -292,9 +292,7 @@ impl BacktraceFrame {
292292
/// This function requires the `std` feature of the `backtrace` crate to be
293293
/// enabled, and the `std` feature is enabled by default.
294294
pub fn module_base_address(&self) -> Option<*mut c_void> {
295-
self.frame
296-
.module_base_address()
297-
.map(|addr| addr as *mut c_void)
295+
self.frame.module_base_address()
298296
}
299297

300298
/// Returns the list of symbols that this frame corresponds to.

src/dbghelp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ macro_rules! dbghelp {
8888

8989
static mut DBGHELP: Dbghelp = Dbghelp {
9090
// Initially we haven't loaded the DLL
91-
dll: 0 as *mut _,
91+
dll: ptr::null_mut(),
9292
// Initially all functions are set to zero to say they need to be
9393
// dynamically loaded.
9494
$($name: 0,)*
@@ -108,7 +108,7 @@ macro_rules! dbghelp {
108108
}
109109
let lib = b"dbghelp.dll\0";
110110
unsafe {
111-
self.dll = LoadLibraryA(lib.as_ptr() as *const i8);
111+
self.dll = LoadLibraryA(lib.as_ptr().cast::<i8>());
112112
if self.dll.is_null() {
113113
Err(())
114114
} else {
@@ -135,7 +135,7 @@ macro_rules! dbghelp {
135135

136136
fn symbol(&self, symbol: &[u8]) -> Option<usize> {
137137
unsafe {
138-
match GetProcAddress(self.dll, symbol.as_ptr() as *const _) as usize {
138+
match GetProcAddress(self.dll, symbol.as_ptr().cast()) as usize {
139139
0 => None,
140140
n => Some(n),
141141
}

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,12 @@ impl Drop for Bomb {
159159
mod lock {
160160
use std::boxed::Box;
161161
use std::cell::Cell;
162+
use std::ptr;
162163
use std::sync::{Mutex, MutexGuard, Once};
163164

164165
pub struct LockGuard(Option<MutexGuard<'static, ()>>);
165166

166-
static mut LOCK: *mut Mutex<()> = 0 as *mut _;
167+
static mut LOCK: *mut Mutex<()> = ptr::null_mut();
167168
static INIT: Once = Once::new();
168169
thread_local!(static LOCK_HELD: Cell<bool> = Cell::new(false));
169170

src/print/fuchsia.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ fn for_each_dso(mut visitor: &mut DsoPrinter<'_, '_>) {
359359
// location.
360360
let name_len = unsafe { libc::strlen(info.name) };
361361
let name_slice: &[u8] =
362-
unsafe { core::slice::from_raw_parts(info.name as *const u8, name_len) };
362+
unsafe { core::slice::from_raw_parts(info.name.cast::<u8>(), name_len) };
363363
let name = match core::str::from_utf8(name_slice) {
364364
Ok(name) => name,
365365
Err(_) => {

src/symbolize/dbghelp.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use core::char;
2323
use core::ffi::c_void;
2424
use core::marker;
2525
use core::mem;
26+
use core::ptr;
2627
use core::slice;
2728

2829
// Store an OsString on std so we can provide the symbol name and filename.
@@ -44,7 +45,7 @@ impl Symbol<'_> {
4445
}
4546

4647
pub fn addr(&self) -> Option<*mut c_void> {
47-
Some(self.addr as *mut _)
48+
Some(self.addr)
4849
}
4950

5051
pub fn filename_raw(&self) -> Option<BytesOrWideString<'_>> {
@@ -184,8 +185,7 @@ unsafe fn do_resolve(
184185
) {
185186
const SIZE: usize = 2 * MAX_SYM_NAME + mem::size_of::<SYMBOL_INFOW>();
186187
let mut data = Aligned8([0u8; SIZE]);
187-
let data = &mut data.0;
188-
let info = &mut *(data.as_mut_ptr() as *mut SYMBOL_INFOW);
188+
let info = &mut *data.0.as_mut_ptr().cast::<SYMBOL_INFOW>();
189189
info.MaxNameLen = MAX_SYM_NAME as ULONG;
190190
// the struct size in C. the value is different to
191191
// `size_of::<SYMBOL_INFOW>() - MAX_SYM_NAME + 1` (== 81)
@@ -200,7 +200,7 @@ unsafe fn do_resolve(
200200
// give a buffer of (MaxNameLen - 1) characters and set NameLen to
201201
// the real value.
202202
let name_len = ::core::cmp::min(info.NameLen as usize, info.MaxNameLen as usize - 1);
203-
let name_ptr = info.Name.as_ptr() as *const u16;
203+
let name_ptr = info.Name.as_ptr().cast::<u16>();
204204
let name = slice::from_raw_parts(name_ptr, name_len);
205205

206206
// Reencode the utf-16 symbol to utf-8 so we can use `SymbolName::new` like
@@ -222,7 +222,7 @@ unsafe fn do_resolve(
222222
}
223223
}
224224
}
225-
let name = core::ptr::addr_of!(name_buffer[..name_len]);
225+
let name = ptr::addr_of!(name_buffer[..name_len]);
226226

227227
let mut line = mem::zeroed::<IMAGEHLP_LINEW64>();
228228
line.SizeOfStruct = mem::size_of::<IMAGEHLP_LINEW64>() as DWORD;
@@ -240,7 +240,7 @@ unsafe fn do_resolve(
240240

241241
let len = len as usize;
242242

243-
filename = Some(slice::from_raw_parts(base, len) as *const [u16]);
243+
filename = Some(ptr::from_ref(slice::from_raw_parts(base, len)));
244244
}
245245

246246
cb(&super::Symbol {

0 commit comments

Comments
 (0)