Skip to content
Open
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
18 changes: 7 additions & 11 deletions src/uu/chgrp/src/chgrp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ fn get_dest_gid(matches: &ArgMatches) -> UResult<(Option<u32>, 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))
Expand All @@ -69,15 +66,14 @@ fn get_dest_gid(matches: &ArgMatches) -> UResult<(Option<u32>, String)> {
fn parse_gid_and_uid(matches: &ArgMatches) -> UResult<GidUidOwnerFilter> {
// Handle --from option
let filter = if let Some(from_group) = matches.get_one::<String>(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),
));
}
}
)
})?
Comment on lines +69 to +76

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.

I prefer let-else over map_err with an intentional ignore:

Suggested change
parse_gid_from_str(from_group)
.map(IfFrom::Group)
.map_err(|_| {
USimpleError::new(
1,
translate!("chgrp-error-invalid-user", "from_group" => from_group),
));
}
}
)
})?
let Ok(g) = parse_gid_from_str(from_group) else {
return Err(USimpleError::new(
1,
translate!("chgrp-error-invalid-user", "from_group" => from_group),
));
};
IfFrom::Group(g)

In this case, the if could also be collapsed into a let-chain.

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.

thiserror could be used for that too, something like #13620

} else {
IfFrom::All
};
Expand Down
11 changes: 3 additions & 8 deletions src/uu/expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,11 @@ fn tabstops_parse(s: &str) -> Result<(RemainingMode, Vec<usize>), ParseError> {
// Parse a number from the byte sequence.
let s = from_utf8(&bytes[i..]).unwrap();
match s.parse::<usize>() {
// 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);
}

Expand Down
20 changes: 8 additions & 12 deletions src/uu/fmt/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,11 @@ impl FmtOptions {
let width_opt = extract_width(matches)?;
let goal_opt_str = matches.get_one::<String>(options::GOAL);
let goal_opt = if let Some(goal_str) = goal_opt_str {
match goal_str.parse::<usize>() {
Ok(goal) => Some(goal),
Err(_) => {
return Err(FmtError::InvalidGoal(goal_str.clone()).into());
}
}
Some(
goal_str
.parse::<usize>()
.map_err(|_| FmtError::InvalidGoal(goal_str.clone()))?,
)
} else {
None
};
Expand Down Expand Up @@ -167,12 +166,9 @@ impl FmtOptions {

let mut tabwidth = 8;
if let Some(s) = matches.get_one::<String>(options::TAB_WIDTH) {
tabwidth = match s.parse::<usize>() {
Ok(t) => t,
Err(_) => {
return Err(FmtError::InvalidTabWidth(s.clone()).into());
}
};
tabwidth = s
.parse::<usize>()
.map_err(|_| FmtError::InvalidTabWidth(s.clone()))?;
}

if tabwidth < 1 {
Expand Down
14 changes: 5 additions & 9 deletions src/uu/id/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions src/uu/ls/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 8 additions & 10 deletions src/uu/numfmt/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/uu/od/src/parse_nrofbytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,8 @@ pub fn parse_number_of_bytes(s: &str) -> Result<u64, ParseSizeError> {
_ => {}
}

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()))
Expand Down
4 changes: 1 addition & 3 deletions src/uu/tac/src/tac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
7 changes: 3 additions & 4 deletions src/uucore/src/lib/features/checksum/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,10 +620,9 @@ fn identify_algo_name_and_length(
) -> Result<(AlgoKind, Option<HashLength>), 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
Expand Down
15 changes: 4 additions & 11 deletions src/uucore/src/lib/features/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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(())
Expand Down
Loading