Skip to content

Commit b13ce98

Browse files
authored
Merge pull request #7699 from nyurik/returns
chore: simplify `return` in multi-branch
2 parents 4bc5185 + b5ed4a4 commit b13ce98

File tree

12 files changed

+61
-66
lines changed

12 files changed

+61
-66
lines changed

src/uu/chmod/src/chmod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,24 +357,24 @@ impl Chmoder {
357357
Ok(meta) => meta.mode() & 0o7777,
358358
Err(err) => {
359359
// Handle dangling symlinks or other errors
360-
if file.is_symlink() && !self.dereference {
360+
return if file.is_symlink() && !self.dereference {
361361
if self.verbose {
362362
println!(
363363
"neither symbolic link {} nor referent has been changed",
364364
file.quote()
365365
);
366366
}
367-
return Ok(()); // Skip dangling symlinks
367+
Ok(()) // Skip dangling symlinks
368368
} else if err.kind() == std::io::ErrorKind::PermissionDenied {
369369
// These two filenames would normally be conditionally
370370
// quoted, but GNU's tests expect them to always be quoted
371-
return Err(USimpleError::new(
371+
Err(USimpleError::new(
372372
1,
373373
format!("{}: Permission denied", file.quote()),
374-
));
374+
))
375375
} else {
376-
return Err(USimpleError::new(1, format!("{}: {err}", file.quote())));
377-
}
376+
Err(USimpleError::new(1, format!("{}: {err}", file.quote())))
377+
};
378378
}
379379
};
380380

src/uu/cp/src/copydir.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,15 @@ fn copy_direntry(
251251
&& !ends_with_slash_dot(&source_absolute)
252252
&& !local_to_target.exists()
253253
{
254-
if target_is_file {
255-
return Err("cannot overwrite non-directory with directory".into());
254+
return if target_is_file {
255+
Err("cannot overwrite non-directory with directory".into())
256256
} else {
257257
build_dir(&local_to_target, false, options, Some(&source_absolute))?;
258258
if options.verbose {
259259
println!("{}", context_for(&source_relative, &local_to_target));
260260
}
261-
return Ok(());
262-
}
261+
Ok(())
262+
};
263263
}
264264

265265
// If the source is not a directory, then we need to copy the file.

src/uu/cp/src/cp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2451,10 +2451,10 @@ fn handle_no_preserve_mode(options: &Options, org_mode: u32) -> u32 {
24512451
{
24522452
const MODE_RW_UGO: u32 = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
24532453
const S_IRWXUGO: u32 = S_IRWXU | S_IRWXG | S_IRWXO;
2454-
if is_explicit_no_preserve_mode {
2455-
return MODE_RW_UGO;
2454+
return if is_explicit_no_preserve_mode {
2455+
MODE_RW_UGO
24562456
} else {
2457-
return org_mode & S_IRWXUGO;
2457+
org_mode & S_IRWXUGO
24582458
};
24592459
}
24602460

src/uu/df/src/blocks.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,7 @@ pub(crate) fn read_block_size(matches: &ArgMatches) -> Result<BlockSize, ParseSi
184184
fn block_size_from_env() -> Option<u64> {
185185
for env_var in ["DF_BLOCK_SIZE", "BLOCK_SIZE", "BLOCKSIZE"] {
186186
if let Ok(env_size) = env::var(env_var) {
187-
if let Ok(size) = parse_size_u64(&env_size) {
188-
return Some(size);
189-
} else {
190-
return None;
191-
}
187+
return parse_size_u64(&env_size).ok();
192188
}
193189
}
194190

src/uu/env/src/env.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -581,19 +581,21 @@ impl EnvAppData {
581581
}
582582
return Err(exit.code().unwrap().into());
583583
}
584-
Err(ref err) => match err.kind() {
585-
io::ErrorKind::NotFound | io::ErrorKind::InvalidInput => {
586-
return Err(self.make_error_no_such_file_or_dir(prog.deref()));
587-
}
588-
io::ErrorKind::PermissionDenied => {
589-
uucore::show_error!("{}: Permission denied", prog.quote());
590-
return Err(126.into());
591-
}
592-
_ => {
593-
uucore::show_error!("unknown error: {err:?}");
594-
return Err(126.into());
595-
}
596-
},
584+
Err(ref err) => {
585+
return match err.kind() {
586+
io::ErrorKind::NotFound | io::ErrorKind::InvalidInput => {
587+
Err(self.make_error_no_such_file_or_dir(prog.deref()))
588+
}
589+
io::ErrorKind::PermissionDenied => {
590+
uucore::show_error!("{}: Permission denied", prog.quote());
591+
Err(126.into())
592+
}
593+
_ => {
594+
uucore::show_error!("unknown error: {err:?}");
595+
Err(126.into())
596+
}
597+
};
598+
}
597599
Ok(_) => (),
598600
}
599601
Ok(())

src/uu/expand/src/expand.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,14 @@ fn tabstops_parse(s: &str) -> Result<(RemainingMode, Vec<usize>), ParseError> {
145145
}
146146

147147
let s = s.trim_start_matches(char::is_numeric);
148-
if s.starts_with('/') || s.starts_with('+') {
149-
return Err(ParseError::SpecifierNotAtStartOfNumber(
148+
return if s.starts_with('/') || s.starts_with('+') {
149+
Err(ParseError::SpecifierNotAtStartOfNumber(
150150
s[0..1].to_string(),
151151
s.to_string(),
152-
));
152+
))
153153
} else {
154-
return Err(ParseError::InvalidCharacter(s.to_string()));
155-
}
154+
Err(ParseError::InvalidCharacter(s.to_string()))
155+
};
156156
}
157157
}
158158
}

