From e505047688628f5254e4f3ef2075baae8455611c Mon Sep 17 00:00:00 2001 From: crapStone Date: Sat, 22 Jul 2023 22:26:36 +0200 Subject: [PATCH] fix clippy warnings --- src/lib.rs | 101 ++++++++++++++++++++++++++++------------------------- 1 file changed, 54 insertions(+), 47 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ab745d6..3496d67 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -88,40 +88,35 @@ pub enum EscapePolicy { impl EscapePolicy { fn escape_basics(self) -> bool { - match self { - EscapePolicy::Nothing => false, - _ => true, - } + self != EscapePolicy::Nothing } fn escape_reserved(self) -> bool { - match self { - EscapePolicy::Reserved => true, - EscapePolicy::ReservedUnicode => true, - EscapePolicy::ReservedUnicodeExtended => true, - EscapePolicy::Everything => true, - _ => false, - } + matches!( + self, + EscapePolicy::Reserved + | EscapePolicy::ReservedUnicode + | EscapePolicy::ReservedUnicodeExtended + | EscapePolicy::Everything + ) } fn escape_unicode(self) -> bool { - match self { - EscapePolicy::BasicsUnicode => true, - EscapePolicy::BasicsUnicodeExtended => true, - EscapePolicy::ReservedUnicode => true, - EscapePolicy::ReservedUnicodeExtended => true, - EscapePolicy::Everything => true, - _ => false, - } + matches!( + self, + EscapePolicy::BasicsUnicode + | EscapePolicy::BasicsUnicodeExtended + | EscapePolicy::ReservedUnicode + | EscapePolicy::ReservedUnicodeExtended + | EscapePolicy::Everything + ) } fn escape_unicode_extended(self) -> bool { - match self { - EscapePolicy::BasicsUnicodeExtended => true, - EscapePolicy::ReservedUnicodeExtended => true, - EscapePolicy::Everything => true, - _ => false, - } + matches!( + self, + EscapePolicy::BasicsUnicodeExtended | EscapePolicy::ReservedUnicodeExtended | EscapePolicy::Everything + ) } /// Given a character this returns true if it should be escaped as @@ -450,7 +445,7 @@ impl Properties { } /// Remove the property with all values with the same key - pub fn remove_all<'a, S: AsRef>(&'a mut self, s: S) -> impl DoubleEndedIterator + 'a { + pub fn remove_all>(&mut self, s: S) -> impl DoubleEndedIterator + '_ { self.data.remove_all(property_get_key!(s.as_ref())) } @@ -813,8 +808,8 @@ impl Ini { if let Some(props) = self.sections.get(&None) { for (k, v) in props.iter() { - let k_str = escape_str(&k[..], opt.escape_policy); - let v_str = escape_str(&v[..], opt.escape_policy); + let k_str = escape_str(k, opt.escape_policy); + let v_str = escape_str(v, opt.escape_policy); write!(writer, "{}={}{}", k_str, v_str, opt.line_separator)?; firstline = false; @@ -838,8 +833,8 @@ impl Ini { )?; for (k, v) in props.iter() { - let k_str = escape_str(&k[..], opt.escape_policy); - let v_str = escape_str(&v[..], opt.escape_policy); + let k_str = escape_str(k, opt.escape_policy); + let v_str = escape_str(v, opt.escape_policy); write!(writer, "{}{}{}{}", k_str, opt.kv_separator, v_str, opt.line_separator)?; } } @@ -928,10 +923,8 @@ impl Ini { // Check if file starts with a BOM marker // UTF-8: EF BB BF let mut bom = [0u8; 3]; - if let Ok(..) = reader.read_exact(&mut bom) { - if &bom == b"\xEF\xBB\xBF" { - with_bom = true; - } + if reader.read_exact(&mut bom).is_ok() && &bom == b"\xEF\xBB\xBF" { + with_bom = true; } if !with_bom { @@ -1182,7 +1175,7 @@ impl<'a> Parser<'a> { Err(e) => return Err(e), }, '=' | ':' => { - if (&curkey[..]).is_empty() { + if (curkey[..]).is_empty() { return self.error("missing key"); } match self.parse_val() { @@ -1571,7 +1564,7 @@ mod test { ..Default::default() }, ); - assert!(!ini.is_ok()); + assert!(ini.is_err()); let err = ini.unwrap_err(); assert_eq!(err.line, 2); @@ -2320,7 +2313,7 @@ c = d let policy = EscapePolicy::Nothing; assert_eq!(escape_str(test_str, policy), test_str); } - + #[test] fn escape_str_basics() { let test_backslash = r"\backslashes\"; @@ -2333,14 +2326,21 @@ c = d assert_eq!(escape_str(test_controls, EscapePolicy::Nothing), test_controls); assert_eq!(escape_str(test_whitespace, EscapePolicy::Nothing), test_whitespace); - for policy in vec![ - EscapePolicy::Basics, EscapePolicy::BasicsUnicode, EscapePolicy::BasicsUnicodeExtended, - EscapePolicy::Reserved, EscapePolicy::ReservedUnicode, EscapePolicy::ReservedUnicodeExtended, + for policy in [ + EscapePolicy::Basics, + EscapePolicy::BasicsUnicode, + EscapePolicy::BasicsUnicodeExtended, + EscapePolicy::Reserved, + EscapePolicy::ReservedUnicode, + EscapePolicy::ReservedUnicodeExtended, EscapePolicy::Everything, ] { assert_eq!(escape_str(test_backslash, policy), r"\\backslashes\\"); assert_eq!(escape_str(test_nul, policy), r"string with \0nulls\0 in it"); - assert_eq!(escape_str(test_controls, policy), r"|\a| bell, |\b| backspace, |\x007f| delete, |\x001b| escape"); + assert_eq!( + escape_str(test_controls, policy), + r"|\a| bell, |\b| backspace, |\x007f| delete, |\x001b| escape" + ); assert_eq!(escape_str(test_whitespace, policy), r"\t \r\n"); } } @@ -2353,17 +2353,21 @@ c = d let test_punctuation = "!@$%^&*()-_+/?.>,<[]{}``"; // These policies should *not* escape reserved characters. - for policy in vec![ + for policy in [ EscapePolicy::Nothing, - EscapePolicy::Basics, EscapePolicy::BasicsUnicode, EscapePolicy::BasicsUnicodeExtended, + EscapePolicy::Basics, + EscapePolicy::BasicsUnicode, + EscapePolicy::BasicsUnicodeExtended, ] { assert_eq!(escape_str(test_reserved, policy), ":=;#"); assert_eq!(escape_str(test_punctuation, policy), test_punctuation); } // These should. - for policy in vec![ - EscapePolicy::Reserved, EscapePolicy::ReservedUnicodeExtended, EscapePolicy::ReservedUnicode, + for policy in [ + EscapePolicy::Reserved, + EscapePolicy::ReservedUnicodeExtended, + EscapePolicy::ReservedUnicode, EscapePolicy::Everything, ] { assert_eq!(escape_str(test_reserved, policy), r"\:\=\;\#"); @@ -2389,7 +2393,7 @@ c = d // The "Unicode" policies should escape standard BMP unicode, but should *not* escape emoji or supplementary CJK codepoints. // The Basics/Reserved policies should behave identically in this regard. - for policy in vec![EscapePolicy::BasicsUnicode, EscapePolicy::ReservedUnicode] { + for policy in [EscapePolicy::BasicsUnicode, EscapePolicy::ReservedUnicode] { assert_eq!(escape_str(test_unicode, policy), r"\x00e9\x00a3\x2233\x5b57\x2728"); assert_eq!(escape_str(test_emoji, policy), test_emoji); assert_eq!(escape_str(test_cjk, policy), test_cjk); @@ -2397,7 +2401,10 @@ c = d } // UnicodeExtended policies should escape both BMP and supplementary plane characters. - for policy in vec![EscapePolicy::BasicsUnicodeExtended, EscapePolicy::ReservedUnicodeExtended] { + for policy in [ + EscapePolicy::BasicsUnicodeExtended, + EscapePolicy::ReservedUnicodeExtended, + ] { assert_eq!(escape_str(test_unicode, policy), r"\x00e9\x00a3\x2233\x5b57\x2728"); assert_eq!(escape_str(test_emoji, policy), r"\x1f431\x1f609"); assert_eq!(escape_str(test_cjk, policy), r"\x2020c\x20547");