Merge branch 'manual-let-else' into 'master'

apply manual_let_else clippy lint

See merge request redox-os/relibc!1536
This commit is contained in:
Jeremy Soller
2026-07-08 06:01:40 -06:00
18 changed files with 116 additions and 182 deletions
+2
View File
@@ -29,6 +29,7 @@ cast_ptr_alignment = "allow" # TODO review occurrences
cast_sign_loss = "allow" # TODO review occurrences
ignored_unit_patterns = "deny"
implicit_clone = "deny"
manual_let_else = "deny"
missing_errors_doc = "allow" # TODO review occurrences
missing_panics_doc = "allow" # TODO review occurrences
missing_safety_doc = "allow" # TODO review occurrences
@@ -38,6 +39,7 @@ ptr_as_ptr = "warn" # TODO review occurrences
ptr_cast_constness = "warn" # TODO review occurrences
ptr_offset_by_literal = "deny"
ref_as_ptr = "warn" # TODO review occurrences
type_complexity = "allow" # TODO review occurrences
upper_case_acronyms = "allow" # TODO review occurrences
zero_ptr = "deny" # must allow on public constants due to cbindgen issue
+3 -6
View File
@@ -74,12 +74,9 @@ pub unsafe extern "C" fn crypt_r(
}
let key = unsafe { CStr::from_ptr(key) }.to_bytes();
let setting = match unsafe { CStr::from_ptr(setting) }.to_str() {
Ok(s) => s,
Err(_) => {
platform::ERRNO.set(EINVAL);
return ptr::null_mut();
}
let Ok(setting) = unsafe { CStr::from_ptr(setting) }.to_str() else {
platform::ERRNO.set(EINVAL);
return ptr::null_mut();
};
let encoded = if setting.starts_with('$') {
+13 -19
View File
@@ -38,7 +38,10 @@ pub const RTLD_LOCAL: c_int = 0x0000;
/// The identifier lookup happens in the normal global scope; that is, a
/// search for an identifier using `handle` would find the same definition
/// as a direct use of this identifier in the program code.
#[allow(clippy::zero_ptr)] // related cbindgen issue: https://github.com/mozilla/cbindgen/issues/948
#[expect(
clippy::zero_ptr,
reason = "cbindgen issue: https://github.com/mozilla/cbindgen/issues/948"
)]
pub const RTLD_DEFAULT: *mut c_void = 0 as *mut c_void; // XXX: cbindgen doesn't like ptr::null_mut() for publically exported constants
static ERROR_NOT_SUPPORTED: &core::ffi::CStr = c"dlfcn not supported";
@@ -108,12 +111,9 @@ pub unsafe extern "C" fn dlopen(cfilename: *const c_char, flags: c_int) -> *mut
}
};
let tcb = match unsafe { Tcb::current() } {
Some(tcb) => tcb,
None => {
ERROR.store(ERROR_NOT_SUPPORTED.as_ptr() as usize, Ordering::SeqCst);
return ptr::null_mut();
}
let Some(tcb) = (unsafe { Tcb::current() }) else {
ERROR.store(ERROR_NOT_SUPPORTED.as_ptr() as usize, Ordering::SeqCst);
return ptr::null_mut();
};
if tcb.linker_ptr.is_null() {
@@ -150,12 +150,9 @@ pub unsafe extern "C" fn dlsym(handle: *mut c_void, symbol: *const c_char) -> *m
// FIXME(andypython): just call obj.scope.get_sym() directly or search the
// global scope. The rest is unnecessary as Linker::get_sym() does not
// depend on the Linker state.
let tcb = match unsafe { Tcb::current() } {
Some(tcb) => tcb,
None => {
ERROR.store(ERROR_NOT_SUPPORTED.as_ptr() as usize, Ordering::SeqCst);
return ptr::null_mut();
}
let Some(tcb) = (unsafe { Tcb::current() }) else {
ERROR.store(ERROR_NOT_SUPPORTED.as_ptr() as usize, Ordering::SeqCst);
return ptr::null_mut();
};
if tcb.linker_ptr.is_null() {
@@ -178,12 +175,9 @@ pub unsafe extern "C" fn dlsym(handle: *mut c_void, symbol: *const c_char) -> *m
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/dlclose.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn dlclose(handle: *mut c_void) -> c_int {
let tcb = match unsafe { Tcb::current() } {
Some(tcb) => tcb,
None => {
ERROR.store(ERROR_NOT_SUPPORTED.as_ptr() as usize, Ordering::SeqCst);
return -1;
}
let Some(tcb) = (unsafe { Tcb::current() }) else {
ERROR.store(ERROR_NOT_SUPPORTED.as_ptr() as usize, Ordering::SeqCst);
return -1;
};
if tcb.linker_ptr.is_null() {
+2 -3
View File
@@ -27,9 +27,8 @@ pub unsafe extern "C" fn strfmon(
}
// Convert the format string from C to string
let format_str = match unsafe { CStr::from_ptr(format) }.to_str() {
Ok(s) => s,
Err(_) => return -1, // Invalid format string
let Ok(format_str) = unsafe { CStr::from_ptr(format) }.to_str() else {
return -1;
};
// Create a mutable slice for the output buffer
+5 -9
View File
@@ -387,12 +387,9 @@ pub unsafe extern "C" fn gethostbyname(name: *const c_char) -> *mut hostent {
return ptr::null_mut();
}
};
let host_addr = match host.into_iter().next() {
Some(result) => result,
None => {
H_ERRNO.set(HOST_NOT_FOUND);
return ptr::null_mut();
}
let Some(host_addr) = host.into_iter().next() else {
H_ERRNO.set(HOST_NOT_FOUND);
return ptr::null_mut();
};
let host_name: Vec<u8> = name_cstr.to_bytes().to_vec();
@@ -730,9 +727,8 @@ pub unsafe extern "C" fn getservent() -> *mut servent {
Some(serv_name) => serv_name.bytes().chain(Some(b'\0')).collect(),
None => continue,
};
let port_proto = match iter.next() {
Some(port_proto) => port_proto,
None => continue,
let Some(port_proto) = iter.next() else {
continue;
};
let mut split = port_proto.split('/');
let mut port: Vec<u8> = match split.next() {
-4
View File
@@ -81,10 +81,6 @@ pub unsafe extern "C" fn pthread_barrier_init(
0
}
fn unlikely(condition: bool) -> bool {
condition
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/pthread_barrier_wait.html>.
///
/// Synchronizes participating threads at the barrier referenced by `barrier`.
+2 -3
View File
@@ -71,9 +71,8 @@ pub unsafe extern "C" fn openpty(
&mut tmp_name
};
let (master, slave) = match unsafe { openpty_inner(name) } {
Ok(ok) => ok,
Err(()) => return -1,
let Ok((master, slave)) = (unsafe { openpty_inner(name) }) else {
return -1;
};
if !termp.is_null() {
+4 -6
View File
@@ -194,9 +194,8 @@ fn pwd_lookup<F>(mut matches: F, destination: Option<DestBuffer>) -> Result<Owne
where
F: FnMut(&passwd) -> bool,
{
let file = match File::open(c"/etc/passwd".into(), fcntl::O_RDONLY) {
Ok(file) => file,
Err(_) => return Err(Cause::Other),
let Ok(file) = File::open(c"/etc/passwd".into(), fcntl::O_RDONLY) else {
return Err(Cause::Other);
};
let mut reader = BufReader::new(file);
@@ -246,9 +245,8 @@ pub extern "C" fn getpwent() -> *mut passwd {
let reader = match unsafe { &mut *READER.as_mut_ptr() } {
Some(reader) => reader,
None => {
let file = match File::open(c"/etc/passwd".into(), fcntl::O_RDONLY) {
Ok(file) => file,
Err(_) => return ptr::null_mut(),
let Ok(file) = File::open(c"/etc/passwd".into(), fcntl::O_RDONLY) else {
return ptr::null_mut();
};
let reader = BufReader::new(file);
unsafe {
+5 -6
View File
@@ -57,18 +57,17 @@ pub unsafe extern "C" fn getdelim(
delim: c_int,
stream: *mut FILE,
) -> ssize_t {
let (lineptr, n, stream) = if let (Some(ptr), Some(n), Some(file)) =
let (Some(lineptr), Some(n), Some(stream)) =
(unsafe { lineptr.as_mut() }, unsafe { n.as_mut() }, unsafe {
stream.as_mut()
}) {
(ptr, n, file)
} else {
})
else {
ERRNO.set(EINVAL);
return -1 as ssize_t;
return -1;
};
if unsafe { feof(stream) } != 0 || unsafe { ferror(stream) } != 0 {
return -1 as ssize_t;
return -1;
}
// POSIX specifies UB but we test anyway
+5 -9
View File
@@ -490,9 +490,8 @@ pub unsafe extern "C" fn fgets(
// TODO: When NLL is a thing, this block can be flattened out
let (read, exit) = {
let buf = match stream.fill_buf() {
Ok(buf) => buf,
Err(_) => return ptr::null_mut(),
let Ok(buf) = stream.fill_buf() else {
return ptr::null_mut();
};
if buf.is_empty() {
break;
@@ -993,12 +992,9 @@ pub unsafe extern "C" fn popen(command: *const c_char, mode: *const c_char) -> *
}
}
let write = match write_opt {
Some(some) => some,
None => {
ERRNO.set(errno::EINVAL);
return ptr::null_mut();
}
let Some(write) = write_opt else {
ERRNO.set(errno::EINVAL);
return ptr::null_mut();
};
let mut pipes = [-1, -1];
+14 -22
View File
@@ -1065,12 +1065,9 @@ pub(crate) unsafe fn inner_printf<T: c_str::Kind>(
let mut string = String::new();
while unsafe { *ptr } != 0 {
let c = match char::from_u32(unsafe { *ptr } as _) {
Some(c) => c,
None => {
platform::ERRNO.set(EILSEQ);
return Err(io::last_os_error());
}
let Some(c) = char::from_u32(unsafe { *ptr } as _) else {
platform::ERRNO.set(EILSEQ);
return Err(io::last_os_error());
};
if string.len() + c.len_utf8() >= max {
break;
@@ -1102,12 +1099,9 @@ pub(crate) unsafe fn inner_printf<T: c_str::Kind>(
pad(w, left, b' ', 1..pad_space)?;
}
VaArg::wint_t(c) => {
let c = match char::from_u32(c as _) {
Some(c) => c,
None => {
platform::ERRNO.set(EILSEQ);
return Err(io::last_os_error());
}
let Some(c) = char::from_u32(c as _) else {
platform::ERRNO.set(EILSEQ);
return Err(io::last_os_error());
};
let mut buf = [0; 4];
@@ -1119,11 +1113,10 @@ pub(crate) unsafe fn inner_printf<T: c_str::Kind>(
}
}
FmtKind::Pointer => {
let ptr = match unsafe {
varargs.get(index, &mut ap, Some((arg.fmtkind, arg.intkind)))
} {
VaArg::pointer(p) => p,
_ => panic!("this should not be possible"),
let VaArg::pointer(ptr) =
(unsafe { varargs.get(index, &mut ap, Some((arg.fmtkind, arg.intkind))) })
else {
panic!("this should not be possible");
};
let mut len = 1;
@@ -1146,11 +1139,10 @@ pub(crate) unsafe fn inner_printf<T: c_str::Kind>(
pad(w, left, b' ', len..pad_space)?;
}
FmtKind::GetWritten => {
let ptr = match unsafe {
varargs.get(index, &mut ap, Some((arg.fmtkind, arg.intkind)))
} {
VaArg::pointer(p) => p,
_ => panic!("this should not be possible"),
let VaArg::pointer(ptr) =
(unsafe { varargs.get(index, &mut ap, Some((arg.fmtkind, arg.intkind))) })
else {
panic!("this should not be possible");
};
match intkind {
+5 -6
View File
@@ -18,12 +18,12 @@ use crate::{
header::{
ctype,
errno::{self, *},
fcntl::*,
fcntl::{O_ACCMODE, O_CLOEXEC, O_CREAT, O_EXCL, O_PATH, O_RDWR, open},
limits,
stdio::flush_io_streams,
stdlib::sort::{QsortContext, QsortRContext},
string::*,
sys_ioctl::*,
string::{strlen, strncmp},
sys_ioctl::{TIOCGPTN, TIOCSPTLCK, ioctl},
time::constants::CLOCK_MONOTONIC,
unistd::{self, _SC_PAGESIZE, sysconf},
wchar::*,
@@ -1194,9 +1194,8 @@ pub unsafe extern "C" fn realpath(pathname: *const c_char, resolved: *mut c_char
let out = unsafe { slice::from_raw_parts_mut(ptr.cast::<u8>(), limits::PATH_MAX) };
{
let file = match File::open(unsafe { CStr::from_ptr(pathname) }, O_PATH | O_CLOEXEC) {
Ok(file) => file,
Err(_) => return ptr::null_mut(),
let Ok(file) = File::open(unsafe { CStr::from_ptr(pathname) }, O_PATH | O_CLOEXEC) else {
return ptr::null_mut();
};
let len = out.len();
+4 -10
View File
@@ -73,12 +73,9 @@ pub unsafe extern "C" fn shmget(key: key_t, size: size_t, shmflg: c_int) -> c_in
}
if (oflag & O_CREAT) != 0 {
let total_size = match size.checked_add(mem::size_of::<ShmHeader>()) {
Some(s) => s,
None => {
ERRNO.set(EINVAL);
return -1;
}
let Some(total_size) = size.checked_add(mem::size_of::<ShmHeader>()) else {
ERRNO.set(EINVAL);
return -1;
};
if ftruncate(fd, total_size as i64) < 0 {
return -1;
@@ -110,10 +107,7 @@ pub unsafe extern "C" fn shmat(
}
let res = unsafe { Sys::mmap(core::ptr::null_mut(), size, prot, MAP_SHARED, shmid, 0) };
let ptr = match res {
Ok(p) => p,
Err(_) => return SHM_FAILED,
};
let Ok(ptr) = res else { return SHM_FAILED };
let header = ptr.cast::<ShmHeader>();
unsafe {
+8 -13
View File
@@ -441,14 +441,11 @@ pub unsafe extern "C" fn mktime(timeptr: *mut tm) -> time_t {
let minute = unsafe { (*timeptr).tm_min } as _;
let second = unsafe { (*timeptr).tm_sec } as _;
let naive_local = match NaiveDate::from_ymd_opt(year, month, day)
let Some(naive_local) = NaiveDate::from_ymd_opt(year, month, day)
.and_then(|date| date.and_hms_opt(hour, minute, second))
{
Some(datetime) => datetime,
None => {
platform::ERRNO.set(EOVERFLOW);
return -1;
}
else {
platform::ERRNO.set(EOVERFLOW);
return -1;
};
let tz = time_zone();
@@ -542,9 +539,8 @@ pub unsafe extern "C" fn time(tloc: *mut time_t) -> time_t {
#[unsafe(no_mangle)]
pub unsafe extern "C" fn timegm(tm: *mut tm) -> time_t {
let tm_val = unsafe { &mut *tm };
let dt = match convert_tm_generic(&Utc, tm_val) {
Some(dt) => dt,
None => return -1,
let Some(dt) = convert_tm_generic(&Utc, tm_val) else {
return -1;
};
unsafe {
@@ -564,9 +560,8 @@ pub unsafe extern "C" fn timegm(tm: *mut tm) -> time_t {
pub unsafe extern "C" fn timelocal(tm: *mut tm) -> time_t {
let tm_val = unsafe { &mut *tm };
let tz = time_zone();
let dt = match convert_tm_generic(&tz, tm_val) {
Some(dt) => dt,
None => return -1,
let Some(dt) = convert_tm_generic(&tz, tm_val) else {
return -1;
};
let tz_name = CString::new(tz.name()).unwrap();
+36 -51
View File
@@ -52,21 +52,15 @@ pub unsafe extern "C" fn strptime(
tm: *mut tm,
) -> *mut c_char {
// Validate inputs
let buf_ptr = if let Some(ptr) = NonNull::new(buf.cast::<c_void>().cast_mut()) {
ptr
} else {
let Some(buf_ptr) = NonNull::new(buf.cast::<c_void>().cast_mut()) else {
return ptr::null_mut();
};
//
let fmt_ptr = if let Some(ptr) = NonNull::new(format.cast::<c_void>().cast_mut()) {
ptr
} else {
let Some(fmt_ptr) = NonNull::new(format.cast::<c_void>().cast_mut()) else {
return ptr::null_mut();
};
let tm_ptr = if let Some(ptr) = NonNull::new(tm) {
ptr
} else {
let Some(tm_ptr) = NonNull::new(tm) else {
return ptr::null_mut();
};
@@ -152,9 +146,8 @@ pub unsafe extern "C" fn strptime(
// Day of Month: %d / %e
'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) => v,
None => return ptr::null_mut(),
let Some((val, len)) = parse_int(&input_str[index_in_input..], 2, false) else {
return ptr::null_mut();
};
unsafe {
(*tm).tm_mday = val as c_int;
@@ -169,9 +162,8 @@ pub unsafe extern "C" fn strptime(
// Month: %m
'm' => {
// parse a 2-digit month
let (val, len) = match parse_int(&input_str[index_in_input..], 2, false) {
Some(v) => v,
None => return ptr::null_mut(),
let Some((val, len)) = parse_int(&input_str[index_in_input..], 2, false) else {
return ptr::null_mut();
};
// tm_mon is 0-based (0 = Jan, 1 = Feb,...)
unsafe {
@@ -186,9 +178,8 @@ pub unsafe extern "C" fn strptime(
// Year without century: %y
'y' => {
// parse a 2-digit year
let (val, len) = match parse_int(&input_str[index_in_input..], 2, false) {
Some(v) => v,
None => return ptr::null_mut(),
let Some((val, len)) = parse_int(&input_str[index_in_input..], 2, false) else {
return ptr::null_mut();
};
// According to POSIX, %y in strptime is [00,99], and the "year" is 1900..1999 for [00..99],
// but the standard says: "values in [69..99] refer to 1969..1999, [00..68] => 2000..2068"
@@ -203,9 +194,8 @@ pub unsafe extern "C" fn strptime(
'Y' => {
// parse up to 4-digit (or more) year
// We allow more than 4 digits if needed
let (val, len) = match parse_int(&input_str[index_in_input..], 4, true) {
Some(v) => v,
None => return ptr::null_mut(),
let Some((val, len)) = parse_int(&input_str[index_in_input..], 4, true) else {
return ptr::null_mut();
};
unsafe {
(*tm).tm_year = (val as c_int) - 1900;
@@ -215,9 +205,8 @@ pub unsafe extern "C" fn strptime(
// Hour (00..23): %H
'H' => {
let (val, len) = match parse_int(&input_str[index_in_input..], 2, false) {
Some(v) => v,
None => return ptr::null_mut(),
let Some((val, len)) = parse_int(&input_str[index_in_input..], 2, false) else {
return ptr::null_mut();
};
if val > 23 {
return ptr::null_mut();
@@ -230,9 +219,8 @@ pub unsafe extern "C" fn strptime(
// Hour (01..12): %I
'I' => {
let (val, len) = match parse_int(&input_str[index_in_input..], 2, false) {
Some(v) => v,
None => return ptr::null_mut(),
let Some((val, len)) = parse_int(&input_str[index_in_input..], 2, false) else {
return ptr::null_mut();
};
if !(1..=12).contains(&val) {
return ptr::null_mut();
@@ -246,9 +234,8 @@ pub unsafe extern "C" fn strptime(
// Minute (00..59): %M
'M' => {
let (val, len) = match parse_int(&input_str[index_in_input..], 2, false) {
Some(v) => v,
None => return ptr::null_mut(),
let Some((val, len)) = parse_int(&input_str[index_in_input..], 2, false) else {
return ptr::null_mut();
};
if val > 59 {
return ptr::null_mut();
@@ -261,9 +248,8 @@ pub unsafe extern "C" fn strptime(
// Seconds (00..60): %S
'S' => {
let (val, len) = match parse_int(&input_str[index_in_input..], 2, false) {
Some(v) => v,
None => return ptr::null_mut(),
let Some((val, len)) = parse_int(&input_str[index_in_input..], 2, false) else {
return ptr::null_mut();
};
if val > 60 {
return ptr::null_mut();
@@ -364,9 +350,8 @@ pub unsafe extern "C" fn strptime(
// Day of year: %j
'j' => {
// parse 3-digit day of year [001..366]
let (val, len) = match parse_int(&input_str[index_in_input..], 3, false) {
Some(v) => v,
None => return ptr::null_mut(),
let Some((val, len)) = parse_int(&input_str[index_in_input..], 3, false) else {
return ptr::null_mut();
};
if !(1..=366).contains(&val) {
return ptr::null_mut();
@@ -384,31 +369,31 @@ pub unsafe extern "C" fn strptime(
// We can do a mini strptime recursion or manually parse
// For simplicity, we'll do it inline here
let subfmt = "%m/%d/%y";
let used =
match unsafe { apply_subformat(&input_str[index_in_input..], subfmt, tm) } {
Some(v) => v,
None => return ptr::null_mut(),
};
let Some(used) =
(unsafe { apply_subformat(&input_str[index_in_input..], subfmt, tm) })
else {
return ptr::null_mut();
};
index_in_input += used;
}
'F' => {
// Equivalent to "%Y-%m-%d"
let subfmt = "%Y-%m-%d";
let used =
match unsafe { apply_subformat(&input_str[index_in_input..], subfmt, tm) } {
Some(v) => v,
None => return ptr::null_mut(),
};
let Some(used) =
(unsafe { apply_subformat(&input_str[index_in_input..], subfmt, tm) })
else {
return ptr::null_mut();
};
index_in_input += used;
}
'T' => {
// Equivalent to %H:%M:%S
let subfmt = "%H:%M:%S";
let used =
match unsafe { apply_subformat(&input_str[index_in_input..], subfmt, tm) } {
Some(v) => v,
None => return ptr::null_mut(),
};
let Some(used) =
(unsafe { apply_subformat(&input_str[index_in_input..], subfmt, tm) })
else {
return ptr::null_mut();
};
index_in_input += used;
}
+2 -3
View File
@@ -674,9 +674,8 @@ impl<W: Write> Write for LineWriter<W> {
// Find the last newline character in the buffer provided. If found then
// we're going to write all the data up to that point and then flush,
// otherwise we just write the whole block to the underlying writer.
let i = match memchr::memrchr(b'\n', buf) {
Some(i) => i,
None => return self.inner.write(buf),
let Some(i) = memchr::memrchr(b'\n', buf) else {
return self.inner.write(buf);
};
// Ok, we're going to write a partial amount of the data given first
+3 -6
View File
@@ -422,12 +422,9 @@ fn stage2(
crate::platform::init_inner(auxv);
}
let (path, _name) = match resolve_path_name(&name_or_path, &envs) {
Some((p, n)) => (p, n),
None => {
eprintln!("[ld.so]: failed to locate '{name_or_path}'");
unistd::_exit(1);
}
let Some((path, _name)) = resolve_path_name(&name_or_path, &envs) else {
eprintln!("[ld.so]: failed to locate '{name_or_path}'");
unistd::_exit(1);
};
let config = Config::from_env(&envs);
+3 -6
View File
@@ -280,12 +280,9 @@ macro_rules! strto_impl {
// check for error parsing octal/hex prefix
// also check to ensure a number was indeed parsed
let (num, i, overflow) = match res {
Some(res) => res,
None => {
invalid_input();
return 0;
}
let Some((num, i, overflow)) = res else {
invalid_input();
return 0;
};
idx += i;