Skip to content

Commit 5d6a04a

Browse files
authored
Fix clippy warning manual_if_else (#7177)
and enable the rule
1 parent 704421b commit 5d6a04a

File tree

21 files changed

+106
-169
lines changed

21 files changed

+106
-169
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ semicolon_if_nothing_returned = "warn"
576576
single_char_pattern = "warn"
577577
explicit_iter_loop = "warn"
578578
if_not_else = "warn"
579+
manual_if_else = "warn"
579580

580581
all = { level = "deny", priority = -1 }
581582
cargo = { level = "warn", priority = -1 }

src/uu/basename/src/basename.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,13 @@ fn basename(fullname: &str, suffix: &str) -> String {
125125

126126
// Convert to path buffer and get last path component
127127
let pb = PathBuf::from(path);
128-
match pb.components().last() {
129-
Some(c) => {
130-
let name = c.as_os_str().to_str().unwrap();
131-
if name == suffix {
132-
name.to_string()
133-
} else {
134-
name.strip_suffix(suffix).unwrap_or(name).to_string()
135-
}
136-
}
137128

138-
None => String::new(),
139-
}
129+
pb.components().next_back().map_or_else(String::new, |c| {
130+
let name = c.as_os_str().to_str().unwrap();
131+
if name == suffix {
132+
name.to_string()
133+
} else {
134+
name.strip_suffix(suffix).unwrap_or(name).to_string()
135+
}
136+
})
140137
}

src/uu/df/src/filesystem.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn is_over_mounted(mounts: &[MountInfo], mount: &MountInfo) -> bool {
5454
let last_mount_for_dir = mounts
5555
.iter()
5656
.filter(|m| m.mount_dir == mount.mount_dir)
57-
.last();
57+
.next_back();
5858

5959
if let Some(lmi) = last_mount_for_dir {
6060
lmi.dev_name != mount.dev_name

src/uu/env/src/env.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,11 @@ fn parse_signal_opt<'a>(opts: &mut Options<'a>, opt: &'a OsStr) -> UResult<()> {
130130
}
131131
});
132132
for sig in sig_vec {
133-
let sig_str = match sig.to_str() {
134-
Some(s) => s,
135-
None => {
136-
return Err(USimpleError::new(
137-
1,
138-
format!("{}: invalid signal", sig.quote()),
139-
))
140-
}
133+
let Some(sig_str) = sig.to_str() else {
134+
return Err(USimpleError::new(
135+
1,
136+
format!("{}: invalid signal", sig.quote()),
137+
));
141138
};
142139
let sig_val = parse_signal_value(sig_str)?;
143140
if !opts.ignore_signal.contains(&sig_val) {

src/uu/fmt/src/parasplit.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,8 @@ impl ParagraphStream<'_> {
255255
if l_slice.starts_with("From ") {
256256
true
257257
} else {
258-
let colon_posn = match l_slice.find(':') {
259-
Some(n) => n,
260-
None => return false,
258+
let Some(colon_posn) = l_slice.find(':') else {
259+
return false;
261260
};
262261

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

561560
// find the start of the next word, and record if we find a tab character
562561
let (before_tab, after_tab, word_start) =
563-
match self.analyze_tabs(&self.string[old_position..]) {
564-
(b, a, Some(s)) => (b, a, s + old_position),
565-
(_, _, None) => {
566-
self.position = self.length;
567-
return None;
568-
}
562+
if let (b, a, Some(s)) = self.analyze_tabs(&self.string[old_position..]) {
563+
(b, a, s + old_position)
564+
} else {
565+
self.position = self.length;
566+
return None;
569567
};
570568

571569
// find the beginning of the next whitespace

src/uu/head/src/parse.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,8 @@ fn process_num_block(
9191
}
9292
if let Some(n) = multiplier {
9393
options.push(OsString::from("-c"));
94-
let num = match num.checked_mul(n) {
95-
Some(n) => n,
96-
None => return Some(Err(ParseError::Overflow)),
94+
let Some(num) = num.checked_mul(n) else {
95+
return Some(Err(ParseError::Overflow));
9796
};
9897
options.push(OsString::from(format!("{num}")));
9998
} else {

src/uu/install/src/install.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ fn copy_files_into_dir(files: &[PathBuf], target_dir: &Path, b: &Behavior) -> UR
652652
}
653653

654654
let mut targetpath = target_dir.to_path_buf();
655-
let filename = sourcepath.components().last().unwrap();
655+
let filename = sourcepath.components().next_back().unwrap();
656656
targetpath.push(filename);
657657

658658
show_if_err!(copy(sourcepath, &targetpath, b));

src/uu/ls/src/ls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,8 @@ fn parse_time_style(options: &clap::ArgMatches) -> Result<TimeStyle, LsError> {
379379
//If both FULL_TIME and TIME_STYLE are present
380380
//The one added last is dominant
381381
if options.get_flag(options::FULL_TIME)
382-
&& options.indices_of(options::FULL_TIME).unwrap().last()
383-
> options.indices_of(options::TIME_STYLE).unwrap().last()
382+
&& options.indices_of(options::FULL_TIME).unwrap().next_back()
383+
> options.indices_of(options::TIME_STYLE).unwrap().next_back()
384384
{
385385
Ok(TimeStyle::FullIso)
386386
} else {

src/uu/seq/src/seq.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
101101
let options = SeqOptions {
102102
separator: matches
103103
.get_one::<String>(OPT_SEPARATOR)
104-
.map(|s| s.as_str())
105-
.unwrap_or("\n")
104+
.map_or("\n", |s| s.as_str())
106105
.to_string(),
107106
terminator: matches
108107
.get_one::<String>(OPT_TERMINATOR)
@@ -150,26 +149,24 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
150149

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

153-
let format = match options.format {
154-
Some(f) => {
155-
let f = Format::<num_format::Float>::parse(f)?;
156-
Some(f)
157-
}
158-
None => None,
159-
};
152+
let format = options
153+
.format
154+
.map(Format::<num_format::Float>::parse)
155+
.transpose()?;
156+
160157
let result = print_seq(
161158
(first.number, increment.number, last.number),
162159
precision,
163160
&options.separator,
164161
&options.terminator,
165162
options.equal_width,
166163
padding,
167-
&format,
164+
format.as_ref(),
168165
);
169166
match result {
170-
Ok(_) => Ok(()),
167+
Ok(()) => Ok(()),
171168
Err(err) if err.kind() == ErrorKind::BrokenPipe => Ok(()),
172-
Err(e) => Err(e.map_err_context(|| "write error".into())),
169+
Err(err) => Err(err.map_err_context(|| "write error".into())),
173170
}
174171
}
175172

@@ -263,7 +260,7 @@ fn print_seq(
263260
terminator: &str,
264261
pad: bool,
265262
padding: usize,
266-
format: &Option<Format<num_format::Float>>,
263+
format: Option<&Format<num_format::Float>>,
267264
) -> std::io::Result<()> {
268265
let stdout = stdout();
269266
let mut stdout = stdout.lock();

src/uu/sort/src/ext_sort.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,8 @@ fn read_write_loop<I: WriteableTmpFile>(
224224
let mut sender_option = Some(sender);
225225
let mut tmp_files = vec![];
226226
loop {
227-
let chunk = match receiver.recv() {
228-
Ok(it) => it,
229-
_ => {
230-
return Ok(ReadResult::WroteChunksToFile { tmp_files });
231-
}
227+
let Ok(chunk) = receiver.recv() else {
228+
return Ok(ReadResult::WroteChunksToFile { tmp_files });
232229
};
233230

234231
let tmp_file = write::<I>(

0 commit comments

Comments
 (0)