fix: clippy::ptr_as_ptr

https://rust-lang.github.io/rust-clippy/master/index.html#ptr_as_ptr

Closes #12136
This commit is contained in:
xtqqczze
2025-12-10 22:30:52 +00:00
committed by Johannes Altmanninger
parent cc29216ea9
commit 27852a6734
12 changed files with 28 additions and 25 deletions

View File

@@ -1,7 +1,6 @@
use crate::arg::ToArg;
use crate::locale::{C_LOCALE, EN_US_LOCALE, Locale};
use crate::{Error, FormatString, sprintf_locale};
use libc::c_char;
use std::f64::consts::{E, PI, TAU};
use std::ffi::CStr;
use std::fmt;
@@ -890,10 +889,16 @@ fn libc_sprintf_one_float_with_precision<'a>(
fmt: &'a CStr,
) -> impl FnMut(usize, f64) -> &'a str {
|preci, float_val| unsafe {
let storage_ptr = storage.as_mut_ptr() as *mut c_char;
let len = libc::snprintf(storage_ptr, storage.len(), fmt.as_ptr(), preci, float_val);
let storage_ptr = storage.as_mut_ptr();
let len = libc::snprintf(
storage_ptr.cast(),
storage.len(),
fmt.as_ptr(),
preci,
float_val,
);
assert!(len >= 0);
let sl = std::slice::from_raw_parts(storage_ptr as *const u8, len as usize);
let sl = std::slice::from_raw_parts(storage_ptr, len as usize);
std::str::from_utf8(sl).unwrap()
}
}

View File

