1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-04 15:07:47 +00:00

refactor/polish ~ fix cargo clippy complaints (match => if let)

This commit is contained in:
Roy Ivy III 2019-12-26 11:01:26 -06:00
parent 600c40490b
commit 3bddf84aec
15 changed files with 130 additions and 194 deletions

View file

@ -52,22 +52,17 @@ pub fn parse_inputs(matches: &dyn CommandLineOpts) -> Result<CommandLineInputs,
if !matches.opts_present(&["A", "j", "N", "t", "v", "w"]) { if !matches.opts_present(&["A", "j", "N", "t", "v", "w"]) {
// test if the last input can be parsed as an offset. // test if the last input can be parsed as an offset.
let offset = parse_offset_operand(&input_strings[input_strings.len() - 1]); let offset = parse_offset_operand(&input_strings[input_strings.len() - 1]);
match offset { if let Ok(n) = offset {
Ok(n) => { // if there is just 1 input (stdin), an offset must start with '+'
// if there is just 1 input (stdin), an offset must start with '+' if input_strings.len() == 1 && input_strings[0].starts_with('+') {
if input_strings.len() == 1 && input_strings[0].starts_with('+') { return Ok(CommandLineInputs::FileAndOffset(("-".to_string(), n, None)));
return Ok(CommandLineInputs::FileAndOffset(("-".to_string(), n, None)));
}
if input_strings.len() == 2 {
return Ok(CommandLineInputs::FileAndOffset((
input_strings[0].clone(),
n,
None,
)));
}
} }
_ => { if input_strings.len() == 2 {
// if it cannot be parsed, it is considered a filename return Ok(CommandLineInputs::FileAndOffset((
input_strings[0].clone(),
n,
None,
)));
} }
} }
} }

View file

