Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clippy warnings for lib, examples, and tests #1585

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions examples/s_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand All @@ -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)
Expand All @@ -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)?;

Expand All @@ -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((
Expand All @@ -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)),
Expand All @@ -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()),
Expand All @@ -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::<i32>().map(Atom::Num)
}),
map(preceded(tag("-"), digit1), |digit_str: &str| {
Atom::Num(-1 * digit_str.parse::<i32>().unwrap())
Atom::Num(-digit_str.parse::<i32>().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,
Expand All @@ -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.
Expand Down Expand Up @@ -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)
});
Expand All @@ -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(
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -291,7 +291,7 @@ fn eval_expression(e: Expr) -> Option<Expr> {
let reduced_head = eval_expression(*head)?;
let reduced_tail = tail
.into_iter()
.map(|expr| eval_expression(expr))
.map(eval_expression)
.collect::<Option<Vec<Expr>>>()?;
if let Expr::Constant(Atom::BuiltIn(bi)) = reduced_head {
Some(Expr::Constant(match bi {
Expand Down Expand Up @@ -361,7 +361,7 @@ fn eval_expression(e: Expr) -> Option<Expr> {
fn eval_from_str(src: &str) -> Result<Expr, String> {
parse_expr(src)
.map_err(|e: nom::Err<VerboseError<&str>>| 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() {
Expand Down
2 changes: 1 addition & 1 deletion examples/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion src/branch/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 3 additions & 1 deletion src/bytes/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F, Input, Error: ParseError<Input>>(
cond: F,
) -> impl Fn(Input) -> IResult<Input, Input, Error>
Expand Down Expand Up @@ -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<F, Input, Error: ParseError<Input>>(
cond: F,
) -> impl Fn(Input) -> IResult<Input, Input, Error>
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/bytes/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F, Input, Error: ParseError<Input>>(
cond: F,
) -> impl Fn(Input) -> IResult<Input, Input, Error>
Expand Down Expand Up @@ -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<F, Input, Error: ParseError<Input>>(
cond: F,
) -> impl Fn(Input) -> IResult<Input, Input, Error>
Expand Down
4 changes: 2 additions & 2 deletions src/bytes/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
);
}

Expand Down
15 changes: 6 additions & 9 deletions src/character/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -897,16 +897,16 @@ 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))));
assert_eq!(digit1(d), Err(Err::Error((d, ErrorKind::Digit))));
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)));
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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"),
Expand All @@ -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"),
Expand Down
8 changes: 4 additions & 4 deletions src/character/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that Clippy's suggested fixes for clippy::manual_range_contains are unidiomatic for this use case (as well as other matches! fixes in this branch):

(0x30..=0x39).contains(&chr) || (0x41..=0x46).contains(&chr) || (0x61..=0x66).contains(&chr)

}

/// Tests if byte is ASCII octal digit: 0-7
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/character/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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!(
Expand Down
6 changes: 3 additions & 3 deletions src/character/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
}
2 changes: 1 addition & 1 deletion src/combinator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
6 changes: 3 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub trait FromExternalError<I, E> {
}

/// default error type, only contains the error' location and code
#[derive(Debug, PartialEq)]
#[derive(Debug, Eq, PartialEq)]
pub struct Error<I> {
/// position of the error in the input data
pub input: I,
Expand Down Expand Up @@ -147,7 +147,7 @@ pub fn append_error<I, E: ParseError<I>>(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<I> {
/// List of errors accumulated by `VerboseError`, containing the affected
/// part of input data, and some context
Expand All @@ -156,7 +156,7 @@ pub struct VerboseError<I> {

#[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
Expand Down
Loading