@@ -242,8 +242,8 @@ pub fn is_same_node(lhs: &dyn Node, rhs: &dyn Node) -> bool {
// Note this is performance-sensitive.
// Different base pointers => not the same.
let lptr = std::ptr::from_ref(lhs) as *const ();
let rptr = std::ptr::from_ref(rhs) as *const ();
let lptr = std::ptr::from_ref(lhs).cast::<()>();
let rptr = std::ptr::from_ref(rhs).cast::<()>();
if !std::ptr::eq(lptr, rptr) {
return false;
}

View File

@@ -1879,8 +1879,8 @@ fn apply_var_assignments<T: AsRef<wstr>>(
let vars = parser.vars();
assert_eq!(
std::ptr::from_ref(self.ctx.vars()) as *const (),
std::ptr::from_ref(vars) as *const (),
std::ptr::from_ref(self.ctx.vars()).cast::<()>(),
std::ptr::from_ref(vars).cast::<()>(),
"Don't know how to tab complete with a parser but a different variable set"
);

View File

@@ -49,7 +49,7 @@
use errno::{errno, set_errno};
use libc::{
EACCES, ENOENT, ENOEXEC, ENOTDIR, EPIPE, EXIT_FAILURE, EXIT_SUCCESS, STDERR_FILENO,
STDIN_FILENO, STDOUT_FILENO, c_char,
STDIN_FILENO, STDOUT_FILENO,
};
use nix::fcntl::OFlag;
use nix::sys::stat;
@@ -414,7 +414,7 @@ fn safe_launch_process(
if nargs <= MAXARGS {
// +1 for /bin/sh, +1 for terminating nullptr
let mut argv2 = [std::ptr::null(); 1 + MAXARGS + 1];
let bshell = PATH_BSHELL.as_ptr() as *const c_char;
let bshell = PATH_BSHELL.as_ptr().cast();
argv2[0] = bshell;
argv2[1..argv.len() + 1].copy_from_slice(argv);
// The command to call should use the full path,

View File

@@ -16,7 +16,7 @@
use crate::threads::assert_is_background_thread;
use crate::wutil::perror;
use errno::errno;
use libc::{EAGAIN, EINTR, EWOULDBLOCK, c_void};
use libc::{EAGAIN, EINTR, EWOULDBLOCK};
cfg_if!(
if #[cfg(have_eventfd)] {
@@ -95,7 +95,7 @@ pub fn try_consume(&self) -> bool {
ret = unsafe {
libc::read(
self.read_fd(),
buff.as_mut_ptr() as *mut c_void,
buff.as_mut_ptr().cast(),
std::mem::size_of_val(&buff),
)
};

View File

@@ -85,7 +85,7 @@ pub fn flog_impl_async_safe(fd: i32, s: impl FloggableDisplayAsyncSafe) {
// Note we deliberately do not retry on signals, etc.
// This is used to report error messages after fork() in the child process.
unsafe {
let _ = libc::write(fd, bytes.as_ptr() as *const libc::c_void, bytes.len());
let _ = libc::write(fd, bytes.as_ptr().cast(), bytes.len());
}
}

View File

@@ -172,7 +172,7 @@ pub(crate) fn spawn(
let cmdcstr = unsafe { CStr::from_ptr(cmd) };
if spawn_err.0 == libc::ENOEXEC && is_thompson_shell_script(cmdcstr) {
// Create a new argv with /bin/sh prepended.
let mut argv2: Vec<*mut c_char> = vec![PATH_BSHELL.as_ptr().cast_mut() as *mut c_char];
let mut argv2 = vec![PATH_BSHELL.as_ptr().cast_mut().cast()];
// The command to call should use the full path,
// not what we would pass as argv0.
@@ -189,7 +189,7 @@ pub(crate) fn spawn(
check_fail(unsafe {
libc::posix_spawn(
&mut pid,
PATH_BSHELL.as_ptr() as *const c_char,
PATH_BSHELL.as_ptr().cast(),
&self.actions.0,
&self.attr.0,
argv2.as_ptr(),

View File

@@ -1138,7 +1138,7 @@ fn format_history_record(
let mut timestamp_str = [0_u8; MAX_TIMESTAMP_LENGTH];
if unsafe {
libc::strftime(
timestamp_str.as_mut_ptr() as *mut libc::c_char,
timestamp_str.as_mut_ptr().cast(),
MAX_TIMESTAMP_LENGTH,
show_time_format.as_ptr(),
timestamp.as_ptr(),

View File

@@ -494,11 +494,11 @@ pub fn new() -> Self {
}
pub fn remove(&mut self, element: &dyn IoData) {
// Discard vtable pointers when comparing.
let e1 = std::ptr::from_ref(element) as *const ();
let e1 = std::ptr::from_ref(element).cast::<()>();
let idx = self
.0
.iter()
.position(|e2| Arc::as_ptr(e2) as *const () == e1)
.position(|e2| Arc::as_ptr(e2).cast::<()>() == e1)
.expect("Element not found");
self.0.remove(idx);
}

View File

@@ -5891,9 +5891,7 @@ fn check_for_orphaned_process(loop_count: usize, shell_pgid: libc::pid_t) -> boo
}
let mut tmp = 0 as libc::c_char;
if unsafe { libc::read(tty_fd.fd(), &raw mut tmp as *mut libc::c_void, 1) } < 0
&& errno().0 == EIO
{
if unsafe { libc::read(tty_fd.fd(), (&raw mut tmp).cast(), 1) } < 0 && errno().0 == EIO {
we_think_we_are_orphaned = true;
}
}

View File

@@ -455,11 +455,11 @@ fn std_thread_inherits_sigmask() {
// Compare the sigset_t values
unsafe {
let t1_sigset_slice = std::slice::from_raw_parts(
&raw const t1_set as *const u8,
(&raw const t1_set).cast::<u8>(),
core::mem::size_of::<libc::sigset_t>(),
);
let t2_sigset_slice = std::slice::from_raw_parts(
&raw const t2_set as *const u8,
(&raw const t2_set).cast::<u8>(),
core::mem::size_of::<libc::sigset_t>(),
);

View File

@@ -478,7 +478,7 @@ mod tests {
use crate::fds::AutoCloseFd;
use crate::tests::prelude::*;
use crate::wchar::prelude::*;
use libc::{O_CREAT, O_RDWR, O_TRUNC, SEEK_SET, c_void};
use libc::{O_CREAT, O_RDWR, O_TRUNC, SEEK_SET};
use rand::Rng;
use std::{ffi::CString, ptr};
@@ -683,7 +683,7 @@ fn test_wwrite_to_fd() {
if size == 0 {
ptr::null_mut()
} else {
contents.as_mut_ptr() as *mut c_void
contents.as_mut_ptr().cast()
},
narrow.len(),
)