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,8 +52,7 @@ 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)));
@ -66,10 +65,6 @@ pub fn parse_inputs(matches: &dyn CommandLineOpts) -> Result<CommandLineInputs,
))); )));
} }
} }
_ => {
// if it cannot be parsed, it is considered a filename
}
}
} }
} }

View file

@ -32,20 +32,16 @@ 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); pm.tokens.push(x);
} }
None => {}
}
if let Some(x) = it.next() { if let Some(x) = it.next() {
it.put_back(x); it.put_back(x);
} else { } else {

View file

@ -53,8 +53,7 @@ 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();
@ -69,8 +68,6 @@ impl Formatter for Decf {
f_sci.post_decimal = Some(String::from(&post_dec[0..i])); f_sci.post_decimal = Some(String::from(&post_dec[0..i]));
} }
} }
None => {}
}
let f_fl = get_primitive_dec( let f_fl = get_primitive_dec(
inprefix, inprefix,
&str_in[inprefix.offset..], &str_in[inprefix.offset..],

View file

@ -185,8 +185,7 @@ 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);
@ -196,8 +195,6 @@ fn round_terminal_digit(
} }
// TODO // TODO
} }
_ => {}
}
} }
(before_dec, after_dec) (before_dec, after_dec)
} }
@ -297,12 +294,9 @@ 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) => {
final_str.push_str(&pre_decimal); final_str.push_str(&pre_decimal);
@ -344,12 +338,9 @@ 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,18 +241,14 @@ 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 {
@ -260,8 +256,6 @@ impl Formatter for Intf {
i -= 1; i -= 1;
} }
} }
None => {}
}
finalstr.push_str(&pre_decimal); finalstr.push_str(&pre_decimal);
} }
None => { None => {

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,12 +142,9 @@ 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,14 +262,11 @@ 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,9 +387,8 @@ 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 {
@ -422,7 +418,5 @@ impl token::Token for Sub {
} }
); );
} }
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,12 +570,9 @@ 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 {
println!("{}: {}: removed", NAME, orig_filename); println!("{}: {}: removed", NAME, orig_filename);

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,8 +110,7 @@ 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,
@ -119,8 +118,6 @@ impl<'a> FileMerger<'a> {
}; };
self.heap.push(mergeable_file); self.heap.push(mergeable_file);
} }
_ => {}
}
} }
} }
@ -522,13 +519,10 @@ 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,8 +108,7 @@ 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;
@ -117,8 +116,6 @@ size is 1000, and default PREFIX is 'x'. With no INPUT, or when INPUT is
crash!(1, "{}: cannot split in more than one way", NAME) crash!(1, "{}: cannot split in more than one way", NAME)
} }
} }
None => {}
}
} }
let mut v = matches.free.iter(); let mut v = matches.free.iter();

View file

@ -369,13 +369,10 @@ 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);
if chars[j] == '.' { if chars[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();
match parsed { if let Some(m) = parsed {
Some(m) => settings.sleep_msec = m * 1000, 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,8 +153,7 @@ 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;
@ -171,8 +166,6 @@ pub fn uumain(args: Vec<String>) -> i32 {
return 1; return 1;
} }
} }
}
None => {}
}, },
}; };