diff --git a/Cargo.toml b/Cargo.toml index 9c153196b97..c2023cea605 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -720,7 +720,6 @@ unexpected_cfgs = { level = "warn", check-cfg = [ unused_qualifications = "warn" [workspace.lints.clippy] -collapsible_if = { level = "allow", priority = 127 } # remove me # The counts were generated with this command: # cargo clippy --all-targets --workspace --message-format=json --quiet \ # | jq -r '.message.code.code | select(. != null and startswith("clippy::"))' \ diff --git a/src/uucore/build.rs b/src/uucore/build.rs index f3b4df331a7..a4a5a551a4f 100644 --- a/src/uucore/build.rs +++ b/src/uucore/build.rs @@ -84,18 +84,18 @@ fn detect_target_utility() -> Option { println!("cargo:rerun-if-env-changed=UUCORE_TARGET_UTIL"); // First check if an explicit environment variable was set - if let Ok(target_util) = env::var("UUCORE_TARGET_UTIL") { - if !target_util.is_empty() { - return Some(target_util); - } + if let Ok(target_util) = env::var("UUCORE_TARGET_UTIL") + && !target_util.is_empty() + { + return Some(target_util); } // Auto-detect utility name from CARGO_PKG_NAME if it's a uu_* package - if let Ok(pkg_name) = env::var("CARGO_PKG_NAME") { - if let Some(util_name) = pkg_name.strip_prefix("uu_") { - println!("cargo:warning=Auto-detected utility name: {util_name}"); - return Some(util_name.to_string()); - } + if let Ok(pkg_name) = env::var("CARGO_PKG_NAME") + && let Some(util_name) = pkg_name.strip_prefix("uu_") + { + println!("cargo:warning=Auto-detected utility name: {util_name}"); + return Some(util_name.to_string()); } // Check for a build configuration file in the target directory @@ -186,10 +186,10 @@ fn embed_all_utility_locales( let mut util_dirs = Vec::new(); for entry in fs::read_dir(&src_uu_dir)? { let entry = entry?; - if entry.file_type()?.is_dir() { - if let Some(dir_name) = entry.file_name().to_str() { - util_dirs.push(dir_name.to_string()); - } + if entry.file_type()?.is_dir() + && let Some(dir_name) = entry.file_name().to_str() + { + util_dirs.push(dir_name.to_string()); } } util_dirs.sort(); @@ -247,18 +247,14 @@ fn embed_static_utility_locales( for entry in entries { let file_name = entry.file_name(); - if let Some(dir_name) = file_name.to_str() { - // Match uu_- - if let Some((util_part, _)) = dir_name.split_once('-') { - if let Some(util_name) = util_part.strip_prefix("uu_") { - embed_component_locales( - embedded_file, - locales_to_embed, - util_name, - |locale| entry.path().join(format!("locales/{locale}.ftl")), - )?; - } - } + // Match uu_- + if let Some(dir_name) = file_name.to_str() + && let Some((util_part, _)) = dir_name.split_once('-') + && let Some(util_name) = util_part.strip_prefix("uu_") + { + embed_component_locales(embedded_file, locales_to_embed, util_name, |locale| { + entry.path().join(format!("locales/{locale}.ftl")) + })?; } } @@ -366,25 +362,26 @@ where F: Fn(&str) -> PathBuf, { let en_path = path_builder("en-US"); - if let Some(locale_dir) = en_path.parent() { - if locale_dir.exists() { - for entry in std::fs::read_dir(locale_dir)? { - let entry = entry?; - let path = entry.path(); - if path.extension().is_some_and(|e| e == "ftl") { - if let Some(locale) = path.file_stem().and_then(|s| s.to_str()) { - embed_locale_file( - embedded_file, - &path, - &format!("{component_name}/{locale}.ftl"), - locale, - component_name, - )?; - } - } + if let Some(locale_dir) = en_path.parent() + && locale_dir.exists() + { + for entry in std::fs::read_dir(locale_dir)? { + let entry = entry?; + let path = entry.path(); + if path.extension().is_some_and(|e| e == "ftl") + && let Some(locale) = path.file_stem().and_then(|s| s.to_str()) + { + embed_locale_file( + embedded_file, + &path, + &format!("{component_name}/{locale}.ftl"), + locale, + component_name, + )?; } } } + Ok(()) } diff --git a/src/uucore/src/lib/mods/clap_localization.rs b/src/uucore/src/lib/mods/clap_localization.rs index fc4f838c216..926f8d15fde 100644 --- a/src/uucore/src/lib/mods/clap_localization.rs +++ b/src/uucore/src/lib/mods/clap_localization.rs @@ -219,17 +219,17 @@ impl<'a> ErrorFormatter<'a> { } // Show possible values for InvalidValue errors - if matches!(err.kind(), ErrorKind::InvalidValue) { - if let Some(valid_values) = err.get(ContextKind::ValidValue) { - if !valid_values.to_string().is_empty() { - let _ = writeln!( - stderr(), - "\n [{}: {valid_values}]", - translate!("clap-error-possible-values") - ); - } - } + if matches!(err.kind(), ErrorKind::InvalidValue) + && let Some(valid_values) = err.get(ContextKind::ValidValue) + && !valid_values.to_string().is_empty() + { + let _ = writeln!( + stderr(), + "\n [{}: {valid_values}]", + translate!("clap-error-possible-values") + ); } + let _ = writeln!(stderr(), "\n{}", translate!("common-help-suggestion")); } else { self.print_simple_error_msg(&err.render().to_string()); diff --git a/src/uucore/src/lib/mods/locale.rs b/src/uucore/src/lib/mods/locale.rs index f25866bc144..36f2d2368c9 100644 --- a/src/uucore/src/lib/mods/locale.rs +++ b/src/uucore/src/lib/mods/locale.rs @@ -93,13 +93,13 @@ impl Localizer { } // Fall back to English bundle if available - if let Some(ref fallback) = self.fallback_bundle { - if let Some(message) = fallback.get_message(id).and_then(|m| m.value()) { - let mut errs = Vec::new(); - return fallback - .format_pattern(message, args, &mut errs) - .to_string(); - } + if let Some(ref fallback) = self.fallback_bundle + && let Some(message) = fallback.get_message(id).and_then(|m| m.value()) + { + let mut errs = Vec::new(); + return fallback + .format_pattern(message, args, &mut errs) + .to_string(); } // Return the key ID if not found anywhere @@ -230,10 +230,9 @@ fn parse_fluent_resource( cache: &'static OnceLock, ) -> Result<&'static FluentResource, LocalizationError> { // global cache breaks unit tests - if cfg!(not(test)) { - if let Some(res) = cache.get() { - return Ok(res); - } + #[cfgnot(test)] + if let Some(res) = cache.get() { + return Ok(res); } let resource = FluentResource::try_new(content.to_string()).map_err( @@ -284,11 +283,12 @@ fn create_english_bundle_from_embedded( } // Checksum algorithms need locale messages from checksum_common - if util_name.ends_with("sum") { - if let Some(uucore_content) = get_embedded_locale("checksum_common/en-US.ftl") { - let uucore_resource = parse_fluent_resource(uucore_content, &CHECKSUM_FLUENT)?; - bundle.add_resource_overriding(uucore_resource); - } + // todo: sum is not checksum_common family. Should we avoid the case? + if util_name.ends_with("sum") + && let Some(uucore_content) = get_embedded_locale("checksum_common/en-US.ftl") + { + let uucore_resource = parse_fluent_resource(uucore_content, &CHECKSUM_FLUENT)?; + bundle.add_resource_overriding(uucore_resource); } // Then, try to load utility-specific strings diff --git a/src/uucore/src/lib/mods/os.rs b/src/uucore/src/lib/mods/os.rs index fca4578ddd6..1d9d6484a8c 100644 --- a/src/uucore/src/lib/mods/os.rs +++ b/src/uucore/src/lib/mods/os.rs @@ -14,11 +14,11 @@ pub fn is_wsl_1() -> bool { if is_wsl_2() { return false; } - if let Ok(b) = std::fs::read("/proc/sys/kernel/osrelease") { - if let Ok(s) = std::str::from_utf8(&b) { - let a = s.to_ascii_lowercase(); - return a.contains("microsoft") || a.contains("wsl"); - } + if let Ok(b) = std::fs::read("/proc/sys/kernel/osrelease") + && let Ok(s) = std::str::from_utf8(&b) + { + let a = s.to_ascii_lowercase(); + return a.contains("microsoft") || a.contains("wsl"); } } false @@ -28,11 +28,11 @@ pub fn is_wsl_1() -> bool { pub fn is_wsl_2() -> bool { #[cfg(target_os = "linux")] { - if let Ok(b) = std::fs::read("/proc/sys/kernel/osrelease") { - if let Ok(s) = std::str::from_utf8(&b) { - let a = s.to_ascii_lowercase(); - return a.contains("wsl2"); - } + if let Ok(b) = std::fs::read("/proc/sys/kernel/osrelease") + && let Ok(s) = std::str::from_utf8(&b) + { + let a = s.to_ascii_lowercase(); + return a.contains("wsl2"); } } false diff --git a/src/uucore/src/lib/mods/panic.rs b/src/uucore/src/lib/mods/panic.rs index 2a67a10ba8d..ce69ab40eb5 100644 --- a/src/uucore/src/lib/mods/panic.rs +++ b/src/uucore/src/lib/mods/panic.rs @@ -18,9 +18,7 @@ use std::panic::{self, PanicHookInfo}; /// Decide whether a panic was caused by a broken pipe (SIGPIPE) error. fn is_broken_pipe(info: &PanicHookInfo) -> bool { if let Some(res) = info.payload().downcast_ref::() { - if res.contains("BrokenPipe") || res.contains("Broken pipe") { - return true; - } + return res.contains("BrokenPipe") || res.contains("Broken pipe"); } false } @@ -55,14 +53,12 @@ pub fn preserve_inherited_sigpipe() { use nix::libc; // Check if parent specified that SIGPIPE should be default - if let Ok(val) = std::env::var("RUST_SIGPIPE") { - if val == "default" { - unsafe { - libc::signal(libc::SIGPIPE, libc::SIG_DFL); - // Remove the environment variable so child processes don't inherit it incorrectly - std::env::remove_var("RUST_SIGPIPE"); - } - } + if let Ok(val) = std::env::var("RUST_SIGPIPE") + && val == "default" + { + unsafe { libc::signal(libc::SIGPIPE, libc::SIG_DFL) }; + // Remove the environment variable so child processes don't inherit it incorrectly + unsafe { std::env::remove_var("RUST_SIGPIPE") }; } }