Skip to content

Commit 6a1836f

Browse files
committed
Enable clippy::absolute_paths lint
In the past we had many submissions that, for one reason or another, were using absolute paths to functions/constants in a very inconsistent manner. It is not particularly great use of anybody's time pointing these out and it's certainly a job that computers can do better. Clippy folks seem to concur that this can be a useful lint [0] and we got blessed with clippy::absolute_paths. Enable it throughout libbpf-rs and libbpf-cargo and fix all violations. [0] rust-lang/rust-clippy#10568 Signed-off-by: Daniel Müller <[email protected]>
1 parent be286ef commit 6a1836f

File tree

16 files changed

+56
-45
lines changed

16 files changed

+56
-45
lines changed

libbpf-cargo/src/build.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::collections::HashSet;
2+
use std::env::consts::ARCH;
23
use std::ffi::OsStr;
34
use std::ffi::OsString;
45
use std::fs;
@@ -170,10 +171,10 @@ fn compile_one(debug: bool, source: &Path, out: &Path, clang: &Path, options: &s
170171
}
171172

172173
if !options.contains("-D__TARGET_ARCH_") {
173-
let arch = match std::env::consts::ARCH {
174+
let arch = match ARCH {
174175
"x86_64" => "x86",
175176
"aarch64" => "arm64",
176-
_ => std::env::consts::ARCH,
177+
_ => ARCH,
177178
};
178179
cmd.arg(format!("-D__TARGET_ARCH_{arch}"));
179180
}

libbpf-cargo/src/gen/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::fs::File;
1010
use std::io::stdout;
1111
use std::io::ErrorKind;
1212
use std::io::Write;
13+
use std::mem::size_of;
1314
use std::os::raw::c_ulong;
1415
use std::path::Path;
1516
use std::path::PathBuf;
@@ -606,7 +607,7 @@ fn gen_skel_link_getter(skel: &mut String, object: &mut BpfObj, obj_name: &str)
606607
fn open_bpf_object(name: &str, data: &[u8]) -> Result<BpfObj> {
607608
let cname = CString::new(name)?;
608609
let obj_opts = libbpf_sys::bpf_object_open_opts {
609-
sz: std::mem::size_of::<libbpf_sys::bpf_object_open_opts>() as libbpf_sys::size_t,
610+
sz: size_of::<libbpf_sys::bpf_object_open_opts>() as libbpf_sys::size_t,
610611
object_name: cname.as_ptr(),
611612
..Default::default()
612613
};

libbpf-cargo/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#![warn(
6060
elided_lifetimes_in_paths,
6161
single_use_lifetimes,
62+
clippy::absolute_paths,
6263
clippy::wildcard_imports
6364
)]
6465
#![deny(unsafe_op_in_unsafe_fn)]

libbpf-cargo/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(clippy::let_unit_value)]
1+
#![allow(clippy::absolute_paths, clippy::let_unit_value)]
22

33
use std::path::PathBuf;
44

libbpf-rs/src/btf/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ use std::marker::PhantomData;
2424
use std::mem::size_of;
2525
use std::num::NonZeroUsize;
2626
use std::ops::Deref;
27+
use std::os::raw::c_ulong;
2728
use std::os::raw::c_void;
2829
use std::os::unix::prelude::AsRawFd;
2930
use std::os::unix::prelude::FromRawFd;
3031
use std::os::unix::prelude::OsStrExt;
3132
use std::os::unix::prelude::OwnedFd;
3233
use std::path::Path;
34+
use std::ptr;
3335
use std::ptr::NonNull;
3436

