From 8f3ac79670f8428f4553e242cdf2aae8e5ed0b57 Mon Sep 17 00:00:00 2001 From: auronandace Date: Thu, 9 Jul 2026 08:35:50 +0100 Subject: [PATCH] tackle some clippy lints in redox-rt --- redox-rt/src/arch/x86_64.rs | 2 +- redox-rt/src/proc.rs | 31 +++++++++++++++++++++------- redox-rt/src/signal.rs | 28 ++++++++++++------------- redox-rt/src/sys.rs | 41 +++++++++++++++++++++---------------- 4 files changed, 61 insertions(+), 41 deletions(-) diff --git a/redox-rt/src/arch/x86_64.rs b/redox-rt/src/arch/x86_64.rs index adffe424ef..71f69d322a 100644 --- a/redox-rt/src/arch/x86_64.rs +++ b/redox-rt/src/arch/x86_64.rs @@ -80,7 +80,7 @@ pub unsafe fn deactivate_tcb(open_via_dup: &FdGuardUpper) -> Result<()> { env.fsbase = 0; env.gsbase = 0; - file.write(&mut env)?; + file.write(&env)?; crate::TLS_ACTIVATED.store(false, core::sync::atomic::Ordering::Relaxed); Ok(()) } diff --git a/redox-rt/src/proc.rs b/redox-rt/src/proc.rs index e7311ff90f..7a07a84008 100644 --- a/redox-rt/src/proc.rs +++ b/redox-rt/src/proc.rs @@ -69,6 +69,7 @@ pub struct ExtraInfo<'a> { pub same_process: bool, } +#[expect(clippy::too_many_arguments)] pub fn fexec_impl( image_file: FdGuardUpper, thread_fd: &FdGuardUpper, @@ -124,7 +125,13 @@ pub fn fexec_impl( min_mmap_addr = cmp::max(min_mmap_addr, (addr + size).next_multiple_of(PAGE_SIZE)); }; - pread_all(&image_file, u64::from(header.e_phoff), phs).map_err(|_| Error::new(EIO))?; + pread_all( + &image_file, + #[expect(clippy::useless_conversion, reason = "could be 32bit Header")] + u64::from(header.e_phoff), + phs, + ) + .map_err(|_| Error::new(EIO))?; let mut span: Option> = None; for ph_idx in 0..phnum { @@ -190,7 +197,12 @@ pub fn fexec_impl( // PT_INTERP must come before any PT_LOAD, so we don't have to iterate twice. PT_INTERP => { let mut interp = vec![0_u8; segment.p_filesz as usize]; - pread_all(&image_file, u64::from(segment.p_offset), &mut interp)?; + pread_all( + &image_file, + #[expect(clippy::useless_conversion, reason = "could be 32bit ProgramHeader")] + u64::from(segment.p_offset), + &mut interp, + )?; interpreter = Some(interp.into_boxed_slice()); } @@ -235,6 +247,10 @@ pub fn fexec_impl( }; pread_all( &image_file, + #[expect( + clippy::useless_conversion, + reason = "could be 32bit ProgramHeader" + )] u64::from(segment.p_offset), &mut dst_memory[voff..voff + filesz], )?; @@ -399,9 +415,9 @@ pub fn fexec_impl( push(extrainfo.umask as usize)?; push(AT_REDOX_UMASK)?; - push(extrainfo.thr_fd as usize)?; + push(extrainfo.thr_fd)?; push(AT_REDOX_THR_FD)?; - push(extrainfo.proc_fd as usize)?; + push(extrainfo.proc_fd)?; push(AT_REDOX_PROC_FD)?; push(extrainfo.ns_fd.unwrap_or(usize::MAX))?; push(AT_REDOX_NS_FD)?; @@ -519,7 +535,7 @@ pub fn fexec_impl( } unsafe { - deactivate_tcb(&thread_fd)?; + deactivate_tcb(thread_fd)?; } let old_filetable_fd = { @@ -718,6 +734,7 @@ impl<'a> MmapGuard<'a> { pub fn addr(&self) -> usize { self.base } + #[expect(clippy::len_without_is_empty, reason = "this len() is not costly")] pub fn len(&self) -> usize { self.size } @@ -948,7 +965,7 @@ pub fn fork_impl(args: &ForkArgs<'_>) -> Result { let old_mask = crate::signal::get_sigmask()?; let pid = unsafe { Error::demux(__relibc_internal_fork_wrapper( - args as *const ForkArgs as usize, + core::ptr::from_ref::(args) as usize, ))? }; @@ -1006,7 +1023,7 @@ pub fn fork_inner(initial_rsp: *mut usize, args: &ForkArgs) -> Result { target_arch = "riscv64" ))] let arg1 = { - let scratchpad_ptr: *const ForkScratchpad = &scratchpad; + let scratchpad_ptr: *const ForkScratchpad = &raw const scratchpad; scratchpad_ptr as usize }; #[cfg(target_arch = "x86")] diff --git a/redox-rt/src/signal.rs b/redox-rt/src/signal.rs index 5f58da26bf..2cb359b4e9 100644 --- a/redox-rt/src/signal.rs +++ b/redox-rt/src/signal.rs @@ -108,11 +108,9 @@ unsafe fn inner(stack: &mut SigStack) { let os = unsafe { &Tcb::current().unwrap().os_specific }; let stack_ptr = NonNull::from(&mut *stack); - stack.link = core::mem::replace( - unsafe { &mut (*os.arch.get()).last_sigstack }, - Some(stack_ptr), - ) - .map_or_else(core::ptr::null_mut, |x| x.as_ptr()); + stack.link = unsafe { &mut (*os.arch.get()).last_sigstack } + .replace(stack_ptr) + .map_or_else(core::ptr::null_mut, |x| x.as_ptr()); let signals_were_disabled = unsafe { (*os.arch.get()).disable_signals_depth > 0 }; @@ -228,7 +226,7 @@ unsafe fn inner(stack: &mut SigStack) { sigaction( stack.sig_num as c_int, core::ptr::addr_of!(info).cast(), - stack as *mut SigStack as *mut (), + core::ptr::from_mut::(stack).cast::<()>(), ) }; } else if let Some(handler) = unsafe { handler.handler } { @@ -415,7 +413,7 @@ fn convert_old(action: &RawAction) -> Sigaction { SigactionKind::Ignore } else { SigactionKind::Handled { - handler: unsafe { core::mem::transmute(handler as usize) }, + handler: unsafe { core::mem::transmute::(handler) }, } }; @@ -696,7 +694,7 @@ pub fn current_setsighandler_struct() -> SetSighandlerData { thread_control_addr: core::ptr::addr_of!( unsafe { Tcb::current() }.unwrap().os_specific.control ) as usize, - proc_control_addr: &PROC_CONTROL_STRUCT as *const SigProcControl as usize, + proc_control_addr: &raw const PROC_CONTROL_STRUCT as usize, } } @@ -821,12 +819,12 @@ pub fn callback_or_signal_async Result>( let old_allowset = get_allowset_raw(&control.word); set_allowset_raw(&control.word, old_allowset, inner_allowset); let res = callback(); - if let Err(err) = &res { - if err.errno == EINTR { - // Run trampoline if EINTR returned - unsafe { - manually_enter_trampoline(); - } + if let Err(err) = &res + && err.errno == EINTR + { + // Run trampoline if EINTR returned + unsafe { + manually_enter_trampoline(); } } @@ -860,7 +858,7 @@ pub fn await_signal_sync(inner_allowset: u64, timeout: Option<&TimeSpec>) -> Res } let res = match timeout { - Some(t) => syscall::nanosleep(&t, &mut TimeSpec::default()), + Some(t) => syscall::nanosleep(t, &mut TimeSpec::default()), None => syscall::nanosleep( &TimeSpec { tv_sec: i64::MAX, diff --git a/redox-rt/src/sys.rs b/redox-rt/src/sys.rs index c4abafa8ae..98b146ab61 100644 --- a/redox-rt/src/sys.rs +++ b/redox-rt/src/sys.rs @@ -1,5 +1,5 @@ use core::{ - mem::{replace, size_of}, + mem::size_of, ptr::addr_of, sync::atomic::{AtomicU32, Ordering}, }; @@ -122,7 +122,11 @@ pub fn posix_setpriority(which: i32, who: u32, prio: u32) -> Result<(), syscall: this_proc_call( &mut [], CallFlags::empty(), - &[ProcCall::SetProcPriority as u64, who as u64, prio as u64], + &[ + ProcCall::SetProcPriority as u64, + u64::from(who), + u64::from(prio), + ], )?; Ok(()) @@ -137,7 +141,7 @@ pub fn posix_getpriority(which: i32, who: u32) -> Result { let res = this_proc_call( &mut [], CallFlags::empty(), - &[ProcCall::GetProcPriority as u64, who as u64], + &[ProcCall::GetProcPriority as u64, u64::from(who)], )?; Ok(res as u32) @@ -152,7 +156,7 @@ pub unsafe fn sys_futex_wait(addr: *mut u32, val: u32, deadline: Option<&TimeSpe addr as usize, syscall::FUTEX_WAIT, val as usize, - deadline.map_or(0, |d| d as *const _ as usize), + deadline.map_or(0, |d| core::ptr::from_ref(d) as usize), 0, ) } @@ -192,13 +196,13 @@ pub fn sys_call_ro( let _siglock = tmp_disable_signals(); - if payload.len() % size_of::() != 0 { + if payload.len().is_multiple_of(size_of::()) { return Err(Error::new(EINVAL)); } let fd_slice = unsafe { core::slice::from_raw_parts_mut( - payload.as_mut_ptr() as *mut usize, + payload.as_mut_ptr().cast::(), payload.len() / size_of::(), ) }; @@ -269,12 +273,12 @@ pub fn sys_call_wo( } let _siglock = tmp_disable_signals(); - if payload.len() % size_of::() != 0 { + if payload.len().is_multiple_of(size_of::()) { return Err(Error::new(EINVAL)); } let fd_slice = unsafe { core::slice::from_raw_parts( - payload.as_ptr() as *const usize, + payload.as_ptr().cast::(), payload.len() / size_of::(), ) }; @@ -572,8 +576,7 @@ pub fn posix_nanosleep(rqtp: &TimeSpec, rmtp: &mut TimeSpec) -> Result<()> { pub fn setns(fd: usize) -> Option { let mut info = DYNAMIC_PROC_INFO.lock(); let new_fd_guard = FdGuard::new(fd).to_upper().unwrap(); - let old_fd_guard = replace(&mut info.ns_fd, Some(new_fd_guard)); - old_fd_guard + info.ns_fd.replace(new_fd_guard) } pub fn getns() -> Result { let cur_ns = crate::current_namespace_fd()?; @@ -814,9 +817,7 @@ pub fn fcntl(fd: usize, cmd: usize, arg: usize) -> Result { let _siglock = tmp_disable_signals(); let res = unsafe { syscall::syscall3(syscall::SYS_FCNTL, fd, cmd, arg) }; - if res.is_err() { - return res; - } + res?; FILETABLE.lock().set_fd_flags(fd, arg)?; return Ok(0); @@ -922,7 +923,7 @@ pub fn close(fd: usize) -> Result { let res = unsafe { syscall::syscall1(syscall::SYS_CLOSE, fd) }; - if res.is_ok() || res.err().map_or(false, |e| e.errno == EBADF) { + if res.is_ok() || res.err().is_some_and(|e| e.errno == EBADF) { let mut guard = FILETABLE.lock(); let _ = guard.remove(fd); } @@ -933,7 +934,7 @@ pub fn close(fd: usize) -> Result { pub fn close_raw(fd: usize) -> Result { let res = unsafe { syscall::syscall1(syscall::SYS_CLOSE, fd) }; - if res.is_ok() || res.err().map_or(false, |e| e.errno == EBADF) { + if res.is_ok() || res.err().is_some_and(|e| e.errno == EBADF) { let mut guard = FILETABLE.lock(); let _ = guard.remove(fd); } @@ -1064,7 +1065,7 @@ impl FdTbl { } fn sync_size(fd: Option<&FdGuardUpper>, new_size: usize, tag: usize) -> Result<()> { - if let Some(ref fd) = fd { + if let Some(fd) = fd { fd.call_wo( &[], CallFlags::empty(), @@ -1313,13 +1314,13 @@ impl PosixFdTbl { fn is_vacant(&self, handle: usize) -> bool { self.table .get(handle) - .map_or(true, |&flags| !flags.contains(FdFlags::OCCUPIED)) + .is_none_or(|&flags| !flags.contains(FdFlags::OCCUPIED)) } fn is_occupied(&self, handle: usize) -> bool { self.table .get(handle) - .map_or(false, |&flags| flags.contains(FdFlags::OCCUPIED)) + .is_some_and(|&flags| flags.contains(FdFlags::OCCUPIED)) } pub fn resize(&mut self, size: usize) { @@ -1337,6 +1338,10 @@ impl PosixFdTbl { .count() } + pub const fn is_empty(&self) -> bool { + self.table.is_empty() + } + pub fn with_transaction(&mut self, rollback_len: usize, f: F) -> Result where F: FnOnce(&mut Self) -> Result,