From 3ebb43463f5a1d403d09969f6b7ad12862462c0c Mon Sep 17 00:00:00 2001 From: easonysliu Date: Sun, 15 Mar 2026 20:27:33 +0800 Subject: [PATCH] fix: replace unsafe unwrap() calls with expect() for better error handling - Replace unwrap() with expect() in validation.rs for binary path resolution - Replace unwrap() with expect() in uudoc.rs for command line argument parsing - Replace unwrap() with expect() in build.rs for locale processing tests This improves error messages and makes failures more descriptive when these operations fail unexpectedly. --- src/bin/uudoc.rs | 14 +++++++------- src/common/validation.rs | 2 +- src/uucore/build.rs | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/bin/uudoc.rs b/src/bin/uudoc.rs index 76f04774ec7..aed7e19bc14 100644 --- a/src/bin/uudoc.rs +++ b/src/bin/uudoc.rs @@ -78,12 +78,12 @@ fn gen_manpage( ) .get_matches_from(std::iter::once(OsString::from("manpage")).chain(args)); - let utility = matches.get_one::("utility").unwrap(); + let utility = matches.get_one::("utility").expect("utility argument should be present"); let command = if utility == "coreutils" { gen_coreutils_app(util_map) } else { validation::setup_localization_or_exit(utility); - let mut cmd = util_map.get(utility).unwrap().1(); + let mut cmd = util_map.get(utility).expect("utility should exist in utility map").1(); cmd.set_bin_name(utility.clone()); let mut cmd = cmd.display_name(utility); if let Some(zip) = tldr { @@ -97,7 +97,7 @@ fn gen_manpage( let man = Man::new(command); man.render(&mut io::stdout()) .expect("Man page generation failed"); - io::stdout().flush().unwrap(); + io::stdout().flush().expect("stdout flush should succeed"); process::exit(0); } @@ -119,19 +119,19 @@ fn gen_completions(args: impl Iterator, util_map: &Uti ) .get_matches_from(std::iter::once(OsString::from("completion")).chain(args)); - let utility = matches.get_one::("utility").unwrap(); - let shell = *matches.get_one::("shell").unwrap(); + let utility = matches.get_one::("utility").expect("utility argument should be present"); + let shell = *matches.get_one::("shell").expect("shell argument should be present"); let mut command = if utility == "coreutils" { gen_coreutils_app(util_map) } else { validation::setup_localization_or_exit(utility); - util_map.get(utility).unwrap().1() + util_map.get(utility).expect("utility should exist in utility map").1() }; let bin_name = std::env::var("PROG_PREFIX").unwrap_or_default() + utility; clap_complete::generate(shell, &mut command, bin_name, &mut io::stdout()); - io::stdout().flush().unwrap(); + io::stdout().flush().expect("stdout flush should succeed"); process::exit(0); } diff --git a/src/common/validation.rs b/src/common/validation.rs index a0a13b5df9a..d1e2c69e128 100644 --- a/src/common/validation.rs +++ b/src/common/validation.rs @@ -78,7 +78,7 @@ fn get_canonical_util_name(util_name: &str) -> &str { pub fn binary_path(args: &mut impl Iterator) -> PathBuf { match args.next() { Some(ref s) if !s.is_empty() => PathBuf::from(s), - _ => std::env::current_exe().unwrap(), + _ => std::env::current_exe().expect("current executable path should be accessible"), } } diff --git a/src/uucore/build.rs b/src/uucore/build.rs index 15068f28aab..6d8e690c6ca 100644 --- a/src/uucore/build.rs +++ b/src/uucore/build.rs @@ -502,7 +502,7 @@ mod tests { collected.push(locale.to_string()); Ok(()) }) - .unwrap(); + .expect("locale processing should succeed"); assert_eq!(collected, vec!["en-US", "fr-FR"]); } @@ -516,7 +516,7 @@ mod tests { collected.push(locale.to_string()); Ok(()) }) - .unwrap(); + .expect("locale processing should succeed"); assert_eq!(collected, vec!["en-US"]); }