@ -32,19 +32,15 @@ impl Memo {
let mut has_sub = false; let mut has_sub = false;
loop { loop {
tmp_token = UnescapedText::from_it(&mut it, pf_args_it); tmp_token = UnescapedText::from_it(&mut it, pf_args_it);
match tmp_token { if let Some(x) = tmp_token {
Some(x) => pm.tokens.push(x), pm.tokens.push(x);
None => {}
} }
tmp_token = Sub::from_it(&mut it, pf_args_it); tmp_token = Sub::from_it(&mut it, pf_args_it);
match tmp_token { if let Some(x) = tmp_token {
Some(x) => { if !has_sub {
if !has_sub { has_sub = true;
has_sub = true;
}
pm.tokens.push(x);
} }
None => {} pm.tokens.push(x);
} }
if let Some(x) = it.next() { if let Some(x) = it.next() {
it.put_back(x); it.put_back(x);

View file

@ -53,23 +53,20 @@ impl Formatter for Decf {
Some(*field.field_char == 'G'), Some(*field.field_char == 'G'),
); );
// strip trailing zeroes // strip trailing zeroes
match f_sci.post_decimal.clone() { if let Some(ref post_dec) = f_sci.post_decimal.clone() {
Some(ref post_dec) => { let mut i = post_dec.len();
let mut i = post_dec.len(); {
{ let mut it = post_dec.chars();
let mut it = post_dec.chars(); while let Some(c) = it.next_back() {
while let Some(c) = it.next_back() { if c != '0' {
if c != '0' { break;
break;
}
i -= 1;
} }
} i -= 1;
if i != post_dec.len() {
f_sci.post_decimal = Some(String::from(&post_dec[0..i]));
} }
} }
None => {} if i != post_dec.len() {
f_sci.post_decimal = Some(String::from(&post_dec[0..i]));
}
} }
let f_fl = get_primitive_dec( let f_fl = get_primitive_dec(
inprefix, inprefix,

View file

@ -185,18 +185,15 @@ fn round_terminal_digit(
.next() .next()
.expect(""); .expect("");
} }
match digit_at_pos { if let '5'..='9' = digit_at_pos {
'5'..='9' => { let (new_after_dec, finished_in_dec) = _round_str_from(&after_dec, position);
let (new_after_dec, finished_in_dec) = _round_str_from(&after_dec, position); if finished_in_dec {
if finished_in_dec { return (before_dec, new_after_dec);
return (before_dec, new_after_dec); } else {
} else { let (new_before_dec, _) = _round_str_from(&before_dec, before_dec.len());
let (new_before_dec, _) = _round_str_from(&before_dec, before_dec.len()); return (new_before_dec, new_after_dec);
return (new_before_dec, new_after_dec);
}
// TODO
} }
_ => {} // TODO
} }
} }
(before_dec, after_dec) (before_dec, after_dec)
@ -297,11 +294,8 @@ pub fn get_primitive_dec(
pub fn primitive_to_str_common(prim: &FormatPrimitive, field: &FormatField) -> String { pub fn primitive_to_str_common(prim: &FormatPrimitive, field: &FormatField) -> String {
let mut final_str = String::new(); let mut final_str = String::new();
match prim.prefix { if let Some(ref prefix) = prim.prefix {
Some(ref prefix) => { final_str.push_str(&prefix);
final_str.push_str(&prefix);
}
None => {}
} }
match prim.pre_decimal { match prim.pre_decimal {
Some(ref pre_decimal) => { Some(ref pre_decimal) => {
@ -344,11 +338,8 @@ pub fn primitive_to_str_common(prim: &FormatPrimitive, field: &FormatField) -> S
); );
} }
} }
match prim.suffix { if let Some(ref suffix) = prim.suffix {
Some(ref suffix) => { final_str.push_str(suffix);
final_str.push_str(suffix);
}
None => {}
} }
final_str final_str

View file

@ -241,26 +241,20 @@ impl Formatter for Intf {
} }
fn primitive_to_str(&self, prim: &FormatPrimitive, field: FormatField) -> String { fn primitive_to_str(&self, prim: &FormatPrimitive, field: FormatField) -> String {
let mut finalstr: String = String::new(); let mut finalstr: String = String::new();
match prim.prefix { if let Some(ref prefix) = prim.prefix {
Some(ref prefix) => { finalstr.push_str(&prefix);
finalstr.push_str(&prefix);
}
None => {}
} }
// integral second fields is zero-padded minimum-width // integral second fields is zero-padded minimum-width
// which gets handled before general minimum-width // which gets handled before general minimum-width
match prim.pre_decimal { match prim.pre_decimal {
Some(ref pre_decimal) => { Some(ref pre_decimal) => {
match field.second_field { if let Some(min) = field.second_field {
Some(min) => { let mut i = min;
let mut i = min; let len = pre_decimal.len() as u32;
let len = pre_decimal.len() as u32; while i > len {
while i > len { finalstr.push('0');
finalstr.push('0'); i -= 1;
i -= 1;
}
} }
None => {}
} }
finalstr.push_str(&pre_decimal); finalstr.push_str(&pre_decimal);
} }

View file

@ -21,15 +21,12 @@ pub fn warn_expected_numeric(pf_arg: &String) {
fn warn_char_constant_ign(remaining_bytes: Vec<u8>) { fn warn_char_constant_ign(remaining_bytes: Vec<u8>) {
match env::var("POSIXLY_CORRECT") { match env::var("POSIXLY_CORRECT") {
Ok(_) => {} Ok(_) => {}
Err(e) => match e { Err(e) => if let env::VarError::NotPresent = e {
env::VarError::NotPresent => { cli::err_msg(&format!(
cli::err_msg(&format!( "warning: {:?}: character(s) following character \
"warning: {:?}: character(s) following character \ constant have been ignored",
constant have been ignored", &*remaining_bytes
&*remaining_bytes ));
));
}
_ => {}
}, },
} }
} }
@ -145,11 +142,8 @@ fn get_inprefix(str_in: &str, field_type: &FieldType) -> InPrefix {
} }
e @ '0'..='9' => { e @ '0'..='9' => {
ret.offset += 1; ret.offset += 1;
match *field_type { if let FieldType::Intf = *field_type {
FieldType::Intf => { ret.radix_in = Base::Octal;
ret.radix_in = Base::Octal;
}
_ => {}
} }
if e == '0' { if e == '0' {
do_clean_lead_zeroes = true; do_clean_lead_zeroes = true;

View file

@ -262,13 +262,10 @@ impl SubParser {
self.validate_field_params(field_char_retrieved); self.validate_field_params(field_char_retrieved);
// if the dot is provided without a second field // if the dot is provided without a second field
// printf interprets it as 0. // printf interprets it as 0.
match self.second_field_tmp.as_mut() { if let Some(x) = self.second_field_tmp.as_mut() {
Some(x) => { if x.is_empty() {
if x.len() == 0 { self.min_width_tmp = Some(String::from("0"));
self.min_width_tmp = Some(String::from("0"));
}
} }
_ => {}
} }
true true
@ -390,39 +387,36 @@ impl token::Token for Sub {
num_format::num_format(&field, pf_arg) num_format::num_format(&field, pf_arg)
} }
}; };
match pre_min_width_opt { if let Some(pre_min_width) = pre_min_width_opt {
// if have a string, print it, ensuring minimum width is met. // if have a string, print it, ensuring minimum width is met.
Some(pre_min_width) => { print!(
print!( "{}",
"{}", match field.min_width {
match field.min_width { Some(min_width) => {
Some(min_width) => { let diff: isize =
let diff: isize = min_width.abs() as isize - pre_min_width.len() as isize;
min_width.abs() as isize - pre_min_width.len() as isize; if diff > 0 {
if diff > 0 { let mut final_str = String::new();
let mut final_str = String::new(); // definitely more efficient ways
// definitely more efficient ways // to do this.
// to do this. let pad_before = min_width > 0;
let pad_before = min_width > 0; if !pad_before {
if !pad_before { final_str.push_str(&pre_min_width);
final_str.push_str(&pre_min_width);
}
for _ in 0..diff {
final_str.push(' ');
}
if pad_before {
final_str.push_str(&pre_min_width);
}
final_str
} else {
pre_min_width
} }
for _ in 0..diff {
final_str.push(' ');
}
if pad_before {
final_str.push_str(&pre_min_width);
}
final_str
} else {
pre_min_width
} }
None => pre_min_width,
} }
); None => pre_min_width,
} }
None => {} );
} }
} }
} }

View file

@ -232,9 +232,10 @@ impl UnescapedText {
new_vec.extend(tmp_str.bytes()); new_vec.extend(tmp_str.bytes());
} }
} }
match addchar { if addchar {
true => Some(Box::new(new_text)), Some(Box::new(new_text))
false => None, } else {
None
} }
} }
} }

