diff --git a/src/uu/chgrp/src/chgrp.rs b/src/uu/chgrp/src/chgrp.rs index b09fd929658..42060237596 100644 --- a/src/uu/chgrp/src/chgrp.rs +++ b/src/uu/chgrp/src/chgrp.rs @@ -57,10 +57,7 @@ fn get_dest_gid(matches: &ArgMatches) -> UResult<(Option, String)> { if group.is_empty() { None } else { - match parse_gid_from_str(group) { - Ok(g) => Some(g), - Err(e) => return Err(USimpleError::new(1, e)), - } + Some(parse_gid_from_str(group).map_err(|e| USimpleError::new(1, e))?) } }; Ok((dest_gid, raw_group)) @@ -69,15 +66,14 @@ fn get_dest_gid(matches: &ArgMatches) -> UResult<(Option, String)> { fn parse_gid_and_uid(matches: &ArgMatches) -> UResult { // Handle --from option let filter = if let Some(from_group) = matches.get_one::(options::FROM) { - match parse_gid_from_str(from_group) { - Ok(g) => IfFrom::Group(g), - Err(_) => { - return Err(USimpleError::new( + parse_gid_from_str(from_group) + .map(IfFrom::Group) + .map_err(|_| { + USimpleError::new( 1, translate!("chgrp-error-invalid-user", "from_group" => from_group), - )); - } - } + ) + })? } else { IfFrom::All }; diff --git a/src/uu/expand/src/expand.rs b/src/uu/expand/src/expand.rs index 007156ab2e8..b328afa0133 100644 --- a/src/uu/expand/src/expand.rs +++ b/src/uu/expand/src/expand.rs @@ -104,16 +104,11 @@ fn tabstops_parse(s: &str) -> Result<(RemainingMode, Vec), ParseError> { // Parse a number from the byte sequence. let s = from_utf8(&bytes[i..]).unwrap(); match s.parse::() { + // Tab size must be positive. + Ok(0) => return Err(ParseError::TabSizeCannotBeZero), Ok(num) => { - // Tab size must be positive. - if num == 0 { - return Err(ParseError::TabSizeCannotBeZero); - } - // Tab sizes must be ascending. - if let Some(last_stop) = nums.last() - && *last_stop >= num - { + if nums.last().is_some_and(|last| *last >= num) { return Err(ParseError::TabSizesMustBeAscending); } diff --git a/src/uu/fmt/src/fmt.rs b/src/uu/fmt/src/fmt.rs index 06cac3cb3ea..8de6eb0349c 100644 --- a/src/uu/fmt/src/fmt.rs +++ b/src/uu/fmt/src/fmt.rs @@ -119,12 +119,11 @@ impl FmtOptions { let width_opt = extract_width(matches)?; let goal_opt_str = matches.get_one::(options::GOAL); let goal_opt = if let Some(goal_str) = goal_opt_str { - match goal_str.parse::() { - Ok(goal) => Some(goal), - Err(_) => { - return Err(FmtError::InvalidGoal(goal_str.clone()).into()); - } - } + Some( + goal_str + .parse::() + .map_err(|_| FmtError::InvalidGoal(goal_str.clone()))?, + ) } else { None }; @@ -167,12 +166,9 @@ impl FmtOptions { let mut tabwidth = 8; if let Some(s) = matches.get_one::(options::TAB_WIDTH) { - tabwidth = match s.parse::() { - Ok(t) => t, - Err(_) => { - return Err(FmtError::InvalidTabWidth(s.clone()).into()); - } - }; + tabwidth = s + .parse::() + .map_err(|_| FmtError::InvalidTabWidth(s.clone()))?; } if tabwidth < 1 { diff --git a/src/uu/id/src/id.rs b/src/uu/id/src/id.rs index 5b54c4313cd..e838d8a84ba 100644 --- a/src/uu/id/src/id.rs +++ b/src/uu/id/src/id.rs @@ -187,15 +187,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { // SELinux context #[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] if state.selinux_supported { - if let Ok(context) = selinux::SecurityContext::current(false) { - let bytes = context.as_bytes(); - write!(lock, "{}{line_ending}", String::from_utf8_lossy(bytes))?; - return Ok(()); - } - return Err(USimpleError::new( - 1, - translate!("id-error-cannot-get-context"), - )); + let context = selinux::SecurityContext::current(false) + .map_err(|_| USimpleError::new(1, translate!("id-error-cannot-get-context")))?; + let bytes = context.as_bytes(); + write!(lock, "{}{line_ending}", String::from_utf8_lossy(bytes))?; + return Ok(()); } // SMACK label diff --git a/src/uu/ls/src/config.rs b/src/uu/ls/src/config.rs index 00e6eb766be..ca08acbf3c6 100644 --- a/src/uu/ls/src/config.rs +++ b/src/uu/ls/src/config.rs @@ -760,13 +760,11 @@ impl Config { (DEFAULT_FILE_SIZE_BLOCK_SIZE, 1000) } else if opt_hr { (DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE) - } else if let Ok(size) = parse_size_non_zero_u64(opt_block_size) { + } else { + let size = parse_size_non_zero_u64(opt_block_size) + .map_err(|_| LsError::BlockSizeParseError(opt_block_size.clone()))?; // --block-size overrides -k (size, size) - } else { - return Err(Box::new(LsError::BlockSizeParseError( - opt_block_size.clone(), - ))); } } else if !opt_si && !opt_hr { resolve_block_sizes_from_env(opt_kb) diff --git a/src/uu/numfmt/src/options.rs b/src/uu/numfmt/src/options.rs index ced2d4b6493..6b5b291882d 100644 --- a/src/uu/numfmt/src/options.rs +++ b/src/uu/numfmt/src/options.rs @@ -190,13 +190,10 @@ impl FromStr for FormatOptions { } if !padding.is_empty() { - if let Ok(p) = padding.parse() { - options.padding = Some(p); - } else { - return Err( - translate!("numfmt-error-invalid-format-width-overflow", "format" => s), - ); - } + let p = padding.parse().map_err( + |_| translate!("numfmt-error-invalid-format-width-overflow", "format" => s), + )?; + options.padding = Some(p); } if let Some('.') = iter.peek() { @@ -217,10 +214,11 @@ impl FromStr for FormatOptions { if precision.is_empty() { options.precision = Some(0); - } else if let Ok(p) = precision.parse() { - options.precision = Some(p); } else { - return Err(translate!("numfmt-error-invalid-precision", "format" => s)); + let p = precision + .parse() + .map_err(|_| translate!("numfmt-error-invalid-precision", "format" => s))?; + options.precision = Some(p); } } diff --git a/src/uu/od/src/parse_nrofbytes.rs b/src/uu/od/src/parse_nrofbytes.rs index 241e1c6e7ca..697b1f7a1dc 100644 --- a/src/uu/od/src/parse_nrofbytes.rs +++ b/src/uu/od/src/parse_nrofbytes.rs @@ -69,10 +69,8 @@ pub fn parse_number_of_bytes(s: &str) -> Result { _ => {} } - let factor = match u64::from_str_radix(&s[start..len], radix) { - Ok(f) => f, - Err(e) => return Err(ParseSizeError::ParseFailure(e.to_string())), - }; + let factor = u64::from_str_radix(&s[start..len], radix) + .map_err(|e| ParseSizeError::ParseFailure(e.to_string()))?; factor .checked_mul(multiply) .ok_or_else(|| ParseSizeError::SizeTooBig(s.to_string())) diff --git a/src/uu/tac/src/tac.rs b/src/uu/tac/src/tac.rs index 008c2c9f2b5..904832c0192 100644 --- a/src/uu/tac/src/tac.rs +++ b/src/uu/tac/src/tac.rs @@ -407,9 +407,7 @@ fn tac(filenames: &[OsString], before: bool, regex: bool, separator: &OsStr) -> }; // If there is any error in writing the output, terminate immediately. - if let Err(e) = result { - return Err(TacError::WriteError(e).into()); - } + result.map_err(TacError::WriteError)?; } Ok(()) } diff --git a/src/uucore/src/lib/features/checksum/validate.rs b/src/uucore/src/lib/features/checksum/validate.rs index d10d9588e06..2f47a3e43f1 100644 --- a/src/uucore/src/lib/features/checksum/validate.rs +++ b/src/uucore/src/lib/features/checksum/validate.rs @@ -620,10 +620,9 @@ fn identify_algo_name_and_length( ) -> Result<(AlgoKind, Option), LineCheckError> { use AlgoKind as ak; let algo_from_line = line_info.algo_name.clone().unwrap_or_default(); - let Ok(line_algo) = AlgoKind::from_cksum(algo_from_line.to_lowercase()) else { - // Unknown algorithm - return Err(LineCheckError::ImproperlyFormatted); - }; + let line_algo = AlgoKind::from_cksum(algo_from_line.to_lowercase()).map_err(|_| + // Unknown algorithm + LineCheckError::ImproperlyFormatted)?; *last_algo = Some(algo_from_line); // check if we are called with XXXsum (example: md5sum) but we detected a diff --git a/src/uucore/src/lib/features/encoding.rs b/src/uucore/src/lib/features/encoding.rs index db6114d2783..d866a775587 100644 --- a/src/uucore/src/lib/features/encoding.rs +++ b/src/uucore/src/lib/features/encoding.rs @@ -106,10 +106,8 @@ impl SupportsFastDecodeAndEncode for Base64SimdWrapper { Self::decode_with_no_pad }; - if decoder(remaining, output).is_err() { - return Err(USimpleError::new(1, "error: invalid input")); - } - + decoder(remaining, output) + .map_err(|_| USimpleError::new(1, "error: invalid input"))?; break; } } @@ -429,13 +427,8 @@ impl SupportsFastDecodeAndEncode for Z85Wrapper { return Err(USimpleError::new(1, "error: invalid input")); } - let decode_result = match z85::decode(input) { - Ok(ve) => ve, - Err(_de) => { - return Err(USimpleError::new(1, "error: invalid input")); - } - }; - + let decode_result = + z85::decode(input).map_err(|_de| USimpleError::new(1, "error: invalid input"))?; output.extend_from_slice(&decode_result); Ok(())