refactor: casting-related clippy::pedantic fixes

Prefer using `ptr::addr_of!()` and `pointer::cast()` instead of raw `as`
where clippy complains and other type casting lints.

Signed-off-by: Thomas Mühlbacher <tmuehlbacher@posteo.net>
This commit is contained in:
Thomas Mühlbacher 2024-05-31 12:44:33 +02:00
parent 15e3c90584
commit 96a346254d
4 changed files with 17 additions and 17 deletions

View File

@ -28,7 +28,7 @@ fn handle_c_command(mut argv: Vec<String>, symlink_cmd: Option<&str>) -> i32 {
let argv: Vec<_> = argv.into_iter().map(|s| CString::new(s).unwrap()).collect();
let mut argv = argv
.into_iter()
.map(|s| Box::into_raw(s.into_boxed_c_str()) as *mut c_char)
.map(|s| Box::into_raw(s.into_boxed_c_str()).cast::<c_char>())
.collect::<Box<[*mut c_char]>>();
let argv = argv.as_mut_ptr();

View File

@ -1,9 +1,11 @@
use std::{
collections::HashMap,
ffi::{c_char, c_void, CString},
env,
ffi::CString,
fs,
io::{stdout, IsTerminal},
path::{Path, PathBuf},
{env, fs, str},
ptr, str,
};
use anyhow::{ensure, Result};
@ -28,12 +30,10 @@ fn mount_inner(
let fstype = CString::new(fstype)?;
// convert to pointers for ffi
let src = src.as_c_str().to_bytes_with_nul().as_ptr() as *const c_char;
let target = target.as_c_str().to_bytes_with_nul().as_ptr() as *const c_char;
let data = data.as_ref().map_or(std::ptr::null(), |data| {
data.as_c_str().to_bytes_with_nul().as_ptr() as *const c_void
});
let fstype = fstype.as_c_str().to_bytes_with_nul().as_ptr() as *const c_char;
let src = src.as_ptr();
let target = target.as_ptr();
let data = data.map_or(ptr::null(), |data| data.as_ptr().cast());
let fstype = fstype.as_ptr();
let ret = {
info!("mounting filesystem");

View File

@ -4,7 +4,7 @@ use std::{
io::{stdin, IsTerminal},
mem,
path::Path,
thread,
ptr, thread,
time::Duration,
};
@ -66,7 +66,7 @@ impl KeyHandle {
let bch_key_magic = BCH_KEY_MAGIC.as_bytes().read_u64::<LittleEndian>().unwrap();
let crypt = sb.sb().crypt().unwrap();
let crypt_ptr = crypt as *const _ as *mut _;
let crypt_ptr = ptr::addr_of!(*crypt).cast_mut();
let mut output: bch_key =
unsafe { bcachefs::derive_passphrase(crypt_ptr, passphrase.get().as_ptr()) };
@ -75,9 +75,9 @@ impl KeyHandle {
let ret = unsafe {
bch2_chacha_encrypt_key(
&mut output as *mut _,
ptr::addr_of_mut!(output),
sb.sb().nonce(),
&mut key as *mut _ as *mut _,
ptr::addr_of_mut!(key).cast(),
mem::size_of_val(&key),
)
};
@ -93,7 +93,7 @@ impl KeyHandle {
keyutils::add_key(
key_type,
key_name,
&output as *const _ as *const _,
ptr::addr_of!(output).cast(),
mem::size_of_val(&output),
keyutils::KEY_SPEC_USER_KEYRING,
)
@ -103,7 +103,7 @@ impl KeyHandle {
info!("Found key in keyring");
Ok(KeyHandle {
_uuid: sb.sb().uuid(),
_id: key_id as c_long,
_id: c_long::from(key_id),
})
} else {
Err(anyhow!("failed to add key to keyring: {}", errno::errno()))

View File

@ -1,4 +1,4 @@
use std::path::Path;
use std::{path::Path, ptr};
use bch_bindgen::c::{
bcache_fs_close, bcache_fs_open, bch_ioctl_subvolume, bchfs_handle, BCH_IOCTL_SUBVOLUME_CREATE,
@ -42,7 +42,7 @@ pub enum BcachefsIoctlPayload {
impl From<&BcachefsIoctlPayload> for *const libc::c_void {
fn from(value: &BcachefsIoctlPayload) -> Self {
match value {
BcachefsIoctlPayload::Subvolume(p) => p as *const _ as *const libc::c_void,
BcachefsIoctlPayload::Subvolume(p) => ptr::addr_of!(p).cast(),
}
}
}