View file

@ -570,11 +570,8 @@ fn do_remove(path: &Path, orig_filename: &str, verbose: bool) -> Result<(), io::
} }
let renamed_path: Option<PathBuf> = wipe_name(&path, verbose); let renamed_path: Option<PathBuf> = wipe_name(&path, verbose);
match renamed_path { if let Some(rp) = renamed_path {
Some(rp) => { fs::remove_file(rp)?;
fs::remove_file(rp)?;
}
None => (),
} }
if verbose { if verbose {

View file

@ -156,9 +156,8 @@ fn read_input_file(filename: &str) -> Vec<u8> {
}); });
let mut data = Vec::new(); let mut data = Vec::new();
match file.read_to_end(&mut data) { if let Err(e) = file.read_to_end(&mut data) {
Err(e) => crash!(1, "failed reading '{}': {}", filename, e), crash!(1, "failed reading '{}': {}", filename, e)
Ok(_) => (),
}; };
data data

View file

@ -110,16 +110,13 @@ impl<'a> FileMerger<'a> {
} }
} }
fn push_file(&mut self, mut lines: Lines<BufReader<Box<dyn Read>>>) { fn push_file(&mut self, mut lines: Lines<BufReader<Box<dyn Read>>>) {
match lines.next() { if let Some(Ok(next_line)) = lines.next() {
Some(Ok(next_line)) => { let mergeable_file = MergeableFile {
let mergeable_file = MergeableFile { lines,
lines, current_line: next_line,
current_line: next_line, settings: &self.settings,
settings: &self.settings, };
}; self.heap.push(mergeable_file);
self.heap.push(mergeable_file);
}
_ => {}
} }
} }
} }
@ -522,12 +519,9 @@ where
for line in iter { for line in iter {
let str = format!("{}\n", line); let str = format!("{}\n", line);
match file.write_all(str.as_bytes()) { if let Err(e) = file.write_all(str.as_bytes()) {
Err(e) => { show_error!("sort: {0}", e.to_string());
show_error!("sort: {0}", e.to_string()); panic!("Write failed");
panic!("Write failed");
}
Ok(_) => (),
} }
} }
} }

