Skip to content

Commit

Permalink
Fix clippy warning manual_if_else (#7177)
Browse files Browse the repository at this point in the history
and enable the rule
  • Loading branch information
danieleades authored Jan 23, 2025
1 parent 704421b commit 5d6a04a
Show file tree
Hide file tree
Showing 21 changed files with 106 additions and 169 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ semicolon_if_nothing_returned = "warn"
single_char_pattern = "warn"
explicit_iter_loop = "warn"
if_not_else = "warn"
manual_if_else = "warn"

all = { level = "deny", priority = -1 }
cargo = { level = "warn", priority = -1 }
Expand Down
19 changes: 8 additions & 11 deletions src/uu/basename/src/basename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,13 @@ fn basename(fullname: &str, suffix: &str) -> String {

// Convert to path buffer and get last path component
let pb = PathBuf::from(path);
match pb.components().last() {
Some(c) => {
let name = c.as_os_str().to_str().unwrap();
if name == suffix {
name.to_string()
} else {
name.strip_suffix(suffix).unwrap_or(name).to_string()
}
}

None => String::new(),
}
pb.components().next_back().map_or_else(String::new, |c| {
let name = c.as_os_str().to_str().unwrap();
if name == suffix {
name.to_string()
} else {
name.strip_suffix(suffix).unwrap_or(name).to_string()
}
})
}
2 changes: 1 addition & 1 deletion src/uu/df/src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn is_over_mounted(mounts: &[MountInfo], mount: &MountInfo) -> bool {
let last_mount_for_dir = mounts
.iter()
.filter(|m| m.mount_dir == mount.mount_dir)
.last();
.next_back();

if let Some(lmi) = last_mount_for_dir {
lmi.dev_name != mount.dev_name
Expand Down
13 changes: 5 additions & 8 deletions src/uu/env/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,11 @@ fn parse_signal_opt<'a>(opts: &mut Options<'a>, opt: &'a OsStr) -> UResult<()> {
}
});
for sig in sig_vec {
let sig_str = match sig.to_str() {
Some(s) => s,
None => {
return Err(USimpleError::new(
1,
format!("{}: invalid signal", sig.quote()),
))
}
let Some(sig_str) = sig.to_str() else {
return Err(USimpleError::new(
1,
format!("{}: invalid signal", sig.quote()),
));
};
let sig_val = parse_signal_value(sig_str)?;
if !opts.ignore_signal.contains(&sig_val) {
Expand Down
16 changes: 7 additions & 9 deletions src/uu/fmt/src/parasplit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,8 @@ impl ParagraphStream<'_> {
if l_slice.starts_with("From ") {
true
} else {
let colon_posn = match l_slice.find(':') {
Some(n) => n,
None => return false,
let Some(colon_posn) = l_slice.find(':') else {
return false;
};

// header field must be nonzero length
Expand Down Expand Up @@ -560,12 +559,11 @@ impl<'a> Iterator for WordSplit<'a> {

// find the start of the next word, and record if we find a tab character
let (before_tab, after_tab, word_start) =
match self.analyze_tabs(&self.string[old_position..]) {
(b, a, Some(s)) => (b, a, s + old_position),
(_, _, None) => {
self.position = self.length;
return None;
}
if let (b, a, Some(s)) = self.analyze_tabs(&self.string[old_position..]) {
(b, a, s + old_position)
} else {
self.position = self.length;
return None;
};

// find the beginning of the next whitespace
Expand Down
5 changes: 2 additions & 3 deletions src/uu/head/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,8 @@ fn process_num_block(
}
if let Some(n) = multiplier {
options.push(OsString::from("-c"));
let num = match num.checked_mul(n) {
Some(n) => n,
None => return Some(Err(ParseError::Overflow)),
let Some(num) = num.checked_mul(n) else {
return Some(Err(ParseError::Overflow));
};
options.push(OsString::from(format!("{num}")));
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/uu/install/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ fn copy_files_into_dir(files: &[PathBuf], target_dir: &Path, b: &Behavior) -> UR
}

let mut targetpath = target_dir.to_path_buf();
let filename = sourcepath.components().last().unwrap();
let filename = sourcepath.components().next_back().unwrap();
targetpath.push(filename);

show_if_err!(copy(sourcepath, &targetpath, b));
Expand Down
4 changes: 2 additions & 2 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ fn parse_time_style(options: &clap::ArgMatches) -> Result<TimeStyle, LsError> {
//If both FULL_TIME and TIME_STYLE are present
//The one added last is dominant
if options.get_flag(options::FULL_TIME)
&& options.indices_of(options::FULL_TIME).unwrap().last()
> options.indices_of(options::TIME_STYLE).unwrap().last()
&& options.indices_of(options::FULL_TIME).unwrap().next_back()
> options.indices_of(options::TIME_STYLE).unwrap().next_back()
{
Ok(TimeStyle::FullIso)
} else {
Expand Down
23 changes: 10 additions & 13 deletions src/uu/seq/src/seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let options = SeqOptions {
separator: matches
.get_one::<String>(OPT_SEPARATOR)
.map(|s| s.as_str())
.unwrap_or("\n")
.map_or("\n", |s| s.as_str())
.to_string(),
terminator: matches
.get_one::<String>(OPT_TERMINATOR)
Expand Down Expand Up @@ -150,26 +149,24 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {

let precision = select_precision(first_precision, increment_precision, last_precision);

let format = match options.format {
Some(f) => {
let f = Format::<num_format::Float>::parse(f)?;
Some(f)
}
None => None,
};
let format = options
.format
.map(Format::<num_format::Float>::parse)
.transpose()?;

let result = print_seq(
(first.number, increment.number, last.number),
precision,
&options.separator,
&options.terminator,
options.equal_width,
padding,
&format,
format.as_ref(),
);
match result {
Ok(_) => Ok(()),
Ok(()) => Ok(()),
Err(err) if err.kind() == ErrorKind::BrokenPipe => Ok(()),
Err(e) => Err(e.map_err_context(|| "write error".into())),
Err(err) => Err(err.map_err_context(|| "write error".into())),
}
}

Expand Down Expand Up @@ -263,7 +260,7 @@ fn print_seq(
terminator: &str,
pad: bool,
padding: usize,
format: &Option<Format<num_format::Float>>,
format: Option<&Format<num_format::Float>>,
) -> std::io::Result<()> {
let stdout = stdout();
let mut stdout = stdout.lock();
Expand Down
7 changes: 2 additions & 5 deletions src/uu/sort/src/ext_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,8 @@ fn read_write_loop<I: WriteableTmpFile>(
let mut sender_option = Some(sender);
let mut tmp_files = vec![];
loop {
let chunk = match receiver.recv() {
Ok(it) => it,
_ => {
return Ok(ReadResult::WroteChunksToFile { tmp_files });
}
let Ok(chunk) = receiver.recv() else {
return Ok(ReadResult::WroteChunksToFile { tmp_files });
};

let tmp_file = write::<I>(
Expand Down
19 changes: 8 additions & 11 deletions src/uu/split/src/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1408,18 +1408,15 @@ where
};

let bytes = line.as_slice();
match kth_chunk {
Some(chunk_number) => {
if (i % num_chunks) == (chunk_number - 1) as usize {
stdout_writer.write_all(bytes)?;
}
if let Some(chunk_number) = kth_chunk {
if (i % num_chunks) == (chunk_number - 1) as usize {
stdout_writer.write_all(bytes)?;
}
None => {
let writer = out_files.get_writer(i % num_chunks, settings)?;
let writer_stdin_open = custom_write_all(bytes, writer, settings)?;
if !writer_stdin_open {
closed_writers += 1;
}
} else {
let writer = out_files.get_writer(i % num_chunks, settings)?;
let writer_stdin_open = custom_write_all(bytes, writer, settings)?;
if !writer_stdin_open {
closed_writers += 1;
}
}
i += 1;
Expand Down
10 changes: 4 additions & 6 deletions src/uu/tail/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ pub enum ParseError {
/// Parses obsolete syntax
/// tail -\[NUM\]\[bcl\]\[f\] and tail +\[NUM\]\[bcl\]\[f\]
pub fn parse_obsolete(src: &OsString) -> Option<Result<ObsoleteArgs, ParseError>> {
let mut rest = match src.to_str() {
Some(src) => src,
None => return Some(Err(ParseError::InvalidEncoding)),
let Some(mut rest) = src.to_str() else {
return Some(Err(ParseError::InvalidEncoding));
};
let sign = if let Some(r) = rest.strip_prefix('-') {
rest = r;
Expand Down Expand Up @@ -86,9 +85,8 @@ pub fn parse_obsolete(src: &OsString) -> Option<Result<ObsoleteArgs, ParseError>
}

let multiplier = if mode == 'b' { 512 } else { 1 };
let num = match num.checked_mul(multiplier) {
Some(n) => n,
None => return Some(Err(ParseError::Overflow)),
let Some(num) = num.checked_mul(multiplier) else {
return Some(Err(ParseError::Overflow));
};

Some(Ok(ObsoleteArgs {
Expand Down
16 changes: 4 additions & 12 deletions src/uu/test/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,8 @@ fn integers(a: &OsStr, b: &OsStr, op: &OsStr) -> ParseResult<bool> {
fn files(a: &OsStr, b: &OsStr, op: &OsStr) -> ParseResult<bool> {
// Don't manage the error. GNU doesn't show error when doing
// test foo -nt bar
let f_a = match fs::metadata(a) {
Ok(f) => f,
Err(_) => return Ok(false),
};
let f_b = match fs::metadata(b) {
Ok(f) => f,
Err(_) => return Ok(false),
let (Ok(f_a), Ok(f_b)) = (fs::metadata(a), fs::metadata(b)) else {
return Ok(false);
};

Ok(match op.to_str() {
Expand Down Expand Up @@ -290,11 +285,8 @@ fn path(path: &OsStr, condition: &PathCondition) -> bool {
fs::metadata(path)
};

let metadata = match metadata {
Ok(metadata) => metadata,
Err(_) => {
return false;
}
let Ok(metadata) = metadata else {
return false;
};

let file_type = metadata.file_type();
Expand Down
13 changes: 5 additions & 8 deletions src/uu/touch/src/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,14 +599,11 @@ fn parse_timestamp(s: &str) -> UResult<FileTime> {

let local = NaiveDateTime::parse_from_str(&ts, format)
.map_err(|_| USimpleError::new(1, format!("invalid date ts format {}", ts.quote())))?;
let mut local = match chrono::Local.from_local_datetime(&local) {
LocalResult::Single(dt) => dt,
_ => {
return Err(USimpleError::new(
1,
format!("invalid date ts format {}", ts.quote()),
))
}
let LocalResult::Single(mut local) = chrono::Local.from_local_datetime(&local) else {
return Err(USimpleError::new(
1,
format!("invalid date ts format {}", ts.quote()),
));
};

// Chrono caps seconds at 59, but 60 is valid. It might be a leap second
Expand Down
9 changes: 3 additions & 6 deletions src/uu/uniq/src/uniq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,9 @@ impl Uniq {

// Convert the leftover bytes to UTF-8 for character-based -w
// If invalid UTF-8, just compare them as individual bytes (fallback).
let string_after_skip = match std::str::from_utf8(fields_to_check) {
Ok(s) => s,
Err(_) => {
// Fallback: if invalid UTF-8, treat them as single-byte “chars”
return closure(&mut fields_to_check.iter().map(|&b| b as char));
}
let Ok(string_after_skip) = std::str::from_utf8(fields_to_check) else {
// Fallback: if invalid UTF-8, treat them as single-byte “chars”
return closure(&mut fields_to_check.iter().map(|&b| b as char));
};

let total_chars = string_after_skip.chars().count();
Expand Down
24 changes: 8 additions & 16 deletions src/uucore/src/lib/features/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,14 +652,10 @@ pub fn are_hardlinks_to_same_file(_source: &Path, _target: &Path) -> bool {
/// * `bool` - Returns `true` if the paths are hard links to the same file, and `false` otherwise.
#[cfg(unix)]
pub fn are_hardlinks_to_same_file(source: &Path, target: &Path) -> bool {
let source_metadata = match fs::symlink_metadata(source) {
Ok(metadata) => metadata,
Err(_) => return false,
};

let target_metadata = match fs::symlink_metadata(target) {
Ok(metadata) => metadata,
Err(_) => return false,
let (Ok(source_metadata), Ok(target_metadata)) =
(fs::symlink_metadata(source), fs::symlink_metadata(target))
else {
return false;
};

source_metadata.ino() == target_metadata.ino() && source_metadata.dev() == target_metadata.dev()
Expand All @@ -682,14 +678,10 @@ pub fn are_hardlinks_or_one_way_symlink_to_same_file(_source: &Path, _target: &P
/// * `bool` - Returns `true` if either of above conditions are true, and `false` otherwise.
#[cfg(unix)]
pub fn are_hardlinks_or_one_way_symlink_to_same_file(source: &Path, target: &Path) -> bool {
let source_metadata = match fs::metadata(source) {
Ok(metadata) => metadata,
Err(_) => return false,
};

let target_metadata = match fs::symlink_metadata(target) {
Ok(metadata) => metadata,
Err(_) => return false,
let (Ok(source_metadata), Ok(target_metadata)) =
(fs::metadata(source), fs::symlink_metadata(target))
else {
return false;
};

source_metadata.ino() == target_metadata.ino() && source_metadata.dev() == target_metadata.dev()
Expand Down
13 changes: 5 additions & 8 deletions src/uucore/src/lib/features/fsxattr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,10 @@ pub fn apply_xattrs<P: AsRef<Path>>(
/// `true` if the file has extended attributes (indicating an ACL), `false` otherwise.
pub fn has_acl<P: AsRef<Path>>(file: P) -> bool {
// don't use exacl here, it is doing more getxattr call then needed
match xattr::list(file) {
Ok(acl) => {
// if we have extra attributes, we have an acl
acl.count() > 0
}
Err(_) => false,
}
xattr::list(file).is_ok_and(|acl| {
// if we have extra attributes, we have an acl
acl.count() > 0
})
}

/// Returns the permissions bits of a file or directory which has Access Control List (ACL) entries based on its
Expand Down Expand Up @@ -132,7 +129,7 @@ pub fn get_acl_perm_bits_from_xattr<P: AsRef<Path>>(source: P) -> u32 {

for entry in acl_entries.chunks_exact(4) {
// Third byte and fourth byte will be the perm bits
perm = (perm << 3) | entry[2] as u32 | entry[3] as u32;
perm = (perm << 3) | u32::from(entry[2]) | u32::from(entry[3]);
}
return perm;
}
Expand Down
Loading

0 comments on commit 5d6a04a

Please sign in to comment.