3537
use num_enum::IntoPrimitive;
@@ -141,7 +143,7 @@ impl Btf<'static> {
141143
Error::with_invalid_data(format!("invalid path {path:?}, has null bytes"))
142144
})?;
143145
let ptr = create_bpf_entity_checked(|| unsafe {
144-
libbpf_sys::btf__parse(path.as_ptr(), std::ptr::null_mut())
146+
libbpf_sys::btf__parse(path.as_ptr(), ptr::null_mut())
145147
})?;
146148
Ok(Btf {
147149
ptr,
@@ -219,15 +221,15 @@ impl<'btf> Btf<'btf> {
219221
.unwrap();
220222

221223
let obj_opts = libbpf_sys::bpf_object_open_opts {
222-
sz: std::mem::size_of::<libbpf_sys::bpf_object_open_opts>() as libbpf_sys::size_t,
224+
sz: size_of::<libbpf_sys::bpf_object_open_opts>() as libbpf_sys::size_t,
223225
object_name: cname.as_ptr(),
224226
..Default::default()
225227
};
226228

227229
let mut bpf_obj = create_bpf_entity_checked(|| unsafe {
228230
libbpf_sys::bpf_object__open_mem(
229231
object_file.as_ptr() as *const c_void,
230-
object_file.len() as std::os::raw::c_ulong,
232+
object_file.len() as c_ulong,
231233
&obj_opts,
232234
)
233235
})?;

libbpf-rs/src/btf/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ pub enum IntEncoding {
339339
impl<'btf> TryFrom<BtfType<'btf>> for Int<'btf> {
340340
type Error = BtfType<'btf>;
341341

342-
fn try_from(t: BtfType<'btf>) -> std::result::Result<Self, Self::Error> {
342+
fn try_from(t: BtfType<'btf>) -> Result<Self, Self::Error> {
343343
if t.kind() == BtfKind::Int {
344344
let int = {
345345
let base_ptr = t.ty as *const libbpf_sys::btf_type;

libbpf-rs/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
missing_debug_implementations,
7373
missing_docs,
7474
single_use_lifetimes,
75+
clippy::absolute_paths,
7576
clippy::wildcard_imports,
7677
rustdoc::broken_intra_doc_links
7778
)]

libbpf-rs/src/map.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use std::os::unix::io::OwnedFd;
1616
use std::os::unix::io::RawFd;
1717
use std::path::Path;
1818
use std::ptr;
19-
use std::ptr::null;
2019
use std::ptr::NonNull;
2120
use std::slice::from_raw_parts;
2221

@@ -83,7 +82,7 @@ impl OpenMap {
8382
let ret = unsafe {
8483
libbpf_sys::bpf_map__set_initial_value(
8584
self.ptr.as_ptr(),
86-
data.as_ptr() as *const std::ffi::c_void,
85+
data.as_ptr() as *const c_void,
8786
data.len() as libbpf_sys::size_t,
8887
)
8988
};
@@ -393,7 +392,7 @@ impl MapHandle {
393392

394393
let map_name_ptr = {
395394
if map_name_str.as_bytes().is_empty() {
396-
null()
395+
ptr::null()
397396
} else {
398397
map_name_str.as_ptr()
399398
}
@@ -934,7 +933,7 @@ impl MapType {
934933
/// Make sure the process has required set of CAP_* permissions (or runs as
935934
/// root) when performing feature checking.
936935
pub fn is_supported(&self) -> Result<bool> {
937-
let ret = unsafe { libbpf_sys::libbpf_probe_bpf_map_type(*self as u32, std::ptr::null()) };
936+
let ret = unsafe { libbpf_sys::libbpf_probe_bpf_map_type(*self as u32, ptr::null()) };
938937
match ret {
939938
0 => Ok(false),
940939
1 => Ok(true),

libbpf-rs/src/object.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl OpenObject {
171171
};
172172

173173
// Populate obj.maps
174-
let mut map: *mut libbpf_sys::bpf_map = std::ptr::null_mut();
174+
let mut map: *mut libbpf_sys::bpf_map = ptr::null_mut();
175175
loop {
176176
// Get the pointer to the next BPF map
177177
let map_ptr = {
@@ -190,7 +190,7 @@ impl OpenObject {
190190
}
191191

192192
// Populate obj.progs
193-
let mut prog: *mut libbpf_sys::bpf_program = std::ptr::null_mut();
193+
let mut prog: *mut libbpf_sys::bpf_program = ptr::null_mut();
194194
loop {
195195
// Get the pointer to the next BPF program
196196
let prog_ptr = {
@@ -233,12 +233,12 @@ impl OpenObject {
233233
// using destructuring we make sure we'll get a compiler error if anything in
234234
// Self changes, which will alert us to change this function as well
235235
let Self { ptr, maps, progs } = &mut self;
236-
std::mem::take(maps);
237-
std::mem::take(progs);
236+
mem::take(maps);
237+
mem::take(progs);
238238
*ptr
239239
};
240240
// avoid double free of self.ptr
241-
std::mem::forget(self);
241+
mem::forget(self);
242242
ptr
243243
}
244244

@@ -354,7 +354,7 @@ impl Object {
354354
};
355355

356356
// Populate obj.maps
357-
let mut map: *mut libbpf_sys::bpf_map = std::ptr::null_mut();
357+
let mut map: *mut libbpf_sys::bpf_map = ptr::null_mut();
358358
loop {
359359
// Get the pointer to the next BPF map
360360
let map_ptr = {
@@ -373,7 +373,7 @@ impl Object {
373373
}
374374

375375
// Populate obj.progs
376-
let mut prog: *mut libbpf_sys::bpf_program = std::ptr::null_mut();
376+
let mut prog: *mut libbpf_sys::bpf_program = ptr::null_mut();
377377
loop {
378378
// Get the pointer to the next BPF program
379379
let prog_ptr = {

libbpf-rs/src/perf_buffer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::fmt::Formatter;
55
use std::fmt::Result as FmtResult;
66
use std::os::unix::io::AsFd;
77
use std::os::unix::prelude::AsRawFd;
8+
use std::ptr;
89
use std::ptr::NonNull;
910
use std::slice;
1011
use std::time::Duration;
@@ -133,7 +134,7 @@ impl<'a, 'b> PerfBufferBuilder<'a, 'b> {
133134
c_sample_cb,
134135
c_lost_cb,
135136
callback_struct_ptr as *mut _,
136-
std::ptr::null(),
137+
ptr::null(),
137138
)
138139
})
139140
.map(|ptr| PerfBuffer {

libbpf-rs/src/print.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::libbpf_sys;
22
use lazy_static::lazy_static;
33
use std::io;
44
use std::io::Write;
5+
use std::mem;
56
use std::os::raw::c_char;
67
use std::sync::Mutex;
78

@@ -117,7 +118,7 @@ pub fn set_print(
117118
mut callback: Option<(PrintLevel, PrintCallback)>,
118119
) -> Option<(PrintLevel, PrintCallback)> {
119120
let real_cb: libbpf_sys::libbpf_print_fn_t = callback.as_ref().and(Some(outer_print_cb));
120-
std::mem::swap(&mut callback, &mut *PRINT_CB.lock().unwrap());
121+
mem::swap(&mut callback, &mut *PRINT_CB.lock().unwrap());
121122
unsafe { libbpf_sys::libbpf_set_print(real_cb) };
122123
callback
123124
}

libbpf-rs/src/program.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use core::ffi::c_void;
22
use std::convert::TryFrom;
33
use std::ffi::CStr;
4-
use std::mem;
4+
use std::mem::size_of;
55
use std::os::unix::io::AsFd;
66
use std::os::unix::io::AsRawFd;
77
use std::os::unix::io::BorrowedFd;
@@ -10,6 +10,7 @@ use std::os::unix::io::OwnedFd;
1010
use std::path::Path;
1111
use std::ptr;
1212
use std::ptr::NonNull;
13+
use std::slice;
1314

1415
use libbpf_sys::bpf_func_id;
1516
use num_enum::TryFromPrimitive;
@@ -60,7 +61,7 @@ impl From<UsdtOpts> for libbpf_sys::bpf_usdt_opts {
6061
_non_exhaustive,
6162
} = opts;
6263
libbpf_sys::bpf_usdt_opts {
63-
sz: mem::size_of::<Self>() as _,
64+
sz: size_of::<Self>() as _,
6465
usdt_cookie: cookie,
6566
}
6667
}
@@ -83,7 +84,7 @@ impl From<TracepointOpts> for libbpf_sys::bpf_tracepoint_opts {
8384
} = opts;
8485

8586
libbpf_sys::bpf_tracepoint_opts {
86-
sz: mem::size_of::<Self>() as _,
87+
sz: size_of::<Self>() as _,
8788
bpf_cookie: cookie,
8889
}
8990
}
@@ -226,7 +227,7 @@ impl OpenProgram {
226227
pub fn insns(&self) -> &[libbpf_sys::bpf_insn] {
227228
let count = self.insn_cnt();
228229
let ptr = unsafe { libbpf_sys::bpf_program__insns(self.ptr.as_ptr()) };
229-
unsafe { std::slice::from_raw_parts(ptr, count) }
230+
unsafe { slice::from_raw_parts(ptr, count) }
230231
}
231232
}
232233

@@ -288,7 +289,7 @@ impl ProgramType {
288289
/// Make sure the process has required set of CAP_* permissions (or runs as
289290
/// root) when performing feature checking.
290291
pub fn is_supported(&self) -> Result<bool> {
291-
let ret = unsafe { libbpf_sys::libbpf_probe_bpf_prog_type(*self as u32, std::ptr::null()) };
292+
let ret = unsafe { libbpf_sys::libbpf_probe_bpf_prog_type(*self as u32, ptr::null()) };
292293
match ret {
293294
0 => Ok(false),
294295
1 => Ok(true),
@@ -302,9 +303,8 @@ impl ProgramType {
302303
/// Make sure the process has required set of CAP_* permissions (or runs as
303304
/// root) when performing feature checking.
304305
pub fn is_helper_supported(&self, helper_id: bpf_func_id) -> Result<bool> {
305-
let ret = unsafe {
306-
libbpf_sys::libbpf_probe_bpf_helper(*self as u32, helper_id, std::ptr::null())
307-
};
306+
let ret =
307+
unsafe { libbpf_sys::libbpf_probe_bpf_helper(*self as u32, helper_id, ptr::null()) };
308308
match ret {
309309
0 => Ok(false),
310310
1 => Ok(true),
@@ -438,7 +438,7 @@ impl Program {
438438
pub fn get_id_by_fd(fd: BorrowedFd<'_>) -> Result<u32> {
439439
let mut prog_info = libbpf_sys::bpf_prog_info::default();
440440
let prog_info_ptr: *mut libbpf_sys::bpf_prog_info = &mut prog_info;
441-
let mut len = mem::size_of::<libbpf_sys::bpf_prog_info>() as u32;
441+
let mut len = size_of::<libbpf_sys::bpf_prog_info>() as u32;
442442
let ret = unsafe {
443443
libbpf_sys::bpf_obj_get_info_by_fd(
444444
fd.as_raw_fd(),
@@ -577,7 +577,7 @@ impl Program {
577577

578578
let func_name = util::str_to_cstring(&func_name)?;
579579
let opts = libbpf_sys::bpf_uprobe_opts {
580-
sz: mem::size_of::<libbpf_sys::bpf_uprobe_opts>() as _,
580+
sz: size_of::<libbpf_sys::bpf_uprobe_opts>() as _,
581581
ref_ctr_offset: ref_ctr_offset as libbpf_sys::size_t,
582582
bpf_cookie: cookie,
583583
retprobe,
@@ -829,8 +829,8 @@ impl Program {
829829
linkinfo.map.map_fd = map_fd.as_raw_fd() as _;
830830
let attach_opt = libbpf_sys::bpf_iter_attach_opts {
831831
link_info: &mut linkinfo as *mut libbpf_sys::bpf_iter_link_info,
832-
link_info_len: std::mem::size_of::<libbpf_sys::bpf_iter_link_info>() as _,
833-
sz: std::mem::size_of::<libbpf_sys::bpf_iter_attach_opts>() as _,
832+
link_info_len: size_of::<libbpf_sys::bpf_iter_link_info>() as _,
833+
sz: size_of::<libbpf_sys::bpf_iter_attach_opts>() as _,
834834
..Default::default()
835835
};
836836

@@ -859,6 +859,6 @@ impl Program {
859859
pub fn insns(&self) -> &[libbpf_sys::bpf_insn] {
860860
let count = self.insn_cnt();
861861
let ptr = unsafe { libbpf_sys::bpf_program__insns(self.ptr.as_ptr()) };
862-
unsafe { std::slice::from_raw_parts(ptr, count) }
862+
unsafe { slice::from_raw_parts(ptr, count) }
863863
}
864864
}

libbpf-rs/src/query.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::ffi::c_void;
1515
use std::ffi::CString;
1616
use std::mem::size_of_val;
1717
use std::os::raw::c_char;
18+
use std::ptr;
1819
use std::time::Duration;
1920

2021
use nix::errno;
@@ -337,7 +338,7 @@ impl ProgramInfo {
337338
}
338339

339340
if opts.include_jited_line_info {
340-
jited_line_info.resize(item.nr_jited_line_info as usize, std::ptr::null());
341+
jited_line_info.resize(item.nr_jited_line_info as usize, ptr::null());
341342
item.jited_line_info = jited_line_info.as_mut_ptr() as *mut c_void as u64;
342343
} else {
343344
item.nr_jited_line_info = 0;
@@ -358,7 +359,7 @@ impl ProgramInfo {
358359
}
359360

360361
if opts.include_jited_ksyms {
361-
jited_ksyms.resize(item.nr_jited_ksyms as usize, std::ptr::null());
362+
jited_ksyms.resize(item.nr_jited_ksyms as usize, ptr::null());
362363
item.jited_ksyms = jited_ksyms.as_mut_ptr() as *mut c_void as u64;
363364
} else {
364365
item.nr_jited_ksyms = 0;

libbpf-rs/src/ringbuf.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::os::raw::c_ulong;
88
use std::os::unix::io::AsFd;
99
use std::os::unix::prelude::AsRawFd;
1010
use std::os::unix::prelude::BorrowedFd;
11+
use std::ptr::null_mut;
1112
use std::ptr::NonNull;
1213
use std::slice;
1314
use std::time::Duration;
@@ -98,7 +99,7 @@ impl<'slf, 'cb: 'slf> RingBufferBuilder<'slf, 'cb> {
9899
fd.as_raw_fd(),
99100
c_sample_cb,
100101
sample_cb_ptr as *mut _,
101-
std::ptr::null_mut(),
102+
null_mut(),
102103
)
103104
})?);
104105
}

libbpf-rs/src/tc.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::mem::size_of;
12
use std::os::unix::io::AsRawFd;
23
use std::os::unix::io::BorrowedFd;
34

@@ -64,8 +65,8 @@ impl TcHook {
6465
opts: libbpf_sys::bpf_tc_opts::default(),
6566
};
6667

67-
tc_hook.hook.sz = std::mem::size_of::<libbpf_sys::bpf_tc_hook>() as libbpf_sys::size_t;
68-
tc_hook.opts.sz = std::mem::size_of::<libbpf_sys::bpf_tc_opts>() as libbpf_sys::size_t;
68+
tc_hook.hook.sz = size_of::<libbpf_sys::bpf_tc_hook>() as libbpf_sys::size_t;
69+
tc_hook.opts.sz = size_of::<libbpf_sys::bpf_tc_opts>() as libbpf_sys::size_t;
6970
tc_hook.opts.prog_fd = fd.as_raw_fd();
7071

7172
tc_hook

0 commit comments

Comments
 (0)