View file

@ -108,16 +108,13 @@ size is 1000, and default PREFIX is 'x'. With no INPUT, or when INPUT is
settings.strategy_param = "1000".to_owned(); settings.strategy_param = "1000".to_owned();
let strategies = vec!["b", "C", "l"]; let strategies = vec!["b", "C", "l"];
for e in &strategies { for e in &strategies {
match matches.opt_str(*e) { if let Some(a) = matches.opt_str(*e) {
Some(a) => { if settings.strategy == "l" {
if settings.strategy == "l" { settings.strategy = (*e).to_owned();
settings.strategy = (*e).to_owned(); settings.strategy_param = a;
settings.strategy_param = a; } else {
} else { crash!(1, "{}: cannot split in more than one way", NAME)
crash!(1, "{}: cannot split in more than one way", NAME)
}
} }
None => {}
} }
} }

View file

@ -369,12 +369,9 @@ impl Stater {
let mut precision = -1_i32; let mut precision = -1_i32;
let mut j = i; let mut j = i;
match fmtstr[j..].scan_num::<usize>() { if let Some((field_width, offset)) = fmtstr[j..].scan_num::<usize>() {
Some((field_width, offset)) => { width = field_width;
width = field_width; j += offset;
j += offset;
}
None => (),
} }
check_bound!(fmtstr, bound, old, j); check_bound!(fmtstr, bound, old, j);

View file

@ -103,12 +103,9 @@ fn tac(filenames: Vec<String>, before: bool, _: bool, separator: &str) {
}); });
let mut data = Vec::new(); let mut data = Vec::new();
match file.read_to_end(&mut data) { if let Err(e) = file.read_to_end(&mut data) {
Err(e) => { show_warning!("failed to read '{}': {}", filename, e);
show_warning!("failed to read '{}': {}", filename, e); continue;
continue;
}
Ok(_) => (),
}; };
// find offsets in string of all separators // find offsets in string of all separators

View file

@ -114,16 +114,12 @@ pub fn uumain(args: Vec<String>) -> i32 {
settings.follow = given_options.opt_present("f"); settings.follow = given_options.opt_present("f");
if settings.follow { if settings.follow {
match given_options.opt_str("s") { if let Some(n) = given_options.opt_str("s") {
Some(n) => { let parsed: Option<u32> = n.parse().ok();
let parsed: Option<u32> = n.parse().ok(); if let Some(m) = parsed {
match parsed { settings.sleep_msec = m * 1000
Some(m) => settings.sleep_msec = m * 1000,
None => {}
}
} }
None => {} }
};
} }
if let Some(pid_str) = given_options.opt_str("pid") { if let Some(pid_str) = given_options.opt_str("pid") {
@ -157,22 +153,19 @@ pub fn uumain(args: Vec<String>) -> i32 {
} }
} }
} }
None => match given_options.opt_str("c") { None => if let Some(n) = given_options.opt_str("c") {
Some(n) => { let mut slice: &str = n.as_ref();
let mut slice: &str = n.as_ref(); if slice.chars().next().unwrap_or('_') == '+' {
if slice.chars().next().unwrap_or('_') == '+' { settings.beginning = true;
settings.beginning = true; slice = &slice[1..];
slice = &slice[1..]; }
} match parse_size(slice) {
match parse_size(slice) { Ok(m) => settings.mode = FilterMode::Bytes(m),
Ok(m) => settings.mode = FilterMode::Bytes(m), Err(e) => {
Err(e) => { show_error!("{}", e.description());
show_error!("{}", e.description()); return 1;
return 1;
}
} }
} }
None => {}
}, },
}; };