Skip to content

Commit 368230e

Browse files
authored
chore: fix clippy::single_match_else (#10699)
1 parent d66dfe9 commit 368230e

37 files changed

Lines changed: 543 additions & 626 deletions

File tree

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,6 @@ redundant_closure_for_method_calls = "allow" # 125
665665
cast_possible_truncation = "allow" # 122
666666
too_many_lines = "allow" # 101
667667
trivially_copy_pass_by_ref = "allow" # 84
668-
single_match_else = "allow" # 82
669668
cast_possible_wrap = "allow" # 78
670669
cast_sign_loss = "allow" # 70
671670
struct_excessive_bools = "allow" # 68

src/uu/cat/src/cat.rs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -639,40 +639,34 @@ fn write_end<W: Write>(
639639

640640
fn write_to_end<W: Write>(in_buf: &[u8], writer: &mut W) -> io::Result<usize> {
641641
// using memchr2 significantly improves performances
642-
match memchr2(b'\n', b'\r', in_buf) {
643-
Some(p) => {
644-
writer.write_all(&in_buf[..p])?;
645-
Ok(p)
646-
}
647-
None => {
648-
writer.write_all(in_buf)?;
649-
Ok(in_buf.len())
650-
}
642+
if let Some(p) = memchr2(b'\n', b'\r', in_buf) {
643+
writer.write_all(&in_buf[..p])?;
644+
Ok(p)
645+
} else {
646+
writer.write_all(in_buf)?;
647+
Ok(in_buf.len())
651648
}
652649
}
653650

654651
fn write_tab_to_end<W: Write>(mut in_buf: &[u8], writer: &mut W) -> io::Result<usize> {
655652
let mut count = 0;
656653
loop {
657-
match in_buf
654+
if let Some(p) = in_buf
658655
.iter()
659656
.position(|c| *c == b'\n' || *c == b'\t' || *c == b'\r')
660657
{
661-
Some(p) => {
662-
writer.write_all(&in_buf[..p])?;
663-
if in_buf[p] == b'\t' {
664-
writer.write_all(b"^I")?;
665-
in_buf = &in_buf[p + 1..];
666-
count += p + 1;
667-
} else {
668-
// b'\n' or b'\r'
669-
return Ok(count + p);
670-
}
671-
}
672-
None => {
673-
writer.write_all(in_buf)?;
674-
return Ok(in_buf.len() + count);
658+
writer.write_all(&in_buf[..p])?;
659+
if in_buf[p] == b'\t' {
660+
writer.write_all(b"^I")?;
661+
in_buf = &in_buf[p + 1..];
662+
count += p + 1;
663+
} else {
664+
// b'\n' or b'\r'
665+
return Ok(count + p);
675666
}
667+
} else {
668+
writer.write_all(in_buf)?;
669+
return Ok(in_buf.len() + count);
676670
}
677671
}
678672
}

src/uu/chmod/src/chmod.rs

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -272,42 +272,41 @@ impl Chmoder {
272272
/// Calculate the new mode based on the current mode and the chmod specification.
273273
/// Returns (`new_mode`, `naively_expected_new_mode`) for symbolic modes, or (`new_mode`, `new_mode`) for numeric/reference modes.
274274
fn calculate_new_mode(&self, current_mode: u32, is_dir: bool) -> UResult<(u32, u32)> {
275-
match self.fmode {
276-
Some(mode) => Ok((mode, mode)),
277-
None => {
278-
let cmode_unwrapped = self.cmode.clone().unwrap();
279-
let mut new_mode = current_mode;
280-
let mut naively_expected_new_mode = current_mode;
281-
282-
for mode in cmode_unwrapped.split(',') {
283-
let result = if mode.chars().any(|c| c.is_ascii_digit()) {
284-
mode::parse_numeric(new_mode, mode, is_dir).map(|v| (v, v))
285-
} else {
286-
mode::parse_symbolic(new_mode, mode, mode::get_umask(), is_dir).map(|m| {
287-
// calculate the new mode as if umask was 0
288-
let naive_mode =
289-
mode::parse_symbolic(naively_expected_new_mode, mode, 0, is_dir)
290-
.unwrap(); // we know that mode must be valid, so this cannot fail
291-
(m, naive_mode)
292-
})
293-
};
294-
295-
match result {
296-
Ok((mode, naive_mode)) => {
297-
new_mode = mode;
298-
naively_expected_new_mode = naive_mode;
299-
}
300-
Err(f) => {
301-
return if self.quiet {
302-
Err(ExitCode::new(1))
303-
} else {
304-
Err(USimpleError::new(1, f))
305-
};
306-
}
275+
if let Some(mode) = self.fmode {
276+
Ok((mode, mode))
277+
} else {
278+
let cmode_unwrapped = self.cmode.clone().unwrap();
279+
let mut new_mode = current_mode;
280+
let mut naively_expected_new_mode = current_mode;
281+
282+
for mode in cmode_unwrapped.split(',') {
283+
let result = if mode.chars().any(|c| c.is_ascii_digit()) {
284+
mode::parse_numeric(new_mode, mode, is_dir).map(|v| (v, v))
285+
} else {
286+
mode::parse_symbolic(new_mode, mode, mode::get_umask(), is_dir).map(|m| {
287+
// calculate the new mode as if umask was 0
288+
let naive_mode =
289+
mode::parse_symbolic(naively_expected_new_mode, mode, 0, is_dir)
290+
.unwrap(); // we know that mode must be valid, so this cannot fail
291+
(m, naive_mode)
292+
})
293+
};
294+
295+
match result {
296+
Ok((mode, naive_mode)) => {
297+
new_mode = mode;
298+
naively_expected_new_mode = naive_mode;
299+
}
300+
Err(f) => {
301+
return if self.quiet {
302+
Err(ExitCode::new(1))
303+
} else {
304+
Err(USimpleError::new(1, f))
305+
};
307306
}
308307
}
309-
Ok((new_mode, naively_expected_new_mode))
310308
}
309+
Ok((new_mode, naively_expected_new_mode))
311310
}
312311
}
313312

@@ -655,32 +654,31 @@ impl Chmoder {
655654
self.calculate_new_mode(fperm, file.is_dir())?;
656655

657656
// Determine how to apply the permissions
658-
match self.fmode {
659-
Some(mode) => self.change_file(fperm, mode, file)?,
660-
None => {
661-
// Special handling for symlinks when not dereferencing
662-
if file.is_symlink() && !dereference {
663-
// TODO: On most Unix systems, symlink permissions are ignored by the kernel,
664-
// so changing them has no effect. We skip this operation for compatibility.
665-
// Note that "chmod without dereferencing" effectively does nothing on symlinks.
666-
if self.verbose {
667-
println!(
668-
"neither symbolic link {} nor referent has been changed",
669-
file.quote()
670-
);
671-
}
672-
} else {
673-
self.change_file(fperm, new_mode, file)?;
674-
}
675-
// if a permission would have been removed if umask was 0, but it wasn't because umask was not 0, print an error and fail
676-
if (new_mode & !naively_expected_new_mode) != 0 {
677-
return Err(ChmodError::NewPermissions(
678-
file.into(),
679-
display_permissions_unix(new_mode as mode_t, false),
680-
display_permissions_unix(naively_expected_new_mode as mode_t, false),
681-
)
682-
.into());
657+
if let Some(mode) = self.fmode {
658+
self.change_file(fperm, mode, file)?;
659+
} else {
660+
// Special handling for symlinks when not dereferencing
661+
if file.is_symlink() && !dereference {
662+
// TODO: On most Unix systems, symlink permissions are ignored by the kernel,
663+
// so changing them has no effect. We skip this operation for compatibility.
664+
// Note that "chmod without dereferencing" effectively does nothing on symlinks.
665+
if self.verbose {
666+
println!(
667+
"neither symbolic link {} nor referent has been changed",
668+
file.quote()
669+
);
683670
}
671+
} else {
672+
self.change_file(fperm, new_mode, file)?;
673+
}
674+
// if a permission would have been removed if umask was 0, but it wasn't because umask was not 0, print an error and fail
675+
if (new_mode & !naively_expected_new_mode) != 0 {
676+
return Err(ChmodError::NewPermissions(
677+
file.into(),
678+
display_permissions_unix(new_mode as mode_t, false),
679+
display_permissions_unix(naively_expected_new_mode as mode_t, false),
680+
)
681+
.into());
684682
}
685683
}
686684

src/uu/cp/src/cp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -828,12 +828,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
828828
let (sources, target) = parse_path_args(paths, &options)?;
829829

830830
if let Err(error) = copy(&sources, &target, &options) {
831-
match error {
831+
if let CpError::NotAllFilesCopied = error {
832832
// Error::NotAllFilesCopied is non-fatal, but the error
833833
// code should still be EXIT_ERR as does GNU cp
834-
CpError::NotAllFilesCopied => {}
834+
} else {
835835
// Else we caught a fatal bubbled-up error, log it to stderr
836-
_ => show_error!("{error}"),
836+
show_error!("{error}");
837837
}
838838
set_exit_code(EXIT_ERR);
839839
}

src/uu/cp/src/platform/macos.rs

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -85,37 +85,32 @@ pub(crate) fn copy_on_write(
8585
if raw_pfn.is_null() || error != 0 {
8686
// clonefile(2) is either not supported or it errored out (possibly because the FS does not
8787
// support COW).
88-
match reflink_mode {
89-
ReflinkMode::Always => {
90-
return Err(translate!("cp-error-failed-to-clone", "source" => source.quote(), "dest" => dest.quote(), "error" => error)
88+
if reflink_mode == ReflinkMode::Always {
89+
return Err(translate!("cp-error-failed-to-clone", "source" => source.quote(), "dest" => dest.quote(), "error" => error)
9190
.into());
92-
}
93-
_ => {
94-
copy_debug.reflink = OffloadReflinkDebug::Yes;
95-
if source_is_stream {
96-
let mut src_file = File::open(source)?;
97-
let mode = 0o622 & !get_umask();
98-
let mut dst_file = OpenOptions::new()
99-
.create(true)
100-
.write(true)
101-
.mode(mode)
102-
.open(dest)?;
103-
104-
let dest_is_stream = is_stream(&dst_file.metadata()?);
105-
if !dest_is_stream {
106-
// `copy_stream` doesn't clear the dest file, if dest is not a stream, we should clear it manually.
107-
dst_file.set_len(0)?;
108-
}
91+
}
92+
copy_debug.reflink = OffloadReflinkDebug::Yes;
93+
if source_is_stream {
94+
let mut src_file = File::open(source)?;
95+
let mode = 0o622 & !get_umask();
96+
let mut dst_file = OpenOptions::new()
97+
.create(true)
98+
.write(true)
99+
.mode(mode)
100+
.open(dest)?;
109101

110-
buf_copy::copy_stream(&mut src_file, &mut dst_file)
111-
.map_err(|_| std::io::Error::from(std::io::ErrorKind::Other))
112-
.map_err(|e| CpError::IoErrContext(e, context.to_owned()))?
113-
} else {
114-
fs::copy(source, dest)
115-
.map_err(|e| CpError::IoErrContext(e, context.to_owned()))?
116-
}
102+
let dest_is_stream = is_stream(&dst_file.metadata()?);
103+
if !dest_is_stream {
104+
// `copy_stream` doesn't clear the dest file, if dest is not a stream, we should clear it manually.
105+
dst_file.set_len(0)?;
117106
}
118-
};
107+
108+
buf_copy::copy_stream(&mut src_file, &mut dst_file)
109+
.map_err(|_| std::io::Error::from(std::io::ErrorKind::Other))
110+
.map_err(|e| CpError::IoErrContext(e, context.to_owned()))?;
111+
} else {
112+
fs::copy(source, dest).map_err(|e| CpError::IoErrContext(e, context.to_owned()))?;
113+
}
119114
}
120115

121116
Ok(copy_debug)

src/uu/csplit/src/csplit.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -454,16 +454,11 @@ impl SplitWriter<'_> {
454454

455455
// write the extra lines required by the offset
456456
while offset > 0 {
457-
match input_iter.next() {
458-
Some((_, line)) => {
459-
self.writeln(&line?)?;
460-
}
461-
None => {
462-
self.finish_split()?;
463-
return Err(CsplitError::LineOutOfRange(
464-
pattern_as_str.to_string(),
465-
));
466-
}
457+
if let Some((_, line)) = input_iter.next() {
458+
self.writeln(&line?)?;
459+
} else {
460+
self.finish_split()?;
461+
return Err(CsplitError::LineOutOfRange(pattern_as_str.to_string()));
467462
}
468463
offset -= 1;
469464
}

src/uu/csplit/src/patterns.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,18 @@ impl Iterator for ExecutePatternIter {
7171
type Item = (Option<usize>, usize);
7272

7373
fn next(&mut self) -> Option<(Option<usize>, usize)> {
74-
match self.max {
74+
if let Some(m) = self.max {
7575
// iterate until m is reached
76-
Some(m) => {
77-
if self.cur == m {
78-
None
79-
} else {
80-
self.cur += 1;
81-
Some((self.max, self.cur))
82-
}
83-
}
84-
// no limit, just increment a counter
85-
None => {
76+
if self.cur == m {
77+
None
78+
} else {
8679
self.cur += 1;
87-
Some((None, self.cur))
80+
Some((self.max, self.cur))
8881
}
82+
} else {
83+
// no limit, just increment a counter
84+
self.cur += 1;
85+
Some((None, self.cur))
8986
}
9087
}
9188
}

0 commit comments

Comments
 (0)