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"]) {
// test if the last input can be parsed as an offset.
let offset = parse_offset_operand(&input_strings[input_strings.len() - 1]);
match offset {
Ok(n) => {
// if there is just 1 input (stdin), an offset must start with '+'
if input_strings.len() == 1 && input_strings[0].starts_with('+') {
return Ok(CommandLineInputs::FileAndOffset(("-".to_string(), n, None)));
}
if input_strings.len() == 2 {
return Ok(CommandLineInputs::FileAndOffset((
input_strings[0].clone(),
n,
None,
)));
}
if let Ok(n) = offset {
// if there is just 1 input (stdin), an offset must start with '+'
if input_strings.len() == 1 && input_strings[0].starts_with('+') {
return Ok(CommandLineInputs::FileAndOffset(("-".to_string(), n, None)));
}
_ => {
// if it cannot be parsed, it is considered a filename
if input_strings.len() == 2 {
return Ok(CommandLineInputs::FileAndOffset((
input_strings[0].clone(),
n,
None,
)));
}
}
}

View file

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

View file

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

View file

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

View file

@ -241,26 +241,20 @@ impl Formatter for Intf {
}
fn primitive_to_str(&self, prim: &FormatPrimitive, field: FormatField) -> String {
let mut finalstr: String = String::new();
match prim.prefix {
Some(ref prefix) => {
finalstr.push_str(&prefix);
}
None => {}
if let Some(ref prefix) = prim.prefix {
finalstr.push_str(&prefix);
}
// integral second fields is zero-padded minimum-width
// which gets handled before general minimum-width
match prim.pre_decimal {
Some(ref pre_decimal) => {
match field.second_field {
Some(min) => {
let mut i = min;
let len = pre_decimal.len() as u32;
while i > len {
finalstr.push('0');
i -= 1;
}
if let Some(min) = field.second_field {
let mut i = min;
let len = pre_decimal.len() as u32;
while i > len {
finalstr.push('0');
i -= 1;
}
None => {}
}
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>) {
match env::var("POSIXLY_CORRECT") {
Ok(_) => {}
Err(e) => match e {
env::VarError::NotPresent => {
cli::err_msg(&format!(
"warning: {:?}: character(s) following character \
constant have been ignored",
&*remaining_bytes
));
}
_ => {}
Err(e) => if let env::VarError::NotPresent = e {
cli::err_msg(&format!(
"warning: {:?}: character(s) following character \
constant have been ignored",
&*remaining_bytes
));
},
}
}
@ -145,11 +142,8 @@ fn get_inprefix(str_in: &str, field_type: &FieldType) -> InPrefix {
}
e @ '0'..='9' => {
ret.offset += 1;
match *field_type {
FieldType::Intf => {
ret.radix_in = Base::Octal;
}
_ => {}
if let FieldType::Intf = *field_type {
ret.radix_in = Base::Octal;
}
if e == '0' {
do_clean_lead_zeroes = true;

View file

@ -262,13 +262,10 @@ impl SubParser {
self.validate_field_params(field_char_retrieved);
// if the dot is provided without a second field
// printf interprets it as 0.
match self.second_field_tmp.as_mut() {
Some(x) => {
if x.len() == 0 {
self.min_width_tmp = Some(String::from("0"));
}
if let Some(x) = self.second_field_tmp.as_mut() {
if x.is_empty() {
self.min_width_tmp = Some(String::from("0"));
}
_ => {}
}
true
@ -390,39 +387,36 @@ impl token::Token for Sub {
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.
Some(pre_min_width) => {
print!(
"{}",
match field.min_width {
Some(min_width) => {
let diff: isize =
min_width.abs() as isize - pre_min_width.len() as isize;
if diff > 0 {
let mut final_str = String::new();
// definitely more efficient ways
// to do this.
let pad_before = min_width > 0;
if !pad_before {
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
print!(
"{}",
match field.min_width {
Some(min_width) => {
let diff: isize =
min_width.abs() as isize - pre_min_width.len() as isize;
if diff > 0 {
let mut final_str = String::new();
// definitely more efficient ways
// to do this.
let pad_before = min_width > 0;
if !pad_before {
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
}
None => pre_min_width,
}
);
}
None => {}
None => pre_min_width,
}
);
}
}
}

View file

@ -232,9 +232,10 @@ impl UnescapedText {
new_vec.extend(tmp_str.bytes());
}
}
match addchar {
true => Some(Box::new(new_text)),
false => None,
if addchar {
Some(Box::new(new_text))
} 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);
match renamed_path {
Some(rp) => {
fs::remove_file(rp)?;
}
None => (),
if let Some(rp) = renamed_path {
fs::remove_file(rp)?;
}
if verbose {

View file

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

View file

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

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();
let strategies = vec!["b", "C", "l"];
for e in &strategies {
match matches.opt_str(*e) {
Some(a) => {
if settings.strategy == "l" {
settings.strategy = (*e).to_owned();
settings.strategy_param = a;
} else {
crash!(1, "{}: cannot split in more than one way", NAME)
}
if let Some(a) = matches.opt_str(*e) {
if settings.strategy == "l" {
settings.strategy = (*e).to_owned();
settings.strategy_param = a;
} else {
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 j = i;
match fmtstr[j..].scan_num::<usize>() {
Some((field_width, offset)) => {
width = field_width;
j += offset;
}
None => (),
if let Some((field_width, offset)) = fmtstr[j..].scan_num::<usize>() {
width = field_width;
j += offset;
}
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();
match file.read_to_end(&mut data) {
Err(e) => {
show_warning!("failed to read '{}': {}", filename, e);
continue;
}
Ok(_) => (),
if let Err(e) = file.read_to_end(&mut data) {
show_warning!("failed to read '{}': {}", filename, e);
continue;
};
// 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");
if settings.follow {
match given_options.opt_str("s") {
Some(n) => {
let parsed: Option<u32> = n.parse().ok();
match parsed {
Some(m) => settings.sleep_msec = m * 1000,
None => {}
}
if let Some(n) = given_options.opt_str("s") {
let parsed: Option<u32> = n.parse().ok();
if let Some(m) = parsed {
settings.sleep_msec = m * 1000
}
None => {}
};
}
}
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") {
Some(n) => {
let mut slice: &str = n.as_ref();
if slice.chars().next().unwrap_or('_') == '+' {
settings.beginning = true;
slice = &slice[1..];
}
match parse_size(slice) {
Ok(m) => settings.mode = FilterMode::Bytes(m),
Err(e) => {
show_error!("{}", e.description());
return 1;
}
None => if let Some(n) = given_options.opt_str("c") {
let mut slice: &str = n.as_ref();
if slice.chars().next().unwrap_or('_') == '+' {
settings.beginning = true;
slice = &slice[1..];
}
match parse_size(slice) {
Ok(m) => settings.mode = FilterMode::Bytes(m),
Err(e) => {
show_error!("{}", e.description());
return 1;
}
}
None => {}
},
};