From c3f3e051e1ebd49765122c8d14a048e412395a5b Mon Sep 17 00:00:00 2001 From: auronandace Date: Sun, 8 Feb 2026 15:39:48 +0000 Subject: [PATCH] add unused-unsafe lint --- Cargo.toml | 4 ++++ src/header/dirent/mod.rs | 7 ++----- src/header/errno/mod.rs | 4 ++-- src/header/inttypes/mod.rs | 40 +++++++++++++++++-------------------- src/header/netdb/lookup.rs | 26 +++++++++--------------- src/header/pthread/attr.rs | 2 +- src/header/stdio/mod.rs | 3 +-- src/header/stdlib/mod.rs | 2 +- src/header/time/strptime.rs | 12 ++++------- src/header/wchar/utf8.rs | 2 +- src/header/wchar/wscanf.rs | 11 ++++------ 11 files changed, 47 insertions(+), 66 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f7ce1d3877..ba7bf1c8f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,8 @@ non_camel_case_types = "allow" unpredictable_function_pointer_comparisons = "deny" unsafe_op_in_unsafe_fn = "deny" unused_mut = "deny" +unused_unsafe = "deny" +unused_variables = "allow" # TODO review occurrences (too many for now) [lints.rust] dangling_pointers_from_temporaries = "deny" @@ -52,6 +54,8 @@ non_camel_case_types = "allow" unpredictable_function_pointer_comparisons = "deny" unsafe_op_in_unsafe_fn = "deny" unused_mut = "deny" +unused_unsafe = "deny" +unused_variables = "allow" # TODO review occurrences (too many for now) [build-dependencies] cc = "1" diff --git a/src/header/dirent/mod.rs b/src/header/dirent/mod.rs index e9c5b8300c..1cf62e6a04 100644 --- a/src/header/dirent/mod.rs +++ b/src/header/dirent/mod.rs @@ -11,9 +11,8 @@ use crate::{ error::{Errno, ResultExt, ResultExtPtrMut}, fs::File, header::{ - errno::{self, EINVAL, EIO, ENOMEM, ENOTDIR}, + errno::{EINVAL, EIO, ENOMEM, ENOTDIR}, fcntl, stdlib, string, sys_stat, - unistd::{SEEK_CUR, SEEK_SET}, }, out::Out, platform::{ @@ -52,9 +51,7 @@ impl DIR { } pub fn from_fd(fd: c_int) -> Result, Errno> { let mut stat = sys_stat::stat::default(); - unsafe { - Sys::fstat(fd, Out::from_mut(&mut stat))?; - } + Sys::fstat(fd, Out::from_mut(&mut stat))?; if (stat.st_mode & sys_stat::S_IFMT) != sys_stat::S_IFDIR { return Err(Errno(ENOTDIR)); } diff --git a/src/header/errno/mod.rs b/src/header/errno/mod.rs index 6bc1451a61..aceb031138 100644 --- a/src/header/errno/mod.rs +++ b/src/header/errno/mod.rs @@ -30,7 +30,7 @@ pub extern "C" fn __errno_location() -> *mut c_int { /// The `program_invocation_name` variable is a GNU extension. #[unsafe(no_mangle)] pub unsafe extern "C" fn __program_invocation_name() -> *mut *mut c_char { - unsafe { &raw mut platform::program_invocation_name } + &raw mut platform::program_invocation_name } /// Get the directory-less name used to invoke the program. @@ -41,7 +41,7 @@ pub unsafe extern "C" fn __program_invocation_name() -> *mut *mut c_char { /// The `program_invocation_short_name` variable is a GNU extension. #[unsafe(no_mangle)] pub unsafe extern "C" fn __program_invocation_short_name() -> *mut *mut c_char { - unsafe { &raw mut platform::program_invocation_short_name } + &raw mut platform::program_invocation_short_name } pub const EPERM: c_int = 1; /* Operation not permitted */ diff --git a/src/header/inttypes/mod.rs b/src/header/inttypes/mod.rs index 07943d3e5b..08e38807e5 100644 --- a/src/header/inttypes/mod.rs +++ b/src/header/inttypes/mod.rs @@ -39,17 +39,15 @@ pub unsafe extern "C" fn strtoimax( endptr: *mut *mut c_char, base: c_int, ) -> intmax_t { - unsafe { - strto_impl!( - intmax_t, - false, - intmax_t::max_value(), - intmax_t::min_value(), - s, - endptr, - base - ) - } + strto_impl!( + intmax_t, + false, + intmax_t::max_value(), + intmax_t::min_value(), + s, + endptr, + base + ) } /// See . @@ -59,17 +57,15 @@ pub unsafe extern "C" fn strtoumax( endptr: *mut *mut c_char, base: c_int, ) -> uintmax_t { - unsafe { - strto_impl!( - uintmax_t, - false, - uintmax_t::max_value(), - uintmax_t::min_value(), - s, - endptr, - base - ) - } + strto_impl!( + uintmax_t, + false, + uintmax_t::max_value(), + uintmax_t::min_value(), + s, + endptr, + base + ) } // wcstoimax(), wcstoumax() currently defined in header::wchar? diff --git a/src/header/netdb/lookup.rs b/src/header/netdb/lookup.rs index 834456bf7f..c5da3fd320 100644 --- a/src/header/netdb/lookup.rs +++ b/src/header/netdb/lookup.rs @@ -1,8 +1,4 @@ -use alloc::{ - boxed::Box, - string::{String, ToString}, - vec::{IntoIter, Vec}, -}; +use alloc::{boxed::Box, string::ToString, vec::Vec}; use core::mem; use crate::{ @@ -43,12 +39,10 @@ pub fn lookup_host(host: &str) -> Result { if let Some(dns_addr) = parse_ipv4_string(&dns_string) { let mut timespec = timespec::default(); - unsafe { - Sys::clock_gettime( - time::constants::CLOCK_REALTIME, - Out::from_mut(&mut timespec), - ); - } + Sys::clock_gettime( + time::constants::CLOCK_REALTIME, + Out::from_mut(&mut timespec), + ); let tid = (timespec.tv_nsec >> 16) as u16; let packet = Dns { @@ -147,12 +141,10 @@ pub fn lookup_addr(addr: in_addr) -> Result>, c_int> { ); let mut timespec = timespec::default(); - unsafe { - Sys::clock_gettime( - time::constants::CLOCK_REALTIME, - Out::from_mut(&mut timespec), - ) - }; + Sys::clock_gettime( + time::constants::CLOCK_REALTIME, + Out::from_mut(&mut timespec), + ); let tid = (timespec.tv_nsec >> 16) as u16; let packet = Dns { diff --git a/src/header/pthread/attr.rs b/src/header/pthread/attr.rs index 3902f28868..3061fba974 100644 --- a/src/header/pthread/attr.rs +++ b/src/header/pthread/attr.rs @@ -159,7 +159,7 @@ pub unsafe extern "C" fn pthread_attr_setschedparam( param: *const sched_param, ) -> c_int { unsafe { - (*attr.cast::()).param = unsafe { param.read() }; + (*attr.cast::()).param = param.read(); } 0 } diff --git a/src/header/stdio/mod.rs b/src/header/stdio/mod.rs index 9ffc6fbb70..f15dfdb5f8 100644 --- a/src/header/stdio/mod.rs +++ b/src/header/stdio/mod.rs @@ -35,7 +35,6 @@ use crate::{ self, ERRNO, Pal, Sys, WriteByte, types::{c_char, c_int, c_long, c_uint, c_ulonglong, c_void, off_t, size_t}, }, - sync::Mutex, }; pub use self::constants::*; @@ -420,7 +419,7 @@ pub unsafe extern "C" fn fflush(stream: *mut FILE) -> c_int { return EOF; } } else { - let mut stream = unsafe { unsafe { (*stream).lock() } }; + let mut stream = unsafe { (*stream).lock() }; if stream.flush().is_err() { return EOF; } diff --git a/src/header/stdlib/mod.rs b/src/header/stdlib/mod.rs index f43b80c508..b535038049 100644 --- a/src/header/stdlib/mod.rs +++ b/src/header/stdlib/mod.rs @@ -1102,7 +1102,7 @@ pub unsafe extern "C" fn rand() -> c_int { None => { let mut rng = XorShiftRng::from_seed([1; 16]); let ret = rng_sampler().sample(&mut rng); - unsafe { RNG = Some(rng) }; + RNG = Some(rng); ret } } diff --git a/src/header/time/strptime.rs b/src/header/time/strptime.rs index 3225540d95..fd67491626 100644 --- a/src/header/time/strptime.rs +++ b/src/header/time/strptime.rs @@ -2,17 +2,13 @@ // // See . -use crate::{ - header::{string::strlen, time::tm}, - platform::types::size_t, -}; -use alloc::{string::String, vec::Vec}; +use crate::header::time::tm; +use alloc::string::String; use core::{ ffi::{CStr, c_char, c_int, c_void}, - mem::MaybeUninit, ptr, ptr::NonNull, - slice, str, + str, }; /// For convenience, we define some helper constants for the C-locale. @@ -153,7 +149,7 @@ pub unsafe extern "C" fn strptime( 'd' | 'e' => { // parse a 2-digit day (with or without leading zero) let (val, len) = match parse_int(&input_str[index_in_input..], 2, false) { - Some(v) => unsafe { v }, + Some(v) => v, None => return ptr::null_mut(), }; unsafe { diff --git a/src/header/wchar/utf8.rs b/src/header/wchar/utf8.rs index b26970227c..191bb3cb0b 100644 --- a/src/header/wchar/utf8.rs +++ b/src/header/wchar/utf8.rs @@ -52,7 +52,7 @@ pub unsafe fn mbrtowc(pwc: *mut wchar_t, s: *const c_char, n: usize, ps: *mut mb } if size == 0 { platform::ERRNO.set(errno::EILSEQ); - return unsafe { -1isize as usize }; + return -1isize as usize; } let slice = unsafe { slice::from_raw_parts(s as *const u8, size) }; diff --git a/src/header/wchar/wscanf.rs b/src/header/wchar/wscanf.rs index a0bb786970..c21f4993e1 100644 --- a/src/header/wchar/wscanf.rs +++ b/src/header/wchar/wscanf.rs @@ -362,18 +362,15 @@ unsafe fn inner_scanf( } } - let mut ptr: Option<*mut $type> = if ignore { - None - } else { - Some(unsafe { ap.arg() }) - }; + let mut ptr: Option<*mut $type> = + if ignore { None } else { Some(ap.arg()) }; while width.map(|w| w > 0).unwrap_or(true) && !(wc_as_char!(wchar)).is_whitespace() { if let Some(ref mut ptr) = ptr { - unsafe { **ptr = wchar as $type }; - *ptr = unsafe { ptr.offset(1) }; + **ptr = wchar as $type; + *ptr = ptr.offset(1); } width = width.map(|w| w - 1); if width.map(|w| w > 0).unwrap_or(true) && !read!() {