Skip to content

Commit f99b7ab

Browse files
author
daniel.eades
committed
minor code quality improvements
1 parent 0a230e5 commit f99b7ab

File tree

6 files changed

+53
-78
lines changed

6 files changed

+53
-78
lines changed

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/ls/src/ls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,8 @@ fn parse_time_style(options: &clap::ArgMatches) -> Result<TimeStyle, LsError> {
403403
//If both FULL_TIME and TIME_STYLE are present
404404
//The one added last is dominant
405405
if options.get_flag(options::FULL_TIME)
406-
&& options.indices_of(options::FULL_TIME).unwrap().last()
407-
> options.indices_of(options::TIME_STYLE).unwrap().last()
406+
&& options.indices_of(options::FULL_TIME).unwrap().next_back()
407+
> options.indices_of(options::TIME_STYLE).unwrap().next_back()
408408
{
409409
Ok(TimeStyle::FullIso)
410410
} 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(|f| Format::<num_format::Float>::parse(f))
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/split/src/split.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,18 +1408,15 @@ where
14081408
};
14091409

14101410
let bytes = line.as_slice();
1411-
match kth_chunk {
1412-
Some(chunk_number) => {
1413-
if (i % num_chunks) == (chunk_number - 1) as usize {
1414-
stdout_writer.write_all(bytes)?;
1415-
}
1411+
if let Some(chunk_number) = kth_chunk {
1412+
if (i % num_chunks) == (chunk_number - 1) as usize {
1413+
stdout_writer.write_all(bytes)?;
14161414
}
1417-
None => {
1418-
let writer = out_files.get_writer(i % num_chunks, settings)?;
1419-
let writer_stdin_open = custom_write_all(bytes, writer, settings)?;
1420-
if !writer_stdin_open {
1421-
closed_writers += 1;
1422-
}
1415+
} else {
1416+
let writer = out_files.get_writer(i % num_chunks, settings)?;
1417+
let writer_stdin_open = custom_write_all(bytes, writer, settings)?;
1418+
if !writer_stdin_open {
1419+
closed_writers += 1;
14231420
}
14241421
}
14251422
i += 1;

src/uucore/src/lib/features/fsxattr.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,7 @@ pub fn apply_xattrs<P: AsRef<Path>>(
7979
/// `true` if the file has extended attributes (indicating an ACL), `false` otherwise.
8080
pub fn has_acl<P: AsRef<Path>>(file: P) -> bool {
8181
// don't use exacl here, it is doing more getxattr call then needed
82-
match xattr::list(file) {
83-
Ok(acl) => {
84-
// if we have extra attributes, we have an acl
85-
acl.count() > 0
86-
}
87-
Err(_) => false,
88-
}
82+
xattr::list(file).map_or(false, |acl| acl.count() > 0)
8983
}
9084

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

133127
for entry in acl_entries.chunks_exact(4) {
134128
// Third byte and fourth byte will be the perm bits
135-
perm = (perm << 3) | entry[2] as u32 | entry[3] as u32;
129+
perm = (perm << 3) | u32::from(entry[2]) | u32::from(entry[3]);
136130
}
137131
return perm;
138132
}

src/uucore/src/lib/features/perms.rs

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -273,18 +273,15 @@ impl ChownExecutor {
273273
#[allow(clippy::cognitive_complexity)]
274274
fn traverse<P: AsRef<Path>>(&self, root: P) -> i32 {
275275
let path = root.as_ref();
276-
let meta = match self.obtain_meta(path, self.dereference) {
277-
Some(m) => m,
278-
_ => {
279-
if self.verbosity.level == VerbosityLevel::Verbose {
280-
println!(
281-
"failed to change ownership of {} to {}",
282-
path.quote(),
283-
self.raw_owner
284-
);
285-
}
286-
return 1;
276+
let Some(meta) = self.obtain_meta(path, self.dereference) else {
277+
if self.verbosity.level == VerbosityLevel::Verbose {
278+
println!(
279+
"failed to change ownership of {} to {}",
280+
path.quote(),
281+
self.raw_owner
282+
);
287283
}
284+
return 1;
288285
};
289286

290287
if self.recursive
@@ -370,17 +367,15 @@ impl ChownExecutor {
370367
Ok(entry) => entry,
371368
};
372369
let path = entry.path();
373-
let meta = match self.obtain_meta(path, self.dereference) {
374-
Some(m) => m,
375-
_ => {
376-
ret = 1;
377-
if entry.file_type().is_dir() {
378-
// Instruct walkdir to skip this directory to avoid getting another error
379-
// when walkdir tries to query the children of this directory.
380-
iterator.skip_current_dir();
381-
}
382-
continue;
370+
371+
let Some(meta) = self.obtain_meta(path, self.dereference) else {
372+
ret = 1;
373+
if entry.file_type().is_dir() {
374+
// Instruct walkdir to skip this directory to avoid getting another error
375+
// when walkdir tries to query the children of this directory.
376+
iterator.skip_current_dir();
383377
}
378+
continue;
384379
};
385380

386381
if self.preserve_root && is_root(path, self.traverse_symlinks == TraverseSymlinks::All)
@@ -425,24 +420,18 @@ impl ChownExecutor {
425420

426421
fn obtain_meta<P: AsRef<Path>>(&self, path: P, follow: bool) -> Option<Metadata> {
427422
let path = path.as_ref();
428-
429-
let meta = get_metadata(path, follow);
430-
431-
match meta {
432-
Err(e) => {
433-
match self.verbosity.level {
434-
VerbosityLevel::Silent => (),
435-
_ => show_error!(
423+
get_metadata(path, follow)
424+
.inspect_err(|e| {
425+
if self.verbosity.level != VerbosityLevel::Silent {
426+
show_error!(
436427
"cannot {} {}: {}",
437428
if follow { "dereference" } else { "access" },
438429
path.quote(),
439-
strip_errno(&e)
440-
),
430+
strip_errno(e)
431+
);
441432
}
442-
None
443-
}
444-
Ok(meta) => Some(meta),
445-
}
433+
})
434+
.ok()
446435
}
447436

448437
#[inline]

0 commit comments

Comments
 (0)