Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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::"))' \
Expand Down
79 changes: 38 additions & 41 deletions src/uucore/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,18 @@
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")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function could be turned into an Option pipeline, chaining with or_else.

&& !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
Expand Down Expand Up @@ -186,10 +186,10 @@
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();
Expand Down Expand Up @@ -247,18 +247,14 @@

for entry in entries {
let file_name = entry.file_name();
if let Some(dir_name) = file_name.to_str() {
// Match uu_<util>-<version>
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_<util>-<version>
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"))
})?;
}
}

Expand Down Expand Up @@ -350,7 +346,7 @@
/// Check if we are cross-compiling for WASI (build.rs runs on the host,
/// so `#[cfg(target_os = "wasi")]` does not work here).
fn is_wasi_target() -> bool {
env::var("CARGO_CFG_TARGET_OS")

Check failure on line 349 in src/uucore/build.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-24.04, unix)

ERROR: `cargo clippy`: called `map(<f>).unwrap_or(false)` on a `Result` value (file:'src/uucore/build.rs', line:349)
.map(|os| os == "wasi")
.unwrap_or(false)
}
Expand All @@ -366,25 +362,26 @@
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(())
}

Expand Down
20 changes: 10 additions & 10 deletions src/uucore/src/lib/mods/clap_localization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
32 changes: 16 additions & 16 deletions src/uucore/src/lib/mods/locale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@
}

// 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
Expand Down Expand Up @@ -230,10 +230,9 @@
cache: &'static OnceLock<FluentResource>,
) -> Result<&'static FluentResource, LocalizationError> {
// global cache breaks unit tests
if cfg!(not(test)) {
if let Some(res) = cache.get() {
return Ok(res);
}
#[cfgnot(test)]

Check failure on line 233 in src/uucore/src/lib/mods/locale.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (unix)

ERROR: `cargo clippy`: cannot find attribute `cfgnot` in this scope (file:'src/uucore/src/lib/mods/locale.rs', line:233)
if let Some(res) = cache.get() {
return Ok(res);
}

let resource = FluentResource::try_new(content.to_string()).map_err(
Expand Down Expand Up @@ -284,11 +283,12 @@
}

// 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
Expand Down
20 changes: 10 additions & 10 deletions src/uucore/src/lib/mods/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
18 changes: 7 additions & 11 deletions src/uucore/src/lib/mods/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>() {
if res.contains("BrokenPipe") || res.contains("Broken pipe") {
return true;
}
return res.contains("BrokenPipe") || res.contains("Broken pipe");
}
false
}
Expand Down Expand Up @@ -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") };
}
}

Expand Down
Loading