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 {

src/symbolize/gimli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ pub unsafe fn resolve(what: ResolveWhat<'_>, cb: &mut dyn FnMut(&super::Symbol))
430430
};
431431

432432
Cache::with_global(|cache| {
433-
let (lib, addr) = match cache.avma_to_svma(addr as *const u8) {
433+
let (lib, addr) = match cache.avma_to_svma(addr.cast_const().cast::<u8>()) {
434434
Some(pair) => pair,
435435
None => return,
436436
};

src/symbolize/gimli/libs_aix.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub(super) fn native_libraries() -> Vec<Library> {
2020
loop {
2121
if libc::loadquery(
2222
libc::L_GETINFO,
23-
buffer.as_mut_ptr() as *mut libc::c_char,
23+
buffer.as_mut_ptr().cast::<libc::c_char>(),
2424
(mem::size_of::<libc::ld_info>() * buffer.len()) as u32,
2525
) != -1
2626
{
@@ -66,8 +66,10 @@ pub(super) fn native_libraries() -> Vec<Library> {
6666
if (*current).ldinfo_next == 0 {
6767
break;
6868
}
69-
current = (current as *mut libc::c_char).offset((*current).ldinfo_next as isize)
70-
as *mut libc::ld_info;
69+
current = current
70+
.cast::<libc::c_char>()
71+
.offset((*current).ldinfo_next as isize)
72+
.cast::<libc::ld_info>();
7173
}
7274
}
7375
return ret;

src/symbolize/gimli/libs_dl_iterate_phdr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use core::slice;
1212
pub(super) fn native_libraries() -> Vec<Library> {
1313
let mut ret = Vec::new();
1414
unsafe {
15-
libc::dl_iterate_phdr(Some(callback), core::ptr::addr_of_mut!(ret) as *mut _);
15+
libc::dl_iterate_phdr(Some(callback), core::ptr::addr_of_mut!(ret).cast());
1616
}
1717
return ret;
1818
}
@@ -43,7 +43,7 @@ unsafe extern "C" fn callback(
4343
vec: *mut libc::c_void,
4444
) -> libc::c_int {
4545
let info = &*info;
46-
let libs = &mut *(vec as *mut Vec<Library>);
46+
let libs = &mut *vec.cast::<Vec<Library>>();
4747
let is_main_prog = info.dlpi_name.is_null() || *info.dlpi_name == 0;
4848
let name = if is_main_prog {
4949
// The man page for dl_iterate_phdr says that the first object visited by

src/symbolize/gimli/libs_illumos.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub(super) fn native_libraries() -> Vec<Library> {
4141
if dlinfo(
4242
RTLD_SELF,
4343
RTLD_DI_LINKMAP,
44-
core::ptr::addr_of_mut!(map) as *mut libc::c_void,
44+
core::ptr::addr_of_mut!(map).cast::<libc::c_void>(),
4545
) != 0
4646
{
4747
return libs;

src/symbolize/gimli/libs_macos.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ use super::{Library, LibrarySegment};
77
use core::convert::TryInto;
88
use core::mem;
99

10+
// FIXME: replace with ptr::from_ref once MSRV is high enough
11+
#[inline(always)]
12+
#[must_use]
13+
const fn ptr_from_ref<T: ?Sized>(r: &T) -> *const T {
14+
r
15+
}
16+
1017
pub(super) fn native_libraries() -> Vec<Library> {
1118
let mut ret = Vec::new();
1219
let images = unsafe { libc::_dyld_image_count() };
@@ -42,18 +49,18 @@ fn native_library(i: u32) -> Option<Library> {
4249
match (*header).magic {
4350
macho::MH_MAGIC => {
4451
let endian = NativeEndian;
45-
let header = &*(header as *const macho::MachHeader32<NativeEndian>);
52+
let header = &*header.cast::<macho::MachHeader32<NativeEndian>>();
4653
let data = core::slice::from_raw_parts(
47-
header as *const _ as *const u8,
54+
ptr_from_ref(header).cast::<u8>(),
4855
mem::size_of_val(header) + header.sizeofcmds.get(endian) as usize,
4956
);
5057
(header.load_commands(endian, data, 0).ok()?, endian)
5158
}
5259
macho::MH_MAGIC_64 => {
5360
let endian = NativeEndian;
54-
let header = &*(header as *const macho::MachHeader64<NativeEndian>);
61+
let header = &*header.cast::<macho::MachHeader64<NativeEndian>>();
5562
let data = core::slice::from_raw_parts(
56-
header as *const _ as *const u8,
63+
ptr_from_ref(header).cast::<u8>(),
5764
mem::size_of_val(header) + header.sizeofcmds.get(endian) as usize,
5865
);
5966
(header.load_commands(endian, data, 0).ok()?, endian)

src/symbolize/gimli/mmap_windows.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl Mmap {
1717
pub unsafe fn map(file: &File, len: usize) -> Option<Mmap> {
1818
let file = file.try_clone().ok()?;
1919
let mapping = CreateFileMappingA(
20-
file.as_raw_handle() as *mut _,
20+
file.as_raw_handle().cast(),
2121
ptr::null_mut(),
2222
PAGE_READONLY,
2323
0,
@@ -43,7 +43,7 @@ impl Deref for Mmap {
4343
type Target = [u8];
4444

4545
fn deref(&self) -> &[u8] {
46-
unsafe { slice::from_raw_parts(self.ptr as *const u8, self.len) }
46+
unsafe { slice::from_raw_parts(self.ptr.cast_const().cast::<u8>(), self.len) }
4747
}
4848
}
4949

src/symbolize/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ impl Symbol {
210210

211211
/// Returns the starting address of this function.
212212
pub fn addr(&self) -> Option<*mut c_void> {
213-
self.inner.addr().map(|p| p as *mut _)
213+
self.inner.addr()
214214
}
215215

216216
/// Returns the raw filename as a slice. This is mainly useful for `no_std`

0 commit comments

Comments
 (0)