Skip to content

Commit ecaf2db

Browse files
committed
Enable the ptr-as-ptr and as-ptr-cast-mut Clippy lints
Replace `as *const _` / `as *mut _` pointer casts with the more type-safe `.cast()`, `.cast::<T>()`, and `.cast_mut()` methods throughout the codebase. Where applicable, also use `&raw const` / `&raw mut` instead of taking a reference and casting it. Enable the `ptr-as-ptr` and `as-ptr-cast-mut` Clippy lints as warnings to prevent regressions. Signed-off-by: Daniel Müller <deso@posteo.net>
1 parent 1b33500 commit ecaf2db

15 files changed

Lines changed: 61 additions & 60 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ let-unit-value = "allow"
4747
module-inception = "allow"
4848
type-complexity = "allow"
4949
absolute-paths = "warn"
50+
as-ptr-cast-mut = "warn"
5051
clone-on-ref-ptr = "warn"
5152
dbg-macro = "warn"
5253
doc-markdown = "warn"
5354
join-absolute-paths = "warn"
5455
large-enum-variant = "warn"
56+
ptr-as-ptr = "warn"
5557
redundant-closure-for-method-calls = "warn"
5658
unchecked-time-subtraction = "warn"
5759
uninlined-format-args = "warn"

examples/tcp_option/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use libc::SOCK_NONBLOCK;
1515
use libc::SOCK_RAW;
1616

1717
use libc::c_int;
18-
use libc::c_void;
1918
use libc::socklen_t;
2019
use std::mem::size_of_val;
2120

