From 4c435448ec0acc5271255e55dea643589a4379bd Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Tue, 3 Feb 2026 16:29:28 -0500 Subject: [PATCH] chore: apply Clippy suggestions and improve code formatting --- Cargo.toml | 8 ++------ src/uu/df/src/filesystem.rs | 4 ++-- src/uu/id/src/id.rs | 16 +++++++--------- src/uu/ls/src/colors.rs | 2 ++ src/uu/ls/src/ls.rs | 10 +++++----- src/uu/rm/src/rm.rs | 8 ++++---- src/uucore/src/lib/features/format/argument.rs | 2 +- src/uucore/src/lib/features/fsext.rs | 2 +- src/uucore/src/lib/features/hardware.rs | 2 +- src/uucore/src/lib/features/lines.rs | 2 +- .../lib/features/parser/shortcut_value_parser.rs | 3 ++- src/uucore/src/lib/features/proc_info.rs | 7 ++----- src/uucore/src/lib/features/uptime.rs | 5 ++++- tests/by-util/test_comm.rs | 3 ++- 14 files changed, 36 insertions(+), 38 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c358721eb5d..388858fd79e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -678,14 +678,10 @@ should_panic_without_expect = "allow" # 2 doc_markdown = "allow" unused_self = "allow" enum_glob_use = "allow" -unnested_or_patterns = "allow" + +# TODO: uucore/src/lib/features/fsxattr.rs apply_xattrs() - consider using iterator implicit_hasher = "allow" struct_field_names = "allow" -doc_link_with_quotes = "allow" -format_push_string = "allow" -flat_map_option = "allow" -from_iter_instead_of_collect = "allow" -large_types_passed_by_value = "allow" [workspace.metadata.cargo-shear] ignored = ["clap", "fluent", "libstdbuf"] diff --git a/src/uu/df/src/filesystem.rs b/src/uu/df/src/filesystem.rs index 858e86917dc..6e3d67e0200 100644 --- a/src/uu/df/src/filesystem.rs +++ b/src/uu/df/src/filesystem.rs @@ -167,7 +167,7 @@ impl Filesystem { mount_info.mount_dir.clone() }; #[cfg(unix)] - let usage = FsUsage::new(statfs(&stat_path).ok()?); + let usage = FsUsage::new(&statfs(&stat_path).ok()?); #[cfg(windows)] let usage = FsUsage::new(Path::new(&stat_path)).ok()?; Some(Self { @@ -256,7 +256,7 @@ impl Filesystem { dummy: false, }; - let usage = FsUsage::new(stat_result); + let usage = FsUsage::new(&stat_result); Ok(Self { file: Some(file), diff --git a/src/uu/id/src/id.rs b/src/uu/id/src/id.rs index 5bd27bc3e59..3b4226d0095 100644 --- a/src/uu/id/src/id.rs +++ b/src/uu/id/src/id.rs @@ -195,18 +195,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { // SMACK label #[cfg(feature = "smack")] if state.smack_supported { - match uucore::smack::get_smack_label_for_self() { + return match uucore::smack::get_smack_label_for_self() { Ok(label) => { write!(lock, "{label}{line_ending}")?; - return Ok(()); + Ok(()) } - Err(_) => { - return Err(USimpleError::new( - 1, - translate!("id-error-cannot-get-context"), - )); - } - } + Err(_) => Err(USimpleError::new( + 1, + translate!("id-error-cannot-get-context"), + )), + }; } // Neither SELinux nor SMACK supported diff --git a/src/uu/ls/src/colors.rs b/src/uu/ls/src/colors.rs index fa9a8903bb8..f680ed97cdf 100644 --- a/src/uu/ls/src/colors.rs +++ b/src/uu/ls/src/colors.rs @@ -716,6 +716,8 @@ fn parse_funky_string( } } +// current match list is much easier to read than nested or-patterns +#[expect(clippy::unnested_or_patterns)] fn is_valid_ls_colors_prefix(label: [u8; 2]) -> bool { matches!( label, diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index 7365cb4dc83..0cc01e9f3b7 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -3554,7 +3554,7 @@ fn get_security_context<'a>( #[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] if config.selinux_supported { - match selinux::SecurityContext::of_path(path, must_dereference, false) { + return match selinux::SecurityContext::of_path(path, must_dereference, false) { Err(_r) => { // TODO: show the actual reason why it failed show_warning!( @@ -3564,9 +3564,9 @@ fn get_security_context<'a>( "path" => path.quote().to_string() ) ); - return Cow::Borrowed(SUBSTITUTE_STRING); + Cow::Borrowed(SUBSTITUTE_STRING) } - Ok(None) => return Cow::Borrowed(SUBSTITUTE_STRING), + Ok(None) => Cow::Borrowed(SUBSTITUTE_STRING), Ok(Some(context)) => { let context = context.as_bytes(); @@ -3585,9 +3585,9 @@ fn get_security_context<'a>( String::from_utf8_lossy(context).to_string() }); - return Cow::Owned(res); + Cow::Owned(res) } - } + }; } #[cfg(all(feature = "smack", target_os = "linux"))] diff --git a/src/uu/rm/src/rm.rs b/src/uu/rm/src/rm.rs index b1c7d1dedb8..6d38fb3c703 100644 --- a/src/uu/rm/src/rm.rs +++ b/src/uu/rm/src/rm.rs @@ -616,16 +616,16 @@ fn remove_dir_recursive( { if let Some(s) = path.to_str() { if s.len() > 1000 { - match fs::remove_dir_all(path) { - Ok(_) => return false, + return match fs::remove_dir_all(path) { + Ok(_) => false, Err(e) => { let e = e.map_err_context( || translate!("rm-error-cannot-remove", "file" => path.quote()), ); show_error!("{e}"); - return true; + true } - } + }; } } diff --git a/src/uucore/src/lib/features/format/argument.rs b/src/uucore/src/lib/features/format/argument.rs index b4eea031384..8dc578e2948 100644 --- a/src/uucore/src/lib/features/format/argument.rs +++ b/src/uucore/src/lib/features/format/argument.rs @@ -126,7 +126,7 @@ impl<'a> FormatArguments<'a> { return Err(ExtendedParserError::NotNumeric); }; - let (Some((b'"', bytes)) | Some((b'\'', bytes))) = s.split_first() else { + let Some((b'"' | b'\'', bytes)) = s.split_first() else { // This really can't happen, the string we are given must start with '/". debug_assert!(false); return Err(ExtendedParserError::NotNumeric); diff --git a/src/uucore/src/lib/features/fsext.rs b/src/uucore/src/lib/features/fsext.rs index 173fe918fd2..39a6d79acf9 100644 --- a/src/uucore/src/lib/features/fsext.rs +++ b/src/uucore/src/lib/features/fsext.rs @@ -556,7 +556,7 @@ pub struct FsUsage { impl FsUsage { #[cfg(unix)] - pub fn new(statvfs: StatFs) -> Self { + pub fn new(statvfs: &StatFs) -> Self { { #[cfg(all( not(any(target_os = "freebsd", target_os = "openbsd")), diff --git a/src/uucore/src/lib/features/hardware.rs b/src/uucore/src/lib/features/hardware.rs index 474990343cb..9756c81de5f 100644 --- a/src/uucore/src/lib/features/hardware.rs +++ b/src/uucore/src/lib/features/hardware.rs @@ -175,7 +175,7 @@ impl HasHardwareFeatures for CpuFeatures { /// This is used by GNU utilities to allow users to disable hardware acceleration. #[derive(Debug, Clone)] pub struct SimdPolicy { - /// Features disabled via GLIBC_TUNABLES (e.g., ["AVX2", "AVX512F"]) + /// Features disabled via GLIBC_TUNABLES (e.g., `["AVX2", "AVX512F"]`) disabled_by_env: BTreeSet, hardware_features: &'static CpuFeatures, } diff --git a/src/uucore/src/lib/features/lines.rs b/src/uucore/src/lib/features/lines.rs index 3e3c82b3a5c..412cc5ae096 100644 --- a/src/uucore/src/lib/features/lines.rs +++ b/src/uucore/src/lib/features/lines.rs @@ -9,7 +9,7 @@ //! [`BufRead::lines`] method. While the [`BufRead::lines`] method //! yields [`String`] instances that do not include the line ending //! characters (`"\n"` or `"\r\n"`), our functions yield -//! [`Vec`]<['u8']> instances that include the line ending +//! [`Vec`]<[`u8`]> instances that include the line ending //! characters. This is useful if the input data does not end with a //! newline character and you want to preserve the exact form of the //! input data. diff --git a/src/uucore/src/lib/features/parser/shortcut_value_parser.rs b/src/uucore/src/lib/features/parser/shortcut_value_parser.rs index 5800a91f8a2..f994543300a 100644 --- a/src/uucore/src/lib/features/parser/shortcut_value_parser.rs +++ b/src/uucore/src/lib/features/parser/shortcut_value_parser.rs @@ -11,6 +11,7 @@ use clap::{ builder::{PossibleValue, TypedValueParser}, error::{ContextKind, ContextValue, ErrorKind}, }; +use std::fmt::Write as _; /// A parser that accepts shortcuts for values. #[derive(Clone)] @@ -69,7 +70,7 @@ fn add_ambiguous_value_tip( ) { let mut formatted_possible_values = String::new(); for (i, s) in possible_values.iter().enumerate() { - formatted_possible_values.push_str(&format!("'{}'", s.get_name())); + let _ = write!(formatted_possible_values, "'{}'", s.get_name()); if i < possible_values.len() - 2 { formatted_possible_values.push_str(", "); } else if i < possible_values.len() - 1 { diff --git a/src/uucore/src/lib/features/proc_info.rs b/src/uucore/src/lib/features/proc_info.rs index 0d99cb8a8b9..ba49e9fbdc6 100644 --- a/src/uucore/src/lib/features/proc_info.rs +++ b/src/uucore/src/lib/features/proc_info.rs @@ -333,7 +333,7 @@ impl ProcessInformation { .follow_links(false) .into_iter() .flatten() - .flat_map(|it| { + .filter_map(|it| { it.path() .file_name() .and_then(|it| it.to_str()) @@ -470,10 +470,7 @@ mod tests { assert_eq!(pid_entry.tty(), Teletype::Unknown); } else { assert_eq!(result.len(), 1); - assert_eq!( - pid_entry.tty(), - Vec::from_iter(result.into_iter()).first().unwrap().clone() - ); + assert_eq!(pid_entry.tty(), result.into_iter().next().unwrap()); } } diff --git a/src/uucore/src/lib/features/uptime.rs b/src/uucore/src/lib/features/uptime.rs index 2aba7cd27d4..e24d0cd2df7 100644 --- a/src/uucore/src/lib/features/uptime.rs +++ b/src/uucore/src/lib/features/uptime.rs @@ -515,7 +515,10 @@ mod tests { assert!(boot_time > 0, "Boot time should be positive"); // Boot time should be after 2000-01-01 (946684800 seconds since epoch) - assert!(boot_time > 946684800, "Boot time should be after year 2000"); + assert!( + boot_time > 946_684_800, + "Boot time should be after year 2000" + ); // Boot time should be before current time let now = Timestamp::now().as_second(); diff --git a/tests/by-util/test_comm.rs b/tests/by-util/test_comm.rs index e314cfaf1f4..7954c14c8e4 100644 --- a/tests/by-util/test_comm.rs +++ b/tests/by-util/test_comm.rs @@ -676,6 +676,7 @@ fn test_output_lossy_utf8() { #[test] #[cfg(any(target_os = "linux", target_os = "android"))] fn test_comm_anonymous_pipes() { + use std::fmt::Write as _; use std::{io::Write, os::fd::AsRawFd, process}; use uucore::pipes::pipe; @@ -692,7 +693,7 @@ fn test_comm_anonymous_pipes() { // write 1500 lines into comm1: 00000\n00001\n...01500\n let mut content = String::new(); for i in 0..1500 { - content.push_str(&format!("{i:05}\n")); + let _ = writeln!(content, "{i:05}"); } assert!(comm1_writer.write_all(content.as_bytes()).is_ok()); drop(comm1_writer);