diff --git a/examples/s_expression.rs b/examples/s_expression.rs index d3abbeb06..222ee40bd 100644 --- a/examples/s_expression.rs +++ b/examples/s_expression.rs @@ -19,7 +19,7 @@ use nom::{ /// In this case, we want something tree-like /// Starting from the most basic, we define some built-in functions that our lisp has -#[derive(Debug, PartialEq, Clone, Copy)] +#[derive(Debug, Eq, PartialEq, Clone, Copy)] pub enum BuiltIn { Plus, Minus, @@ -32,7 +32,7 @@ pub enum BuiltIn { /// We now wrap this type and a few other primitives into our Atom type. /// Remember from before that Atoms form one half of our language. -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, Eq, PartialEq, Clone)] pub enum Atom { Num(i32), Keyword(String), @@ -50,7 +50,7 @@ pub enum Atom { /// structure that we can deal with programmatically. Thus any valid expression /// is also a valid data structure in Lisp itself. -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, Eq, PartialEq, Clone)] pub enum Expr { Constant(Atom), /// (func-name arg1 arg2) @@ -65,7 +65,7 @@ pub enum Expr { /// Continuing the trend of starting from the simplest piece and building up, /// we start by creating a parser for the built-in operator functions. -fn parse_builtin_op<'a>(i: &'a str) -> IResult<&'a str, BuiltIn, VerboseError<&'a str>> { +fn parse_builtin_op(i: &str) -> IResult<&str, BuiltIn, VerboseError<&str>> { // one_of matches one of the characters we give it let (i, t) = one_of("+-*/=")(i)?; @@ -84,7 +84,7 @@ fn parse_builtin_op<'a>(i: &'a str) -> IResult<&'a str, BuiltIn, VerboseError<&' )) } -fn parse_builtin<'a>(i: &'a str) -> IResult<&'a str, BuiltIn, VerboseError<&'a str>> { +fn parse_builtin(i: &str) -> IResult<&str, BuiltIn, VerboseError<&str>> { // alt gives us the result of first parser that succeeds, of the series of // parsers we give it alt(( @@ -96,7 +96,7 @@ fn parse_builtin<'a>(i: &'a str) -> IResult<&'a str, BuiltIn, VerboseError<&'a s } /// Our boolean values are also constant, so we can do it the same way -fn parse_bool<'a>(i: &'a str) -> IResult<&'a str, Atom, VerboseError<&'a str>> { +fn parse_bool(i: &str) -> IResult<&str, Atom, VerboseError<&str>> { alt(( map(tag("#t"), |_| Atom::Boolean(true)), map(tag("#f"), |_| Atom::Boolean(false)), @@ -109,7 +109,7 @@ fn parse_bool<'a>(i: &'a str) -> IResult<&'a str, Atom, VerboseError<&'a str>> { /// /// Put plainly: `preceded(tag(":"), cut(alpha1))` means that once we see the `:` /// character, we have to see one or more alphabetic chararcters or the input is invalid. -fn parse_keyword<'a>(i: &'a str) -> IResult<&'a str, Atom, VerboseError<&'a str>> { +fn parse_keyword(i: &str) -> IResult<&str, Atom, VerboseError<&str>> { map( context("keyword", preceded(tag(":"), cut(alpha1))), |sym_str: &str| Atom::Keyword(sym_str.to_string()), @@ -118,20 +118,20 @@ fn parse_keyword<'a>(i: &'a str) -> IResult<&'a str, Atom, VerboseError<&'a str> /// Next up is number parsing. We're keeping it simple here by accepting any number (> 1) /// of digits but ending the program if it doesn't fit into an i32. -fn parse_num<'a>(i: &'a str) -> IResult<&'a str, Atom, VerboseError<&'a str>> { +fn parse_num(i: &str) -> IResult<&str, Atom, VerboseError<&str>> { alt(( map_res(digit1, |digit_str: &str| { digit_str.parse::().map(Atom::Num) }), map(preceded(tag("-"), digit1), |digit_str: &str| { - Atom::Num(-1 * digit_str.parse::().unwrap()) + Atom::Num(-digit_str.parse::().unwrap()) }), ))(i) } /// Now we take all these simple parsers and connect them. /// We can now parse half of our language! -fn parse_atom<'a>(i: &'a str) -> IResult<&'a str, Atom, VerboseError<&'a str>> { +fn parse_atom(i: &str) -> IResult<&str, Atom, VerboseError<&str>> { alt(( parse_num, parse_bool, @@ -141,8 +141,8 @@ fn parse_atom<'a>(i: &'a str) -> IResult<&'a str, Atom, VerboseError<&'a str>> { } /// We then add the Expr layer on top -fn parse_constant<'a>(i: &'a str) -> IResult<&'a str, Expr, VerboseError<&'a str>> { - map(parse_atom, |atom| Expr::Constant(atom))(i) +fn parse_constant(i: &str) -> IResult<&str, Expr, VerboseError<&str>> { + map(parse_atom, Expr::Constant)(i) } /// Before continuing, we need a helper function to parse lists. @@ -172,7 +172,7 @@ where /// /// `tuple` is used to sequence parsers together, so we can translate this directly /// and then map over it to transform the output into an `Expr::Application` -fn parse_application<'a>(i: &'a str) -> IResult<&'a str, Expr, VerboseError<&'a str>> { +fn parse_application(i: &str) -> IResult<&str, Expr, VerboseError<&str>> { let application_inner = map(tuple((parse_expr, many0(parse_expr))), |(head, tail)| { Expr::Application(Box::new(head), tail) }); @@ -186,7 +186,7 @@ fn parse_application<'a>(i: &'a str) -> IResult<&'a str, Expr, VerboseError<&'a /// /// In fact, we define our parser as if `Expr::If` was defined with an Option in it, /// we have the `opt` combinator which fits very nicely here. -fn parse_if<'a>(i: &'a str) -> IResult<&'a str, Expr, VerboseError<&'a str>> { +fn parse_if(i: &str) -> IResult<&str, Expr, VerboseError<&str>> { let if_inner = context( "if expression", map( @@ -218,19 +218,19 @@ fn parse_if<'a>(i: &'a str) -> IResult<&'a str, Expr, VerboseError<&'a str>> { /// This example doesn't have the symbol atom, but by adding variables and changing /// the definition of quote to not always be around an S-expression, we'd get them /// naturally. -fn parse_quote<'a>(i: &'a str) -> IResult<&'a str, Expr, VerboseError<&'a str>> { +fn parse_quote(i: &str) -> IResult<&str, Expr, VerboseError<&str>> { // this should look very straight-forward after all we've done: // we find the `'` (quote) character, use cut to say that we're unambiguously // looking for an s-expression of 0 or more expressions, and then parse them map( context("quote", preceded(tag("'"), cut(s_exp(many0(parse_expr))))), - |exprs| Expr::Quote(exprs), + Expr::Quote, )(i) } /// We tie them all together again, making a top-level expression parser! -fn parse_expr<'a>(i: &'a str) -> IResult<&'a str, Expr, VerboseError<&'a str>> { +fn parse_expr(i: &str) -> IResult<&str, Expr, VerboseError<&str>> { preceded( multispace0, alt((parse_constant, parse_application, parse_if, parse_quote)), @@ -291,7 +291,7 @@ fn eval_expression(e: Expr) -> Option { let reduced_head = eval_expression(*head)?; let reduced_tail = tail .into_iter() - .map(|expr| eval_expression(expr)) + .map(eval_expression) .collect::>>()?; if let Expr::Constant(Atom::BuiltIn(bi)) = reduced_head { Some(Expr::Constant(match bi { @@ -361,7 +361,7 @@ fn eval_expression(e: Expr) -> Option { fn eval_from_str(src: &str) -> Result { parse_expr(src) .map_err(|e: nom::Err>| format!("{:#?}", e)) - .and_then(|(_, exp)| eval_expression(exp).ok_or("Eval failed".to_string())) + .and_then(|(_, exp)| eval_expression(exp).ok_or_else(|| "Eval failed".to_string())) } fn main() { diff --git a/examples/string.rs b/examples/string.rs index 3be2d665d..b4015c571 100644 --- a/examples/string.rs +++ b/examples/string.rs @@ -54,7 +54,7 @@ where // the function returns None, map_opt returns an error. In this case, because // not all u32 values are valid unicode code points, we have to fallibly // convert to char with from_u32. - map_opt(parse_u32, |value| std::char::from_u32(value))(input) + map_opt(parse_u32, std::char::from_u32)(input) } /// Parse an escaped character: \n, \t, \r, \u{00AC}, etc. diff --git a/src/branch/tests.rs b/src/branch/tests.rs index ecd44407e..ff0e173fe 100644 --- a/src/branch/tests.rs +++ b/src/branch/tests.rs @@ -12,7 +12,7 @@ use crate::{ }; #[cfg(feature = "alloc")] -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, Eq, PartialEq)] pub struct ErrorStr(String); #[cfg(feature = "alloc")] @@ -114,6 +114,7 @@ fn alt_incomplete() { #[test] fn permutation_test() { + #[allow(clippy::type_complexity)] fn perm(i: &[u8]) -> IResult<&[u8], (&[u8], &[u8], &[u8])> { permutation((tag("abcd"), tag("efg"), tag("hi")))(i) } diff --git a/src/bytes/complete.rs b/src/bytes/complete.rs index a5442b53f..25da0754e 100644 --- a/src/bytes/complete.rs +++ b/src/bytes/complete.rs @@ -327,6 +327,7 @@ where /// assert_eq!(till_colon("12345"), Ok(("", "12345"))); /// assert_eq!(till_colon(""), Ok(("", ""))); /// ``` +#[allow(clippy::redundant_closure)] pub fn take_till>( cond: F, ) -> impl Fn(Input) -> IResult @@ -358,6 +359,7 @@ where /// assert_eq!(till_colon("12345"), Ok(("", "12345"))); /// assert_eq!(till_colon(""), Err(Err::Error(Error::new("", ErrorKind::TakeTill1)))); /// ``` +#[allow(clippy::redundant_closure)] pub fn take_till1>( cond: F, ) -> impl Fn(Input) -> IResult @@ -736,7 +738,7 @@ mod tests { } // issue ##1118 escaped does not work with empty string - fn unquote<'a>(input: &'a str) -> IResult<&'a str, &'a str> { + fn unquote(input: &str) -> IResult<&str, &str> { use crate::bytes::complete::*; use crate::character::complete::*; use crate::combinator::opt; diff --git a/src/bytes/streaming.rs b/src/bytes/streaming.rs index e972760e2..a8207a967 100644 --- a/src/bytes/streaming.rs +++ b/src/bytes/streaming.rs @@ -340,6 +340,7 @@ where /// assert_eq!(till_colon("12345"), Err(Err::Incomplete(Needed::new(1)))); /// assert_eq!(till_colon(""), Err(Err::Incomplete(Needed::new(1)))); /// ``` +#[allow(clippy::redundant_closure)] pub fn take_till>( cond: F, ) -> impl Fn(Input) -> IResult @@ -372,6 +373,7 @@ where /// assert_eq!(till_colon("12345"), Err(Err::Incomplete(Needed::new(1)))); /// assert_eq!(till_colon(""), Err(Err::Incomplete(Needed::new(1)))); /// ``` +#[allow(clippy::redundant_closure)] pub fn take_till1>( cond: F, ) -> impl Fn(Input) -> IResult diff --git a/src/bytes/tests.rs b/src/bytes/tests.rs index 159c4b4ff..4af4eeff0 100644 --- a/src/bytes/tests.rs +++ b/src/bytes/tests.rs @@ -612,11 +612,11 @@ fn case_insensitive() { assert_eq!(test2("ab"), Err(Err::Incomplete(Needed::new(2)))); assert_eq!( test2("Hello"), - Err(Err::Error(error_position!(&"Hello"[..], ErrorKind::Tag))) + Err(Err::Error(error_position!("Hello", ErrorKind::Tag))) ); assert_eq!( test2("Hel"), - Err(Err::Error(error_position!(&"Hel"[..], ErrorKind::Tag))) + Err(Err::Error(error_position!("Hel", ErrorKind::Tag))) ); } diff --git a/src/character/complete.rs b/src/character/complete.rs index 7cb760a68..eccbb4e3a 100644 --- a/src/character/complete.rs +++ b/src/character/complete.rs @@ -897,8 +897,8 @@ mod tests { let e = " "; assert_eq!(alpha1::<_, (_, ErrorKind)>(a), Ok((empty, a))); assert_eq!(alpha1(b), Err(Err::Error((b, ErrorKind::Alpha)))); - assert_eq!(alpha1::<_, (_, ErrorKind)>(c), Ok((&c[1..], &"a"[..]))); - assert_eq!(alpha1::<_, (_, ErrorKind)>(d), Ok(("é12", &"az"[..]))); + assert_eq!(alpha1::<_, (_, ErrorKind)>(c), Ok((&c[1..], "a"))); + assert_eq!(alpha1::<_, (_, ErrorKind)>(d), Ok(("é12", "az"))); assert_eq!(digit1(a), Err(Err::Error((a, ErrorKind::Digit)))); assert_eq!(digit1::<_, (_, ErrorKind)>(b), Ok((empty, b))); assert_eq!(digit1(c), Err(Err::Error((c, ErrorKind::Digit)))); @@ -906,7 +906,7 @@ mod tests { assert_eq!(hex_digit1::<_, (_, ErrorKind)>(a), Ok((empty, a))); assert_eq!(hex_digit1::<_, (_, ErrorKind)>(b), Ok((empty, b))); assert_eq!(hex_digit1::<_, (_, ErrorKind)>(c), Ok((empty, c))); - assert_eq!(hex_digit1::<_, (_, ErrorKind)>(d), Ok(("zé12", &"a"[..]))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(d), Ok(("zé12", "a"))); assert_eq!(hex_digit1(e), Err(Err::Error((e, ErrorKind::HexDigit)))); assert_eq!(oct_digit1(a), Err(Err::Error((a, ErrorKind::OctDigit)))); assert_eq!(oct_digit1::<_, (_, ErrorKind)>(b), Ok((empty, b))); @@ -994,10 +994,7 @@ mod tests { ); let d: &[u8] = b"ab12cd"; - assert_eq!( - not_line_ending::<_, (_, ErrorKind)>(d), - Ok((&[][..], &d[..])) - ); + assert_eq!(not_line_ending::<_, (_, ErrorKind)>(d), Ok((&[][..], d))); } #[test] @@ -1131,7 +1128,7 @@ mod tests { assert_parse!(crlf("\r\na"), Ok(("a", "\r\n"))); assert_parse!( crlf("\r"), - Err(Err::Error(error_position!(&"\r"[..], ErrorKind::CrLf))) + Err(Err::Error(error_position!("\r", ErrorKind::CrLf))) ); assert_parse!( crlf("\ra"), @@ -1156,7 +1153,7 @@ mod tests { assert_parse!(line_ending("\r\na"), Ok(("a", "\r\n"))); assert_parse!( line_ending("\r"), - Err(Err::Error(error_position!(&"\r"[..], ErrorKind::CrLf))) + Err(Err::Error(error_position!("\r", ErrorKind::CrLf))) ); assert_parse!( line_ending("\ra"), diff --git a/src/character/mod.rs b/src/character/mod.rs index 2c5d3bc4a..7ed98e92f 100644 --- a/src/character/mod.rs +++ b/src/character/mod.rs @@ -19,7 +19,7 @@ pub mod streaming; /// ``` #[inline] pub fn is_alphabetic(chr: u8) -> bool { - (chr >= 0x41 && chr <= 0x5A) || (chr >= 0x61 && chr <= 0x7A) + matches!(chr, 0x41..=0x5A | 0x61..=0x7A) } /// Tests if byte is ASCII digit: 0-9 @@ -33,7 +33,7 @@ pub fn is_alphabetic(chr: u8) -> bool { /// ``` #[inline] pub fn is_digit(chr: u8) -> bool { - chr >= 0x30 && chr <= 0x39 + matches!(chr, 0x30..=0x39) } /// Tests if byte is ASCII hex digit: 0-9, A-F, a-f @@ -49,7 +49,7 @@ pub fn is_digit(chr: u8) -> bool { /// ``` #[inline] pub fn is_hex_digit(chr: u8) -> bool { - (chr >= 0x30 && chr <= 0x39) || (chr >= 0x41 && chr <= 0x46) || (chr >= 0x61 && chr <= 0x66) + matches!(chr, 0x30..=0x39 | 0x41..=0x46 | 0x61..=0x66) } /// Tests if byte is ASCII octal digit: 0-7 @@ -64,7 +64,7 @@ pub fn is_hex_digit(chr: u8) -> bool { /// ``` #[inline] pub fn is_oct_digit(chr: u8) -> bool { - chr >= 0x30 && chr <= 0x37 + matches!(chr, 0x30..=0x37) } /// Tests if byte is ASCII alphanumeric: A-Z, a-z, 0-9 diff --git a/src/character/streaming.rs b/src/character/streaming.rs index 88aabba35..eaa25516d 100644 --- a/src/character/streaming.rs +++ b/src/character/streaming.rs @@ -836,8 +836,8 @@ mod tests { Err(Err::Incomplete(Needed::new(1))) ); assert_eq!(alpha1(b), Err(Err::Error((b, ErrorKind::Alpha)))); - assert_eq!(alpha1::<_, (_, ErrorKind)>(c), Ok((&c[1..], &"a"[..]))); - assert_eq!(alpha1::<_, (_, ErrorKind)>(d), Ok(("é12", &"az"[..]))); + assert_eq!(alpha1::<_, (_, ErrorKind)>(c), Ok((&c[1..], "a"))); + assert_eq!(alpha1::<_, (_, ErrorKind)>(d), Ok(("é12", "az"))); assert_eq!(digit1(a), Err(Err::Error((a, ErrorKind::Digit)))); assert_eq!( digit1::<_, (_, ErrorKind)>(b), @@ -857,7 +857,7 @@ mod tests { hex_digit1::<_, (_, ErrorKind)>(c), Err(Err::Incomplete(Needed::new(1))) ); - assert_eq!(hex_digit1::<_, (_, ErrorKind)>(d), Ok(("zé12", &"a"[..]))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(d), Ok(("zé12", "a"))); assert_eq!(hex_digit1(e), Err(Err::Error((e, ErrorKind::HexDigit)))); assert_eq!(oct_digit1(a), Err(Err::Error((a, ErrorKind::OctDigit)))); assert_eq!( diff --git a/src/character/tests.rs b/src/character/tests.rs index 64c2a1c8a..ef3b02a0d 100644 --- a/src/character/tests.rs +++ b/src/character/tests.rs @@ -54,9 +54,9 @@ fn char_str() { char('c')(i) } - let a = &"abcd"[..]; + let a = "abcd"; assert_eq!(f(a), Err(Err::Error(error_position!(a, ErrorKind::Char)))); - let b = &"cde"[..]; - assert_eq!(f(b), Ok((&"de"[..], 'c'))); + let b = "cde"; + assert_eq!(f(b), Ok(("de", 'c'))); } diff --git a/src/combinator/tests.rs b/src/combinator/tests.rs index 15d32b8aa..d209e2b11 100644 --- a/src/combinator/tests.rs +++ b/src/combinator/tests.rs @@ -183,7 +183,7 @@ fn test_verify_alloc() { s == &b"abc"[..] }); - assert_eq!(parser1(&b"abcd"[..]), Ok((&b"d"[..], (&b"abc").to_vec()))); + assert_eq!(parser1(&b"abcd"[..]), Ok((&b"d"[..], b"abc".to_vec()))); assert_eq!( parser1(&b"defg"[..]), Err(Err::Error((&b"defg"[..], ErrorKind::Verify))) diff --git a/src/error.rs b/src/error.rs index 498b5e135..f0bb26a7b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -54,7 +54,7 @@ pub trait FromExternalError { } /// default error type, only contains the error' location and code -#[derive(Debug, PartialEq)] +#[derive(Debug, Eq, PartialEq)] pub struct Error { /// position of the error in the input data pub input: I, @@ -147,7 +147,7 @@ pub fn append_error>(input: I, kind: ErrorKind, other: E) -> /// it can be used to display user friendly error messages #[cfg(feature = "alloc")] #[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))] -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, Eq, PartialEq)] pub struct VerboseError { /// List of errors accumulated by `VerboseError`, containing the affected /// part of input data, and some context @@ -156,7 +156,7 @@ pub struct VerboseError { #[cfg(feature = "alloc")] #[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))] -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, Eq, PartialEq)] /// Error context for `VerboseError` pub enum VerboseErrorKind { /// Static string added by the `context` function diff --git a/src/internal.rs b/src/internal.rs index b7572fbd0..ee3b24582 100644 --- a/src/internal.rs +++ b/src/internal.rs @@ -108,11 +108,7 @@ pub enum Err { impl Err { /// Tests if the result is Incomplete pub fn is_incomplete(&self) -> bool { - if let Err::Incomplete(_) = self { - true - } else { - false - } + matches!(self, Err::Incomplete(..)) } /// Applies the given function to the inner error @@ -317,9 +313,9 @@ pub trait Parser { } } -impl<'a, I, O, E, F> Parser for F +impl Parser for F where - F: FnMut(I) -> IResult + 'a, + F: FnMut(I) -> IResult, { fn parse(&mut self, i: I) -> IResult { self(i) @@ -330,7 +326,7 @@ where use alloc::boxed::Box; #[cfg(feature = "alloc")] -impl<'a, I, O, E> Parser for Box + 'a> { +impl Parser for Box> { fn parse(&mut self, input: I) -> IResult { (**self).parse(input) } @@ -344,7 +340,7 @@ pub struct Map { phantom: core::marker::PhantomData, } -impl<'a, I, O1, O2, E, F: Parser, G: Fn(O1) -> O2> Parser for Map { +impl, G: Fn(O1) -> O2> Parser for Map { fn parse(&mut self, i: I) -> IResult { match self.f.parse(i) { Err(e) => Err(e), @@ -361,7 +357,7 @@ pub struct FlatMap { phantom: core::marker::PhantomData, } -impl<'a, I, O1, O2, E, F: Parser, G: Fn(O1) -> H, H: Parser> Parser +impl, G: Fn(O1) -> H, H: Parser> Parser for FlatMap { fn parse(&mut self, i: I) -> IResult { @@ -378,7 +374,7 @@ pub struct AndThen { phantom: core::marker::PhantomData, } -impl<'a, I, O1, O2, E, F: Parser, G: Parser> Parser +impl, G: Parser> Parser for AndThen { fn parse(&mut self, i: I) -> IResult { @@ -395,9 +391,7 @@ pub struct And { g: G, } -impl<'a, I, O1, O2, E, F: Parser, G: Parser> Parser - for And -{ +impl, G: Parser> Parser for And { fn parse(&mut self, i: I) -> IResult { let (i, o1) = self.f.parse(i)?; let (i, o2) = self.g.parse(i)?; @@ -412,7 +406,7 @@ pub struct Or { g: G, } -impl<'a, I: Clone, O, E: crate::error::ParseError, F: Parser, G: Parser> +impl, F: Parser, G: Parser> Parser for Or { fn parse(&mut self, i: I) -> IResult { @@ -437,7 +431,6 @@ pub struct Into, E1, E2: From> { } impl< - 'a, I: Clone, O1, O2: From, @@ -465,7 +458,7 @@ mod tests { #[macro_export] macro_rules! assert_size ( ($t:ty, $sz:expr) => ( - assert_eq!(crate::lib::std::mem::size_of::<$t>(), $sz); + assert_eq!($crate::lib::std::mem::size_of::<$t>(), $sz); ); ); diff --git a/src/lib.rs b/src/lib.rs index 3beb2f417..1f87eafa0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -372,9 +372,9 @@ //! **Going further:** Read the [guides](https://github.com/Geal/nom/tree/main/doc), //! check out the [recipes]! #![cfg_attr(not(feature = "std"), no_std)] -#![cfg_attr(feature = "cargo-clippy", allow(clippy::doc_markdown))] #![cfg_attr(feature = "docsrs", feature(doc_cfg))] #![cfg_attr(feature = "docsrs", feature(extended_key_value_attributes))] +#![allow(clippy::doc_markdown)] #![deny(missing_docs)] #[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))] #[cfg(feature = "alloc")] diff --git a/src/multi/tests.rs b/src/multi/tests.rs index 96a651817..c62693173 100644 --- a/src/multi/tests.rs +++ b/src/multi/tests.rs @@ -155,6 +155,7 @@ fn many1_test() { #[test] #[cfg(feature = "alloc")] fn many_till_test() { + #[allow(clippy::type_complexity)] fn multi(i: &[u8]) -> IResult<&[u8], (Vec<&[u8]>, &[u8])> { many_till(tag("abcd"), tag("efgh"))(i) } @@ -301,7 +302,7 @@ fn count_zero() { assert_eq!(counter_2(error_2), Ok((error_2_remain, parsed_err_2))); } -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, Eq, PartialEq)] pub struct NilError; impl From<(I, ErrorKind)> for NilError { diff --git a/src/number/complete.rs b/src/number/complete.rs index 98b8b3abf..9c43bec9d 100644 --- a/src/number/complete.rs +++ b/src/number/complete.rs @@ -1503,6 +1503,7 @@ where } } + #[allow(clippy::or_fun_call)] let position = position.unwrap_or(i.input_len()); let index = if zero_count == 0 { @@ -1989,14 +1990,13 @@ mod tests { println!("now parsing: {} -> {}", test, expected32); - let larger = format!("{}", test); - assert_parse!(recognize_float(&larger[..]), Ok(("", test))); + assert_parse!(recognize_float(test), Ok(("", test))); - assert_parse!(float(larger.as_bytes()), Ok((&b""[..], expected32))); - assert_parse!(float(&larger[..]), Ok(("", expected32))); + assert_parse!(float(test.as_bytes()), Ok((&b""[..], expected32))); + assert_parse!(float(test), Ok(("", expected32))); - assert_parse!(double(larger.as_bytes()), Ok((&b""[..], expected64))); - assert_parse!(double(&larger[..]), Ok(("", expected64))); + assert_parse!(double(test.as_bytes()), Ok((&b""[..], expected64))); + assert_parse!(double(test), Ok(("", expected64))); } let remaining_exponent = "-1.234E-"; diff --git a/src/sequence/tests.rs b/src/sequence/tests.rs index 30ad0d678..fec2e812c 100644 --- a/src/sequence/tests.rs +++ b/src/sequence/tests.rs @@ -257,6 +257,7 @@ fn delimited_test() { #[test] fn tuple_test() { + #[allow(clippy::type_complexity)] fn tuple_3(i: &[u8]) -> IResult<&[u8], (u16, &[u8], &[u8])> { tuple((be_u16, take(3u8), tag("fg")))(i) } diff --git a/src/str.rs b/src/str.rs index 1a8b8ba2d..575a7eab3 100644 --- a/src/str.rs +++ b/src/str.rs @@ -168,10 +168,10 @@ mod test { let c = "abcd123"; let d = "123"; - assert_eq!(f(&a[..]), Err(Err::Incomplete(Needed::new(1)))); - assert_eq!(f(&b[..]), Err(Err::Incomplete(Needed::new(1)))); - assert_eq!(f(&c[..]), Ok((&d[..], &b[..]))); - assert_eq!(f(&d[..]), Ok((&d[..], &a[..]))); + assert_eq!(f(a), Err(Err::Incomplete(Needed::new(1)))); + assert_eq!(f(b), Err(Err::Incomplete(Needed::new(1)))); + assert_eq!(f(c), Ok((d, b))); + assert_eq!(f(d), Ok((d, a))); } #[test] @@ -186,12 +186,12 @@ mod test { let c = "abcd123"; let d = "123"; - assert_eq!(f(&a[..]), Err(Err::Incomplete(Needed::new(1)))); - assert_eq!(f(&b[..]), Err(Err::Incomplete(Needed::new(1)))); - assert_eq!(f(&c[..]), Ok((&"123"[..], &b[..]))); + assert_eq!(f(a), Err(Err::Incomplete(Needed::new(1)))); + assert_eq!(f(b), Err(Err::Incomplete(Needed::new(1)))); + assert_eq!(f(c), Ok(("123", b))); assert_eq!( - f(&d[..]), - Err(Err::Error(error_position!(&d[..], ErrorKind::TakeWhile1))) + f(d), + Err(Err::Error(error_position!(d, ErrorKind::TakeWhile1))) ); } @@ -302,15 +302,7 @@ mod test { const CONSUMED: &str = "βèƒôřèÂßÇ"; const LEFTOVER: &str = "áƒƭèř"; fn while_s(c: char) -> bool { - c == 'β' - || c == 'è' - || c == 'ƒ' - || c == 'ô' - || c == 'ř' - || c == 'è' - || c == 'Â' - || c == 'ß' - || c == 'Ç' + matches!(c, 'β' | 'è' | 'ƒ' | 'ô' | 'ř' | 'Â' | 'ß' | 'Ç') } fn test(input: &str) -> IResult<&str, &str> { take_while(while_s)(input) @@ -361,15 +353,7 @@ mod test { const CONSUMED: &str = "βèƒôřèÂßÇ"; const LEFTOVER: &str = "áƒƭèř"; fn while1_s(c: char) -> bool { - c == 'β' - || c == 'è' - || c == 'ƒ' - || c == 'ô' - || c == 'ř' - || c == 'è' - || c == 'Â' - || c == 'ß' - || c == 'Ç' + matches!(c, 'β' | 'è' | 'ƒ' | 'ô' | 'ř' | 'Â' | 'ß' | 'Ç') } fn test(input: &str) -> IResult<&str, &str> { take_while1(while1_s)(input) @@ -510,8 +494,8 @@ mod test { recognize(many1(alt((tag("a"), tag("b")))))(i) } - assert_eq!(f(&a[..]), Ok((&a[6..], &a[..]))); - assert_eq!(f(&b[..]), Ok((&b[4..], &b[..4]))); + assert_eq!(f(a), Ok((&a[6..], a))); + assert_eq!(f(b), Ok((&b[4..], &b[..4]))); } #[test] diff --git a/src/traits.rs b/src/traits.rs index 394e5bc3a..43d175c42 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -109,7 +109,7 @@ impl AsBytes for str { impl<'a> AsBytes for &'a [u8] { #[inline(always)] fn as_bytes(&self) -> &[u8] { - *self + self } } @@ -148,7 +148,8 @@ as_bytes_array_impls! { } /// Transforms common types to a char for basic token parsing -pub trait AsChar { +#[allow(clippy::len_without_is_empty)] +pub trait AsChar: Copy { /// makes a char from self fn as_char(self) -> char; @@ -178,7 +179,7 @@ impl AsChar for u8 { } #[inline] fn is_alpha(self) -> bool { - (self >= 0x41 && self <= 0x5A) || (self >= 0x61 && self <= 0x7A) + matches!(self, 0x41..=0x5A | 0x61..=0x7A) } #[inline] fn is_alphanum(self) -> bool { @@ -186,17 +187,15 @@ impl AsChar for u8 { } #[inline] fn is_dec_digit(self) -> bool { - self >= 0x30 && self <= 0x39 + matches!(self, 0x30..=0x39) } #[inline] fn is_hex_digit(self) -> bool { - (self >= 0x30 && self <= 0x39) - || (self >= 0x41 && self <= 0x46) - || (self >= 0x61 && self <= 0x66) + matches!(self, 0x30..=0x39 | 0x41..=0x46 | 0x61..=0x66) } #[inline] fn is_oct_digit(self) -> bool { - self >= 0x30 && self <= 0x37 + matches!(self, 0x30..=0x37) } #[inline] fn len(self) -> usize { @@ -210,7 +209,7 @@ impl<'a> AsChar for &'a u8 { } #[inline] fn is_alpha(self) -> bool { - (*self >= 0x41 && *self <= 0x5A) || (*self >= 0x61 && *self <= 0x7A) + matches!(*self, 0x41..=0x5A | 0x61..=0x7A) } #[inline] fn is_alphanum(self) -> bool { @@ -218,17 +217,15 @@ impl<'a> AsChar for &'a u8 { } #[inline] fn is_dec_digit(self) -> bool { - *self >= 0x30 && *self <= 0x39 + matches!(*self, 0x30..=0x39) } #[inline] fn is_hex_digit(self) -> bool { - (*self >= 0x30 && *self <= 0x39) - || (*self >= 0x41 && *self <= 0x46) - || (*self >= 0x61 && *self <= 0x66) + matches!(*self, 0x30..=0x39 | 0x41..=0x46 | 0x61..=0x66) } #[inline] fn is_oct_digit(self) -> bool { - *self >= 0x30 && *self <= 0x37 + matches!(*self, 0x30..=0x37) } #[inline] fn len(self) -> usize { @@ -703,7 +700,7 @@ impl<'a> InputTakeAtPosition for &'a str { /// Indicates whether a comparison was successful, an error, or /// if more data was needed -#[derive(Debug, PartialEq)] +#[derive(Debug, Eq, PartialEq)] pub enum CompareResult { /// Comparison was successful Ok, @@ -1385,7 +1382,7 @@ impl HexDisplay for [u8] { v.push(b'\t'); for &byte in chunk { - if (byte >= 32 && byte <= 126) || byte >= 128 { + if matches!(byte, 32..=126 | 128..=255) { v.push(byte); } else { v.push(b'.'); @@ -1429,8 +1426,7 @@ mod tests { #[test] fn test_offset_str() { - let s = "abcřèÂßÇd123"; - let a = &s[..]; + let a = "abcřèÂßÇd123"; let b = &a[7..]; let c = &a[..5]; let d = &a[5..9]; diff --git a/tests/css.rs b/tests/css.rs index ad3d72b8f..90becbd94 100644 --- a/tests/css.rs +++ b/tests/css.rs @@ -3,7 +3,7 @@ use nom::combinator::map_res; use nom::sequence::tuple; use nom::IResult; -#[derive(Debug, PartialEq)] +#[derive(Debug, Eq, PartialEq)] pub struct Color { pub red: u8, pub green: u8, @@ -15,7 +15,7 @@ fn from_hex(input: &str) -> Result { } fn is_hex_digit(c: char) -> bool { - c.is_digit(16) + c.is_ascii_hexdigit() } fn hex_primary(input: &str) -> IResult<&str, u8> { diff --git a/tests/ini_str.rs b/tests/ini_str.rs index 370230352..db975e996 100644 --- a/tests/ini_str.rs +++ b/tests/ini_str.rs @@ -54,6 +54,7 @@ fn category_and_keys(i: &str) -> IResult<&str, (&str, HashMap<&str, &str>)> { pair(category, keys_and_values)(i) } +#[allow(clippy::type_complexity)] fn categories_aggregator(i: &str) -> IResult<&str, Vec<(&str, HashMap<&str, &str>)>> { many0(category_and_keys)(i) } diff --git a/tests/issues.rs b/tests/issues.rs index 4f7eaad18..fde900774 100644 --- a/tests/issues.rs +++ b/tests/issues.rs @@ -1,6 +1,6 @@ //#![feature(trace_macros)] #![allow(dead_code)] -#![cfg_attr(feature = "cargo-clippy", allow(redundant_closure))] +#![allow(clippy::redundant_closure)] use nom::{error::ErrorKind, Err, IResult, Needed}; @@ -120,6 +120,7 @@ mod issue_647 { v: Vec, } + #[allow(clippy::type_complexity)] fn list<'a, 'b>( input: Input<'a>, _cs: &'b f64, diff --git a/tests/mp4.rs b/tests/mp4.rs index 852bf2955..79dbc7c65 100644 --- a/tests/mp4.rs +++ b/tests/mp4.rs @@ -26,7 +26,7 @@ fn mp4_box(input: &[u8]) -> IResult<&[u8], &[u8]> { } } -#[cfg_attr(rustfmt, rustfmt_skip)] +#[rustfmt::skip] #[derive(PartialEq,Eq,Debug)] struct FileType<'a> { major_brand: &'a str, @@ -34,7 +34,7 @@ struct FileType<'a> { compatible_brands: Vec<&'a str> } -#[cfg_attr(rustfmt, rustfmt_skip)] +#[rustfmt::skip] #[allow(non_snake_case)] #[derive(Debug,Clone)] pub struct Mvhd32 { @@ -64,7 +64,7 @@ pub struct Mvhd32 { track_id: u32 } -#[cfg_attr(rustfmt, rustfmt_skip)] +#[rustfmt::skip] #[allow(non_snake_case)] #[derive(Debug,Clone)] pub struct Mvhd64 { @@ -94,7 +94,7 @@ pub struct Mvhd64 { track_id: u32 } -#[cfg_attr(rustfmt, rustfmt_skip)] +#[rustfmt::skip] fn mvhd32(i: &[u8]) -> IResult<&[u8], MvhdBox> { let (i, version_flags) = be_u32(i)?; let (i, created_date) = be_u32(i)?; @@ -146,7 +146,7 @@ fn mvhd32(i: &[u8]) -> IResult<&[u8], MvhdBox> { Ok((i, mvhd_box)) } -#[cfg_attr(rustfmt, rustfmt_skip)] +#[rustfmt::skip] fn mvhd64(i: &[u8]) -> IResult<&[u8], MvhdBox> { let (i, version_flags) = be_u32(i)?; let (i, created_date) = be_u64(i)?; diff --git a/tests/overflow.rs b/tests/overflow.rs index ea513bb39..8ac074ae1 100644 --- a/tests/overflow.rs +++ b/tests/overflow.rs @@ -1,4 +1,4 @@ -#![cfg_attr(feature = "cargo-clippy", allow(unreadable_literal))] +#![allow(clippy::unreadable_literal)] #![cfg(target_pointer_width = "64")] use nom::bytes::streaming::take; @@ -73,6 +73,7 @@ fn overflow_incomplete_many1() { fn overflow_incomplete_many_till() { use nom::{bytes::complete::tag, multi::many_till}; + #[allow(clippy::type_complexity)] fn multi(i: &[u8]) -> IResult<&[u8], (Vec<&[u8]>, &[u8])> { many_till(length_data(be_u64), tag("abc"))(i) } diff --git a/tests/reborrow_fold.rs b/tests/reborrow_fold.rs index 486617e42..ba3df092d 100644 --- a/tests/reborrow_fold.rs +++ b/tests/reborrow_fold.rs @@ -10,7 +10,7 @@ use nom::multi::fold_many0; use nom::sequence::delimited; use nom::IResult; -fn atom<'a>(_tomb: &'a mut ()) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], String> { +fn atom(_tomb: &mut ()) -> impl for<'a> FnMut(&'a [u8]) -> IResult<&'a [u8], String> { move |input| { map( map_res(is_not(" \t\r\n"), str::from_utf8), @@ -20,7 +20,7 @@ fn atom<'a>(_tomb: &'a mut ()) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], Stri } // FIXME: should we support the use case of borrowing data mutably in a parser? -fn list<'a>(i: &'a [u8], tomb: &'a mut ()) -> IResult<&'a [u8], String> { +fn list<'a>(i: &'a [u8], tomb: &mut ()) -> IResult<&'a [u8], String> { delimited( char('('), fold_many0(atom(tomb), String::new, |acc: String, next: String| {