diff --git a/src/uu/tr/src/operation.rs b/src/uu/tr/src/operation.rs index 71089385d..9660e594a 100644 --- a/src/uu/tr/src/operation.rs +++ b/src/uu/tr/src/operation.rs @@ -75,7 +75,7 @@ impl Sequence { Sequence::CharRepeat(c, n) => Box::new(std::iter::repeat(*c).take(*n)), Sequence::Alnum => Box::new(('0'..='9').chain('A'..='Z').chain('a'..='z')), Sequence::Alpha => Box::new(('A'..='Z').chain('a'..='z')), - Sequence::Blank => Box::new(unicode_table::BLANK.into_iter().cloned()), + Sequence::Blank => Box::new(unicode_table::BLANK.iter().cloned()), Sequence::Control => Box::new( (0..=31) .chain(std::iter::once(127)) @@ -113,7 +113,7 @@ impl Sequence { .chain(123..=126) .flat_map(char::from_u32), ), - Sequence::Space => Box::new(unicode_table::SPACES.into_iter().cloned()), + Sequence::Space => Box::new(unicode_table::SPACES.iter().cloned()), Sequence::Upper => Box::new('A'..='Z'), Sequence::Xdigit => Box::new(('0'..='9').chain('A'..='F').chain('a'..='f')), } @@ -129,12 +129,7 @@ impl Sequence { let set1 = Sequence::from_str(set1_str)?; let set2 = Sequence::from_str(set2_str)?; - let is_char_star = |s: &&Sequence| -> bool { - match s { - Sequence::CharStar(_) => true, - _ => false, - } - }; + let is_char_star = |s: &&Sequence| -> bool { matches!(s, Sequence::CharStar(_)) }; let set1_star_count = set1.iter().filter(is_char_star).count(); if set1_star_count == 0 { let set2_star_count = set2.iter().filter(is_char_star).count(); @@ -143,10 +138,9 @@ impl Sequence { Sequence::CharStar(c) => Some(c), _ => None, }); - let mut partition = set2.as_slice().split(|s| match s { - Sequence::CharStar(_) => true, - _ => false, - }); + let mut partition = set2 + .as_slice() + .split(|s| matches!(s, Sequence::CharStar(_))); let set1_len = set1.iter().flat_map(Sequence::flatten).count(); let set2_len = set2 .iter() @@ -199,7 +193,7 @@ impl Sequence { if truncate_set1_flag { set1_solved.truncate(set2_solved.len()); } - return Ok((set1_solved, set2_solved)); + Ok((set1_solved, set2_solved)) } else { Err(BadSequence::MultipleCharRepeatInSet2) } @@ -211,7 +205,7 @@ impl Sequence { impl Sequence { pub fn from_str(input: &str) -> Result, BadSequence> { - let result = many0(alt(( + many0(alt(( alt(( Sequence::parse_char_range, Sequence::parse_char_star, @@ -244,8 +238,7 @@ impl Sequence { .map(|(_, r)| r) .unwrap() .into_iter() - .collect::, _>>(); - result + .collect::, _>>() } fn parse_backslash(input: &str) -> IResult<&str, char> { @@ -442,21 +435,19 @@ pub struct TranslateOperationStandard { impl TranslateOperationStandard { fn new(set1: Vec, set2: Vec) -> Result { - if let Some(fallback) = set2.last().map(|s| *s) { + if let Some(fallback) = set2.last().copied() { Ok(TranslateOperationStandard { translation_map: set1 .into_iter() .zip(set2.into_iter().chain(std::iter::repeat(fallback))) .collect::>(), }) + } else if set1.is_empty() && set2.is_empty() { + Ok(TranslateOperationStandard { + translation_map: HashMap::new(), + }) } else { - if set1.is_empty() && set2.is_empty() { - Ok(TranslateOperationStandard { - translation_map: HashMap::new(), - }) - } else { - Err("when not truncating set1, string2 must be non-empty".to_string()) - } + Err("when not truncating set1, string2 must be non-empty".to_string()) } } } diff --git a/src/uu/tr/src/tr.rs b/src/uu/tr/src/tr.rs index a7faffe56..872f894c2 100644 --- a/src/uu/tr/src/tr.rs +++ b/src/uu/tr/src/tr.rs @@ -101,7 +101,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } if let Some(first) = sets.get(0) { - if first.ends_with(r"\") { + if first.ends_with('\\') { show_error!("warning: an unescaped backslash at end of string is not portable"); } } @@ -130,7 +130,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let mut delete_buffer = vec![]; { let mut delete_writer = BufWriter::new(&mut delete_buffer); - let delete_op = DeleteOperation::new(set1.clone(), complement_flag); + let delete_op = DeleteOperation::new(set1, complement_flag); translate_input(&mut locked_stdin, &mut delete_writer, delete_op); } { @@ -150,7 +150,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let mut translate_buffer = vec![]; { let mut writer = BufWriter::new(&mut translate_buffer); - match TranslateOperation::new(set1.clone(), set2.clone(), complement_flag) { + match TranslateOperation::new(set1, set2.clone(), complement_flag) { Ok(op) => translate_input(&mut locked_stdin, &mut writer, op), Err(s) => { show_error!("{}", s); diff --git a/src/uu/tr/src/unicode_table.rs b/src/uu/tr/src/unicode_table.rs index 1ec6a4fdb..781e4cdba 100644 --- a/src/uu/tr/src/unicode_table.rs +++ b/src/uu/tr/src/unicode_table.rs @@ -6,5 +6,5 @@ pub static VT: char = '\u{000B}'; pub static FF: char = '\u{000C}'; pub static CR: char = '\u{000D}'; pub static SPACE: char = '\u{0020}'; -pub static SPACES: &'static [char] = &[HT, LF, VT, FF, CR, SPACE]; -pub static BLANK: &'static [char] = &[SPACE, HT]; +pub static SPACES: &[char] = &[HT, LF, VT, FF, CR, SPACE]; +pub static BLANK: &[char] = &[SPACE, HT];