1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

fix(clippy): redundant_else

This commit is contained in:
Gabriele Belluardo 2025-06-17 00:24:53 +02:00
parent d64fcb1826
commit 35f3975e17
No known key found for this signature in database
GPG key ID: 21671B8C89CCBF4F
12 changed files with 189 additions and 192 deletions

View file

@ -1150,9 +1150,8 @@ impl Options {
CpError::Error("SELinux was not enabled during the compile time!".to_owned());
if required {
return Err(selinux_disabled_error);
} else {
show_error_if_needed(&selinux_disabled_error);
}
show_error_if_needed(&selinux_disabled_error);
}
// Extract the SELinux related flags and options
@ -1925,10 +1924,9 @@ fn handle_existing_dest(
source.quote()
)
.into());
} else {
is_dest_removed = dest.is_symlink();
backup_dest(dest, &backup_path, is_dest_removed)?;
}
is_dest_removed = dest.is_symlink();
backup_dest(dest, &backup_path, is_dest_removed)?;
}
if !is_dest_removed {
delete_dest_if_needed_and_allowed(
@ -2195,21 +2193,21 @@ fn handle_copy_mode(
let dest_time = dest_metadata.modified()?;
if src_time <= dest_time {
return Ok(PerformedAction::Skipped);
} else {
options.overwrite.verify(dest, options.debug)?;
copy_helper(
source,
dest,
options,
context,
source_is_symlink,
source_is_fifo,
symlinked_files,
#[cfg(unix)]
source_is_stream,
)?;
}
options.overwrite.verify(dest, options.debug)?;
copy_helper(
source,
dest,
options,
context,
source_is_symlink,
source_is_fifo,
symlinked_files,
#[cfg(unix)]
source_is_stream,
)?;
}
}
} else {

View file

@ -429,9 +429,8 @@ fn get_delimiters(matches: &ArgMatches) -> UResult<(Delimiter, Option<&[u8]>)> {
1,
get_message("cut-error-delimiter-must-be-single-character"),
));
} else {
Delimiter::from(os_string)
}
Delimiter::from(os_string)
}
}
None => {

View file

@ -34,9 +34,8 @@ impl Matcher for ExactMatcher<'_> {
|| haystack[match_idx + 1..].starts_with(&self.needle[1..])
{
return Some((match_idx, match_idx + self.needle.len()));
} else {
pos = match_idx + 1;
}
pos = match_idx + 1;
}
None => {
return None;

View file

@ -204,81 +204,81 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
};
return set_system_datetime(date);
} else {
// Get the current time, either in the local time zone or UTC.
let now = if settings.utc {
Timestamp::now().to_zoned(TimeZone::UTC)
} else {
Zoned::now()
};
}
// Iterate over all dates - whether it's a single date or a file.
let dates: Box<dyn Iterator<Item = _>> = match settings.date_source {
DateSource::Custom(ref input) => {
let date = parse_date(input);
let iter = std::iter::once(date);
Box::new(iter)
}
DateSource::Human(relative_time) => {
// Double check the result is overflow or not of the current_time + relative_time
// it may cause a panic of chrono::datetime::DateTime add
match now.checked_add(relative_time) {
Ok(date) => {
let iter = std::iter::once(Ok(date));
Box::new(iter)
}
Err(_) => {
return Err(USimpleError::new(
1,
format!("invalid date {relative_time}"),
));
}
// Get the current time, either in the local time zone or UTC.
let now = if settings.utc {
Timestamp::now().to_zoned(TimeZone::UTC)
} else {
Zoned::now()
};
// Iterate over all dates - whether it's a single date or a file.
let dates: Box<dyn Iterator<Item = _>> = match settings.date_source {
DateSource::Custom(ref input) => {
let date = parse_date(input);
let iter = std::iter::once(date);
Box::new(iter)
}
DateSource::Human(relative_time) => {
// Double check the result is overflow or not of the current_time + relative_time
// it may cause a panic of chrono::datetime::DateTime add
match now.checked_add(relative_time) {
Ok(date) => {
let iter = std::iter::once(Ok(date));
Box::new(iter)
}
}
DateSource::Stdin => {
let lines = BufReader::new(std::io::stdin()).lines();
let iter = lines.map_while(Result::ok).map(parse_date);
Box::new(iter)
}
DateSource::File(ref path) => {
if path.is_dir() {
Err(_) => {
return Err(USimpleError::new(
2,
format!("expected file, got directory {}", path.quote()),
1,
format!("invalid date {relative_time}"),
));
}
let file = File::open(path)
.map_err_context(|| path.as_os_str().to_string_lossy().to_string())?;
let lines = BufReader::new(file).lines();
let iter = lines.map_while(Result::ok).map(parse_date);
Box::new(iter)
}
DateSource::Now => {
let iter = std::iter::once(Ok(now));
Box::new(iter)
}
DateSource::Stdin => {
let lines = BufReader::new(std::io::stdin()).lines();
let iter = lines.map_while(Result::ok).map(parse_date);
Box::new(iter)
}
DateSource::File(ref path) => {
if path.is_dir() {
return Err(USimpleError::new(
2,
format!("expected file, got directory {}", path.quote()),
));
}
};
let file = File::open(path)
.map_err_context(|| path.as_os_str().to_string_lossy().to_string())?;
let lines = BufReader::new(file).lines();
let iter = lines.map_while(Result::ok).map(parse_date);
Box::new(iter)
}
DateSource::Now => {
let iter = std::iter::once(Ok(now));
Box::new(iter)
}
};
let format_string = make_format_string(&settings);
let format_string = make_format_string(&settings);
// Format all the dates
for date in dates {
match date {
// TODO: Switch to lenient formatting.
Ok(date) => match strtime::format(format_string, &date) {
Ok(s) => println!("{s}"),
Err(e) => {
return Err(USimpleError::new(
1,
format!("invalid format {format_string} ({e})"),
));
}
},
Err((input, _err)) => show!(USimpleError::new(
1,
format!("invalid date {}", input.quote())
)),
}
// Format all the dates
for date in dates {
match date {
// TODO: Switch to lenient formatting.
Ok(date) => match strtime::format(format_string, &date) {
Ok(s) => println!("{s}"),
Err(e) => {
return Err(USimpleError::new(
1,
format!("invalid format {format_string} ({e})"),
));
}
},
Err((input, _err)) => show!(USimpleError::new(
1,
format!("invalid date {}", input.quote())
)),
}
}

View file

@ -33,27 +33,27 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
if dirnames.is_empty() {
return Err(UUsageError::new(1, get_message("dirname-missing-operand")));
} else {
for path in &dirnames {
let p = Path::new(path);
match p.parent() {
Some(d) => {
if d.components().next().is_none() {
print!(".");
} else {
print_verbatim(d).unwrap();
}
}
None => {
if p.is_absolute() || path == "/" {
print!("/");
} else {
print!(".");
}
}
for path in &dirnames {
let p = Path::new(path);
match p.parent() {
Some(d) => {
if d.components().next().is_none() {
print!(".");
} else {
print_verbatim(d).unwrap();
}
}
None => {
if p.is_absolute() || path == "/" {
print!("/");
} else {
print!(".");
}
}
print!("{line_ending}");
}
print!("{line_ending}");
}
Ok(())

View file

@ -594,9 +594,11 @@ impl EnvAppData {
match cmd.status() {
Ok(exit) if !exit.success() => {
#[cfg(unix)]
if let Some(exit_code) = exit.code() {
return Err(exit_code.into());
} else {
{
if let Some(exit_code) = exit.code() {
return Err(exit_code.into());
}
// `exit.code()` returns `None` on Unix when the process is terminated by a signal.
// See std::os::unix::process::ExitStatusExt for more information. This prints out
// the interrupted process and the signal it received.

View file

@ -175,9 +175,8 @@ fn break_knuth_plass<'a, T: Clone + Iterator<Item = &'a WordInfo<'a>>>(
fresh = true;
}
break;
} else {
write_with_spaces(word, slen, args.ostream)?;
}
write_with_spaces(word, slen, args.ostream)?;
}
Ok((prev_punct, fresh))
},

View file

@ -1086,9 +1086,8 @@ fn write_columns(
}
if not_found_break && feed_line_present {
break;
} else {
out.write_all(line_separator)?;
}
out.write_all(line_separator)?;
}
Ok(lines_printed)

View file

@ -287,13 +287,13 @@ fn read_to_buffer<T: Read>(
let end = last_line_end.unwrap();
// We want to include the separator here, because it shouldn't be carried over.
return Ok((end + 1, true));
} else {
// We need to read more lines
let len = buffer.len();
// resize the vector to 10 KB more
buffer.resize(len + 1024 * 10, 0);
read_target = &mut buffer[len..];
}
// We need to read more lines
let len = buffer.len();
// resize the vector to 10 KB more
buffer.resize(len + 1024 * 10, 0);
read_target = &mut buffer[len..];
} else {
// This file has been fully read.
let mut leftover_len = read_target.len();

View file

@ -763,28 +763,28 @@ impl Write for ByteChunkWriter<'_> {
let num_bytes_written = custom_write(buf, &mut self.inner, self.settings)?;
self.num_bytes_remaining_in_current_chunk -= num_bytes_written as u64;
return Ok(carryover_bytes_written + num_bytes_written);
} else {
// Write enough bytes to fill the current chunk.
//
// Conversion to usize is safe because we checked that
// self.num_bytes_remaining_in_current_chunk is lower than
// n, which is already usize.
let i = self.num_bytes_remaining_in_current_chunk as usize;
let num_bytes_written = custom_write(&buf[..i], &mut self.inner, self.settings)?;
self.num_bytes_remaining_in_current_chunk -= num_bytes_written as u64;
// It's possible that the underlying writer did not
// write all the bytes.
if num_bytes_written < i {
return Ok(carryover_bytes_written + num_bytes_written);
} else {
// Move the window to look at only the remaining bytes.
buf = &buf[i..];
// Remember for the next iteration that we wrote these bytes.
carryover_bytes_written += num_bytes_written;
}
}
// Write enough bytes to fill the current chunk.
//
// Conversion to usize is safe because we checked that
// self.num_bytes_remaining_in_current_chunk is lower than
// n, which is already usize.
let i = self.num_bytes_remaining_in_current_chunk as usize;
let num_bytes_written = custom_write(&buf[..i], &mut self.inner, self.settings)?;
self.num_bytes_remaining_in_current_chunk -= num_bytes_written as u64;
// It's possible that the underlying writer did not
// write all the bytes.
if num_bytes_written < i {
return Ok(carryover_bytes_written + num_bytes_written);
}
// Move the window to look at only the remaining bytes.
buf = &buf[i..];
// Remember for the next iteration that we wrote these bytes.
carryover_bytes_written += num_bytes_written;
}
}
fn flush(&mut self) -> io::Result<()> {

View file

@ -582,13 +582,13 @@ impl LinesChunkBuffer {
if self.chunks.is_empty() {
// chunks is empty when a file is empty so quitting early here
return Ok(());
} else {
let length = &self.chunks.len();
let last = &mut self.chunks[length - 1];
if !last.get_buffer().ends_with(&[self.delimiter]) {
last.lines += 1;
self.lines += 1;
}
}
let length = &self.chunks.len();
let last = &mut self.chunks[length - 1];
if !last.get_buffer().ends_with(&[self.delimiter]) {
last.lines += 1;
self.lines += 1;
}
// skip unnecessary chunks and save the first chunk which may hold some lines we have to

View file

@ -113,51 +113,52 @@ pub fn wrap_chown<P: AsRef<Path>>(
}
}
return Err(out);
} else {
let changed = dest_uid != meta.uid() || dest_gid != meta.gid();
if changed {
match verbosity.level {
VerbosityLevel::Changes | VerbosityLevel::Verbose => {
let gid = meta.gid();
out = if verbosity.groups_only {
format!(
"changed group of {} from {} to {}",
path.quote(),
entries::gid2grp(gid).unwrap_or_else(|_| gid.to_string()),
entries::gid2grp(dest_gid).unwrap_or_else(|_| dest_gid.to_string())
)
} else {
let gid = meta.gid();
let uid = meta.uid();
format!(
"changed ownership of {} from {}:{} to {}:{}",
path.quote(),
entries::uid2usr(uid).unwrap_or_else(|_| uid.to_string()),
entries::gid2grp(gid).unwrap_or_else(|_| gid.to_string()),
entries::uid2usr(dest_uid).unwrap_or_else(|_| dest_uid.to_string()),
entries::gid2grp(dest_gid).unwrap_or_else(|_| dest_gid.to_string())
)
};
}
_ => (),
};
} else if verbosity.level == VerbosityLevel::Verbose {
out = if verbosity.groups_only {
format!(
"group of {} retained as {}",
path.quote(),
entries::gid2grp(dest_gid).unwrap_or_default()
)
} else {
format!(
"ownership of {} retained as {}:{}",
path.quote(),
entries::uid2usr(dest_uid).unwrap_or_else(|_| dest_uid.to_string()),
entries::gid2grp(dest_gid).unwrap_or_else(|_| dest_gid.to_string())
)
};
}
}
let changed = dest_uid != meta.uid() || dest_gid != meta.gid();
if changed {
match verbosity.level {
VerbosityLevel::Changes | VerbosityLevel::Verbose => {
let gid = meta.gid();
out = if verbosity.groups_only {
format!(
"changed group of {} from {} to {}",
path.quote(),
entries::gid2grp(gid).unwrap_or_else(|_| gid.to_string()),
entries::gid2grp(dest_gid).unwrap_or_else(|_| dest_gid.to_string())
)
} else {
let gid = meta.gid();
let uid = meta.uid();
format!(
"changed ownership of {} from {}:{} to {}:{}",
path.quote(),
entries::uid2usr(uid).unwrap_or_else(|_| uid.to_string()),
entries::gid2grp(gid).unwrap_or_else(|_| gid.to_string()),
entries::uid2usr(dest_uid).unwrap_or_else(|_| dest_uid.to_string()),
entries::gid2grp(dest_gid).unwrap_or_else(|_| dest_gid.to_string())
)
};
}
_ => (),
};
} else if verbosity.level == VerbosityLevel::Verbose {
out = if verbosity.groups_only {
format!(
"group of {} retained as {}",
path.quote(),
entries::gid2grp(dest_gid).unwrap_or_default()
)
} else {
format!(
"ownership of {} retained as {}:{}",
path.quote(),
entries::uid2usr(dest_uid).unwrap_or_else(|_| dest_uid.to_string()),
entries::gid2grp(dest_gid).unwrap_or_else(|_| dest_gid.to_string())
)
};
}
Ok(out)
}