src/uu/fmt/src/fmt.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,14 @@ fn extract_files(matches: &ArgMatches) -> UResult<Vec<String>> {
270270
fn extract_width(matches: &ArgMatches) -> UResult<Option<usize>> {
271271
let width_opt = matches.get_one::<String>(options::WIDTH);
272272
if let Some(width_str) = width_opt {
273-
if let Ok(width) = width_str.parse::<usize>() {
274-
return Ok(Some(width));
273+
return if let Ok(width) = width_str.parse::<usize>() {
274+
Ok(Some(width))
275275
} else {
276-
return Err(USimpleError::new(
276+
Err(USimpleError::new(
277277
1,
278278
format!("invalid width: {}", width_str.quote()),
279-
));
280-
}
279+
))
280+
};
281281
}
282282

283283
if let Some(1) = matches.index_of(options::FILES_OR_WIDTH) {

src/uu/id/src/id.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
176176
let line_ending = LineEnding::from_zero_flag(state.zflag);
177177

178178
if state.cflag {
179-
if state.selinux_supported {
179+
return if state.selinux_supported {
180180
// print SElinux context and exit
181181
#[cfg(all(any(target_os = "linux", target_os = "android"), feature = "selinux"))]
182182
if let Ok(context) = selinux::SecurityContext::current(false) {
@@ -186,13 +186,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
186186
// print error because `cflag` was explicitly requested
187187
return Err(USimpleError::new(1, "can't get process context"));
188188
}
189-
return Ok(());
189+
Ok(())
190190
} else {
191-
return Err(USimpleError::new(
191+
Err(USimpleError::new(
192192
1,
193193
"--context (-Z) works only on an SELinux-enabled kernel",
194-
));
195-
}
194+
))
195+
};
196196
}
197197

198198
for i in 0..=users.len() {

src/uu/readlink/src/readlink.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
8484
show(&path, line_ending).map_err_context(String::new)?;
8585
}
8686
Err(err) => {
87-
if verbose {
88-
return Err(USimpleError::new(
87+
return if verbose {
88+
Err(USimpleError::new(
8989
1,
9090
err.map_err_context(move || f.maybe_quote().to_string())
9191
.to_string(),
92-
));
92+
))
9393
} else {
94-
return Err(1.into());
95-
}
94+
Err(1.into())
95+
};
9696
}
9797
}
9898
}

src/uu/tail/src/follow/files.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,13 @@ impl FileHandling {
162162
pub fn needs_header(&self, path: &Path, verbose: bool) -> bool {
163163
if verbose {
164164
if let Some(ref last) = self.last {
165-
return !last.eq(&path);
165+
!last.eq(&path)
166166
} else {
167-
return true;
167+
true
168168
}
169+
} else {
170+
false
169171
}
170-
false
171172
}
172173
}
173174

0 commit comments

Comments
 (0)