@@ -120,7 +119,7 @@ fn main() -> Result<()> {
120119
target_socket_fd,
121120
SOL_SOCKET,
122121
SO_ATTACH_BPF,
123-
&prog_fd as *const _ as *const c_void,
122+
(&raw const prog_fd).cast(),
124123
size_of_val(&prog_fd) as socklen_t,
125124
)
126125
} {

libbpf-cargo/src/gen/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ fn open_bpf_object(name: &str, data: &[u8]) -> Result<Object> {
755755
};
756756
let object = unsafe {
757757
libbpf_sys::bpf_object__open_mem(
758-
data.as_ptr() as *const c_void,
758+
data.as_ptr().cast::<c_void>(),
759759
data.len() as c_ulong,
760760
&obj_opts,
761761
)

libbpf-rs/src/btf/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ impl<'btf> Btf<'btf> {
261261

262262
let ptr = unsafe {
263263
libbpf_sys::bpf_object__open_mem(
264-
object_file.as_ptr() as *const c_void,
264+
object_file.as_ptr().cast::<c_void>(),
265265
object_file.len() as c_ulong,
266266
&obj_opts,
267267
)

libbpf-rs/src/iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl Iter {
3636
impl io::Read for Iter {
3737
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
3838
let bytes_read =
39-
unsafe { libc::read(self.fd.as_raw_fd(), buf.as_mut_ptr() as *mut _, buf.len()) };
39+
unsafe { libc::read(self.fd.as_raw_fd(), buf.as_mut_ptr().cast(), buf.len()) };
4040
if bytes_read < 0 {
4141
return Err(io::Error::last_os_error());
4242
}

libbpf-rs/src/linker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl Linker {
5959
let err = unsafe {
6060
libbpf_sys::bpf_linker__add_buf(
6161
self.linker.as_ptr(),
62-
buf.as_ptr() as *mut _,
62+
buf.as_ptr().cast_mut().cast(),
6363
buf.len() as _,
6464
opts,
6565
)

libbpf-rs/src/map.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<'obj> OpenMap<'obj> {
9090
fn initial_value_raw(&self) -> (*mut u8, usize) {
9191
let mut size = 0u64;
9292
let ptr = unsafe {
93-
libbpf_sys::bpf_map__initial_value(self.ptr.as_ptr(), &mut size as *mut _ as _)
93+
libbpf_sys::bpf_map__initial_value(self.ptr.as_ptr(), (&raw mut size).cast())
9494
};
9595
(ptr.cast(), size as _)
9696
}
@@ -149,7 +149,7 @@ impl<'obj> OpenMapMut<'obj> {
149149
let ret = unsafe {
150150
libbpf_sys::bpf_map__set_initial_value(
151151
self.ptr.as_ptr(),
152-
data.as_ptr() as *const c_void,
152+
data.as_ptr().cast::<c_void>(),
153153
data.len() as libbpf_sys::size_t,
154154
)
155155
};
@@ -317,7 +317,7 @@ where
317317
return ptr::null();
318318
}
319319

320-
key.as_ptr() as *const c_void
320+
key.as_ptr().cast::<c_void>()
321321
}
322322

323323
/// Internal function to perform a map lookup and write the value into raw pointer.
@@ -414,7 +414,7 @@ where
414414
libbpf_sys::bpf_map_update_elem(
415415
map.as_fd().as_raw_fd(),
416416
map_key(map, key),
417-
value.as_ptr() as *const c_void,
417+
value.as_ptr().cast::<c_void>(),
418418
flags.bits(),
419419
)
420420
};
@@ -605,7 +605,7 @@ pub trait MapCore: Debug + AsFd + private::Sealed {
605605
libbpf_sys::bpf_map_lookup_elem(
606606
self.as_fd().as_raw_fd(),
607607
ptr::null(),
608-
value.to_vec().as_mut_ptr() as *mut c_void,
608+
value.to_vec().as_mut_ptr().cast::<c_void>(),
609609
)
610610
};
611611

@@ -661,7 +661,7 @@ pub trait MapCore: Debug + AsFd + private::Sealed {
661661
};
662662

663663
let ret = unsafe {
664-
libbpf_sys::bpf_map_delete_elem(self.as_fd().as_raw_fd(), key.as_ptr() as *const c_void)
664+
libbpf_sys::bpf_map_delete_elem(self.as_fd().as_raw_fd(), key.as_ptr().cast::<c_void>())
665665
};
666666
util::parse_ret(ret)
667667
}
@@ -698,7 +698,7 @@ pub trait MapCore: Debug + AsFd + private::Sealed {
698698
let ret = unsafe {
699699
libbpf_sys::bpf_map_delete_batch(
700700
self.as_fd().as_raw_fd(),
701-
keys.as_ptr() as *const c_void,
701+
keys.as_ptr().cast::<c_void>(),
702702
&mut count,
703703
&opts as *const libbpf_sys::bpf_map_batch_opts,
704704
)
@@ -727,7 +727,7 @@ pub trait MapCore: Debug + AsFd + private::Sealed {
727727
libbpf_sys::bpf_map_lookup_and_delete_elem(
728728
self.as_fd().as_raw_fd(),
729729
map_key(self, key),
730-
out.as_mut_ptr() as *mut c_void,
730+
out.as_mut_ptr().cast::<c_void>(),
731731
)
732732
};
733733

@@ -814,8 +814,8 @@ pub trait MapCore: Debug + AsFd + private::Sealed {
814814
let ret = unsafe {
815815
libbpf_sys::bpf_map_update_batch(
816816
self.as_fd().as_raw_fd(),
817-
keys.as_ptr() as *const c_void,
818-
values.as_ptr() as *const c_void,
817+
keys.as_ptr().cast::<c_void>(),
818+
values.as_ptr().cast::<c_void>(),
819819
&mut count,
820820
&opts as *const libbpf_sys::bpf_map_batch_opts,
821821
)
@@ -1571,8 +1571,8 @@ impl Iterator for MapKeyIter<'_> {
15711571
let ret = unsafe {
15721572
libbpf_sys::bpf_map_get_next_key(
15731573
self.map_fd.as_raw_fd(),
1574-
prev as _,
1575-
self.next.as_mut_ptr() as _,
1574+
prev.cast(),
1575+
self.next.as_mut_ptr().cast(),
15761576
)
15771577
};
15781578
if ret != 0 {
@@ -1717,7 +1717,7 @@ impl MapInfo {
17171717
let () = util::parse_ret(unsafe {
17181718
bpf_obj_get_info_by_fd(
17191719
fd.as_raw_fd(),
1720-
&mut map_info as *mut bpf_map_info as *mut c_void,
1720+
(&mut map_info as *mut bpf_map_info).cast::<c_void>(),
17211721
&mut size as *mut u32,
17221722
)
17231723
})?;

libbpf-rs/src/object.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl ObjectBuilder {
227227
let opts_ptr = self.as_libbpf_object().as_ptr();
228228
let ptr = unsafe {
229229
libbpf_sys::bpf_object__open_mem(
230-
mem.as_ptr() as *const c_void,
230+
mem.as_ptr().cast::<c_void>(),
231231
mem.len() as libbpf_sys::size_t,
232232
opts_ptr,
233233
)
@@ -289,7 +289,7 @@ impl OpenObject {
289289
// SAFETY: We ensured `ptr` is valid during construction.
290290
let name_ptr = unsafe { libbpf_sys::bpf_object__name(self.ptr.as_ptr()) };
291291
// SAFETY: `libbpf_get_error` is always safe to call.
292-
let err = unsafe { libbpf_sys::libbpf_get_error(name_ptr as *const _) };
292+
let err = unsafe { libbpf_sys::libbpf_get_error(name_ptr.cast()) };
293293
if err != 0 {
294294
return None
295295
}
@@ -389,7 +389,7 @@ impl Object {
389389
// SAFETY: We ensured `ptr` is valid during construction.
390390
let name_ptr = unsafe { libbpf_sys::bpf_object__name(self.ptr.as_ptr()) };
391391
// SAFETY: `libbpf_get_error` is always safe to call.
392-
let err = unsafe { libbpf_sys::libbpf_get_error(name_ptr as *const _) };
392+
let err = unsafe { libbpf_sys::libbpf_get_error(name_ptr.cast()) };
393393
if err != 0 {
394394
return None
395395
}

libbpf-rs/src/perf_buffer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ where
142142
self.pages as libbpf_sys::size_t,
143143
c_sample_cb,
144144
c_lost_cb,
145-
callback_struct_ptr as *mut _,
145+
callback_struct_ptr.cast(),
146146
ptr::null(),
147147
)
148148
};
@@ -155,7 +155,7 @@ where
155155
}
156156

157157
unsafe extern "C" fn call_sample_cb(ctx: *mut c_void, cpu: i32, data: *mut c_void, size: u32) {
158-
let callback_struct = ctx as *mut CbStruct<'_>;
158+
let callback_struct = ctx.cast::<CbStruct<'_>>();
159159

160160
if let Some(cb) = unsafe { &mut (*callback_struct).sample_cb } {
161161
let slice = unsafe { slice::from_raw_parts(data as *const u8, size as usize) };
@@ -164,7 +164,7 @@ where
164164
}
165165

166166
unsafe extern "C" fn call_lost_cb(ctx: *mut c_void, cpu: i32, count: u64) {
167-
let callback_struct = ctx as *mut CbStruct<'_>;
167+
let callback_struct = ctx.cast::<CbStruct<'_>>();
168168

169169
if let Some(cb) = unsafe { &mut (*callback_struct).lost_cb } {
170170
cb(cpu, count);

libbpf-rs/src/program.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ impl<'obj> Program<'obj> {
815815
let ret = unsafe {
816816
libbpf_sys::bpf_obj_get_info_by_fd(
817817
fd.as_raw_fd(),
818-
prog_info_ptr as *mut c_void,
818+
prog_info_ptr.cast::<c_void>(),
819819
&mut len,
820820
)
821821
};
@@ -1244,7 +1244,7 @@ impl<'obj> ProgramMut<'obj> {
12441244

12451245
let opts = libbpf_sys::bpf_kprobe_multi_opts {
12461246
sz: size_of::<libbpf_sys::bpf_kprobe_multi_opts>() as _,
1247-
syms: syms.as_mut_ptr() as _,
1247+
syms: syms.as_mut_ptr().cast(),
12481248
cnt: cnt as libbpf_sys::size_t,
12491249
retprobe,
12501250
// bpf_kprobe_multi_opts might have padding fields on some platform
@@ -1275,9 +1275,9 @@ impl<'obj> ProgramMut<'obj> {
12751275

12761276
let opts = libbpf_sys::bpf_kprobe_multi_opts {
12771277
sz: size_of::<libbpf_sys::bpf_kprobe_multi_opts>() as _,
1278-
syms: syms.as_mut_ptr() as _,
1278+
syms: syms.as_mut_ptr().cast(),
12791279
cookies: if !cookies.is_empty() {
1280-
cookies.as_mut_ptr() as _
1280+
cookies.as_mut_ptr().cast()
12811281
} else {
12821282
ptr::null()
12831283
},
@@ -1327,7 +1327,7 @@ impl<'obj> ProgramMut<'obj> {
13271327
self.ptr.as_ptr(),
13281328
tp_category_ptr,
13291329
tp_name_ptr,
1330-
opts as *const _,
1330+
opts.cast(),
13311331
)
13321332
};
13331333

0 commit comments

Comments
 (0)