Skip to content

Commit 3ee23fe

Browse files
committed
Fix clippy warnings for lib, examples, and tests.
1 parent 3645656 commit 3ee23fe

25 files changed

+110
-130
lines changed

examples/s_expression.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use nom::{
1919
/// In this case, we want something tree-like
2020
2121
/// Starting from the most basic, we define some built-in functions that our lisp has
22-
#[derive(Debug, PartialEq, Clone, Copy)]
22+
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
2323
pub enum BuiltIn {
2424
Plus,
2525
Minus,
@@ -32,7 +32,7 @@ pub enum BuiltIn {
3232
/// We now wrap this type and a few other primitives into our Atom type.
3333
/// Remember from before that Atoms form one half of our language.
3434
35-
#[derive(Debug, PartialEq, Clone)]
35+
#[derive(Debug, Eq, PartialEq, Clone)]
3636
pub enum Atom {
3737
Num(i32),
3838
Keyword(String),
@@ -50,7 +50,7 @@ pub enum Atom {
5050
/// structure that we can deal with programmatically. Thus any valid expression
5151
/// is also a valid data structure in Lisp itself.
5252
53-
#[derive(Debug, PartialEq, Clone)]
53+
#[derive(Debug, Eq, PartialEq, Clone)]
5454
pub enum Expr {
5555
Constant(Atom),
5656
/// (func-name arg1 arg2)
@@ -65,7 +65,7 @@ pub enum Expr {
6565

6666
/// Continuing the trend of starting from the simplest piece and building up,
6767
/// we start by creating a parser for the built-in operator functions.
68-
fn parse_builtin_op<'a>(i: &'a str) -> IResult<&'a str, BuiltIn, VerboseError<&'a str>> {
68+
fn parse_builtin_op(i: &str) -> IResult<&str, BuiltIn, VerboseError<&str>> {
6969
// one_of matches one of the characters we give it
7070
let (i, t) = one_of("+-*/=")(i)?;
7171

@@ -84,7 +84,7 @@ fn parse_builtin_op<'a>(i: &'a str) -> IResult<&'a str, BuiltIn, VerboseError<&'
8484
))
8585
}
8686

87-
fn parse_builtin<'a>(i: &'a str) -> IResult<&'a str, BuiltIn, VerboseError<&'a str>> {
87+
fn parse_builtin(i: &str) -> IResult<&str, BuiltIn, VerboseError<&str>> {
8888
// alt gives us the result of first parser that succeeds, of the series of
8989
// parsers we give it
9090
alt((
@@ -96,7 +96,7 @@ fn parse_builtin<'a>(i: &'a str) -> IResult<&'a str, BuiltIn, VerboseError<&'a s
9696
}
9797

9898
/// Our boolean values are also constant, so we can do it the same way
99-
fn parse_bool<'a>(i: &'a str) -> IResult<&'a str, Atom, VerboseError<&'a str>> {
99+
fn parse_bool(i: &str) -> IResult<&str, Atom, VerboseError<&str>> {
100100
alt((
101101
map(tag("#t"), |_| Atom::Boolean(true)),
102102
map(tag("#f"), |_| Atom::Boolean(false)),
@@ -109,7 +109,7 @@ fn parse_bool<'a>(i: &'a str) -> IResult<&'a str, Atom, VerboseError<&'a str>> {
109109
///
110110
/// Put plainly: `preceded(tag(":"), cut(alpha1))` means that once we see the `:`
111111
/// character, we have to see one or more alphabetic chararcters or the input is invalid.
112-
fn parse_keyword<'a>(i: &'a str) -> IResult<&'a str, Atom, VerboseError<&'a str>> {
112+
fn parse_keyword(i: &str) -> IResult<&str, Atom, VerboseError<&str>> {
113113
map(
114114
context("keyword", preceded(tag(":"), cut(alpha1))),
115115
|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>
118118

119119
/// Next up is number parsing. We're keeping it simple here by accepting any number (> 1)
120120
/// of digits but ending the program if it doesn't fit into an i32.
121-
fn parse_num<'a>(i: &'a str) -> IResult<&'a str, Atom, VerboseError<&'a str>> {
121+
fn parse_num(i: &str) -> IResult<&str, Atom, VerboseError<&str>> {
122122
alt((
123123
map_res(digit1, |digit_str: &str| {
124124
digit_str.parse::<i32>().map(Atom::Num)
125125
}),
126126
map(preceded(tag("-"), digit1), |digit_str: &str| {
127-
Atom::Num(-1 * digit_str.parse::<i32>().unwrap())
127+
Atom::Num(-digit_str.parse::<i32>().unwrap())
128128
}),
129129
))(i)
130130
}
131131

132132
/// Now we take all these simple parsers and connect them.
133133
/// We can now parse half of our language!
134-
fn parse_atom<'a>(i: &'a str) -> IResult<&'a str, Atom, VerboseError<&'a str>> {
134+
fn parse_atom(i: &str) -> IResult<&str, Atom, VerboseError<&str>> {
135135
alt((
136136
parse_num,
137137
parse_bool,
@@ -141,8 +141,8 @@ fn parse_atom<'a>(i: &'a str) -> IResult<&'a str, Atom, VerboseError<&'a str>> {
141141
}
142142

143143
/// We then add the Expr layer on top
144-
fn parse_constant<'a>(i: &'a str) -> IResult<&'a str, Expr, VerboseError<&'a str>> {
145-
map(parse_atom, |atom| Expr::Constant(atom))(i)
144+
fn parse_constant(i: &str) -> IResult<&str, Expr, VerboseError<&str>> {
145+
map(parse_atom, Expr::Constant)(i)
146146
}
147147

148148
/// Before continuing, we need a helper function to parse lists.
@@ -172,7 +172,7 @@ where
172172
///
173173
/// `tuple` is used to sequence parsers together, so we can translate this directly
174174
/// and then map over it to transform the output into an `Expr::Application`
175-
fn parse_application<'a>(i: &'a str) -> IResult<&'a str, Expr, VerboseError<&'a str>> {
175+
fn parse_application(i: &str) -> IResult<&str, Expr, VerboseError<&str>> {
176176
let application_inner = map(tuple((parse_expr, many0(parse_expr))), |(head, tail)| {
177177
Expr::Application(Box::new(head), tail)
178178
});
@@ -186,7 +186,7 @@ fn parse_application<'a>(i: &'a str) -> IResult<&'a str, Expr, VerboseError<&'a
186186
///
187187
/// In fact, we define our parser as if `Expr::If` was defined with an Option in it,
188188
/// we have the `opt` combinator which fits very nicely here.
189-
fn parse_if<'a>(i: &'a str) -> IResult<&'a str, Expr, VerboseError<&'a str>> {
189+
fn parse_if(i: &str) -> IResult<&str, Expr, VerboseError<&str>> {
190190
let if_inner = context(
191191
"if expression",
192192
map(
@@ -218,19 +218,19 @@ fn parse_if<'a>(i: &'a str) -> IResult<&'a str, Expr, VerboseError<&'a str>> {
218218
/// This example doesn't have the symbol atom, but by adding variables and changing
219219
/// the definition of quote to not always be around an S-expression, we'd get them
220220
/// naturally.
221-
fn parse_quote<'a>(i: &'a str) -> IResult<&'a str, Expr, VerboseError<&'a str>> {
221+
fn parse_quote(i: &str) -> IResult<&str, Expr, VerboseError<&str>> {
222222
// this should look very straight-forward after all we've done:
223223
// we find the `'` (quote) character, use cut to say that we're unambiguously
224224
// looking for an s-expression of 0 or more expressions, and then parse them
225225
map(
226226
context("quote", preceded(tag("'"), cut(s_exp(many0(parse_expr))))),
227-
|exprs| Expr::Quote(exprs),
227+
Expr::Quote,
228228
)(i)
229229
}
230230

231231
/// We tie them all together again, making a top-level expression parser!
232232
233-
fn parse_expr<'a>(i: &'a str) -> IResult<&'a str, Expr, VerboseError<&'a str>> {
233+
fn parse_expr(i: &str) -> IResult<&str, Expr, VerboseError<&str>> {
234234
preceded(
235235
multispace0,
236236
alt((parse_constant, parse_application, parse_if, parse_quote)),
@@ -291,7 +291,7 @@ fn eval_expression(e: Expr) -> Option<Expr> {
291291
let reduced_head = eval_expression(*head)?;
292292
let reduced_tail = tail
293293
.into_iter()
294-
.map(|expr| eval_expression(expr))
294+
.map(eval_expression)
295295
.collect::<Option<Vec<Expr>>>()?;
296296
if let Expr::Constant(Atom::BuiltIn(bi)) = reduced_head {
297297
Some(Expr::Constant(match bi {
@@ -361,7 +361,7 @@ fn eval_expression(e: Expr) -> Option<Expr> {
361361
fn eval_from_str(src: &str) -> Result<Expr, String> {
362362
parse_expr(src)
363363
.map_err(|e: nom::Err<VerboseError<&str>>| format!("{:#?}", e))
364-
.and_then(|(_, exp)| eval_expression(exp).ok_or("Eval failed".to_string()))
364+
.and_then(|(_, exp)| eval_expression(exp).ok_or_else(|| "Eval failed".to_string()))
365365
}
366366

367367
fn main() {

examples/string.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ where
5454
// the function returns None, map_opt returns an error. In this case, because
5555
// not all u32 values are valid unicode code points, we have to fallibly
5656
// convert to char with from_u32.
57-
map_opt(parse_u32, |value| std::char::from_u32(value))(input)
57+
map_opt(parse_u32, std::char::from_u32)(input)
5858
}
5959

6060
/// Parse an escaped character: \n, \t, \r, \u{00AC}, etc.

src/branch/tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
};
1313

1414
#[cfg(feature = "alloc")]
15-
#[derive(Debug, Clone, PartialEq)]
15+
#[derive(Debug, Clone, Eq, PartialEq)]
1616
pub struct ErrorStr(String);
1717

1818
#[cfg(feature = "alloc")]
@@ -114,6 +114,7 @@ fn alt_incomplete() {
114114

115115
#[test]
116116
fn permutation_test() {
117+
#[allow(clippy::type_complexity)]
117118
fn perm(i: &[u8]) -> IResult<&[u8], (&[u8], &[u8], &[u8])> {
118119
permutation((tag("abcd"), tag("efg"), tag("hi")))(i)
119120
}

src/bytes/complete.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ where
327327
/// assert_eq!(till_colon("12345"), Ok(("", "12345")));
328328
/// assert_eq!(till_colon(""), Ok(("", "")));
329329
/// ```
330+
#[allow(clippy::redundant_closure)]
330331
pub fn take_till<F, Input, Error: ParseError<Input>>(
331332
cond: F,
332333
) -> impl Fn(Input) -> IResult<Input, Input, Error>
@@ -358,6 +359,7 @@ where
358359
/// assert_eq!(till_colon("12345"), Ok(("", "12345")));
359360
/// assert_eq!(till_colon(""), Err(Err::Error(Error::new("", ErrorKind::TakeTill1))));
360361
/// ```
362+
#[allow(clippy::redundant_closure)]
361363
pub fn take_till1<F, Input, Error: ParseError<Input>>(
362364
cond: F,
363365
) -> impl Fn(Input) -> IResult<Input, Input, Error>
@@ -736,7 +738,7 @@ mod tests {
736738
}
737739

738740
// issue ##1118 escaped does not work with empty string
739-
fn unquote<'a>(input: &'a str) -> IResult<&'a str, &'a str> {
741+
fn unquote(input: &str) -> IResult<&str, &str> {
740742
use crate::bytes::complete::*;
741743
use crate::character::complete::*;
742744
use crate::combinator::opt;

src/bytes/streaming.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ where
340340
/// assert_eq!(till_colon("12345"), Err(Err::Incomplete(Needed::new(1))));
341341
/// assert_eq!(till_colon(""), Err(Err::Incomplete(Needed::new(1))));
342342
/// ```
343+
#[allow(clippy::redundant_closure)]
343344
pub fn take_till<F, Input, Error: ParseError<Input>>(
344345
cond: F,
345346
) -> impl Fn(Input) -> IResult<Input, Input, Error>
@@ -372,6 +373,7 @@ where
372373
/// assert_eq!(till_colon("12345"), Err(Err::Incomplete(Needed::new(1))));
373374
/// assert_eq!(till_colon(""), Err(Err::Incomplete(Needed::new(1))));
374375
/// ```
376+
#[allow(clippy::redundant_closure)]
375377
pub fn take_till1<F, Input, Error: ParseError<Input>>(
376378
cond: F,
377379
) -> impl Fn(Input) -> IResult<Input, Input, Error>

src/bytes/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,11 +612,11 @@ fn case_insensitive() {
612612
assert_eq!(test2("ab"), Err(Err::Incomplete(Needed::new(2))));
613613
assert_eq!(
614614
test2("Hello"),
615-
Err(Err::Error(error_position!(&"Hello"[..], ErrorKind::Tag)))
615+
Err(Err::Error(error_position!("Hello", ErrorKind::Tag)))
616616
);
617617
assert_eq!(
618618
test2("Hel"),
619-
Err(Err::Error(error_position!(&"Hel"[..], ErrorKind::Tag)))
619+
Err(Err::Error(error_position!("Hel", ErrorKind::Tag)))
620620
);
621621
}
622622

src/character/complete.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -897,16 +897,16 @@ mod tests {
897897
let e = " ";
898898
assert_eq!(alpha1::<_, (_, ErrorKind)>(a), Ok((empty, a)));
899899
assert_eq!(alpha1(b), Err(Err::Error((b, ErrorKind::Alpha))));
900-
assert_eq!(alpha1::<_, (_, ErrorKind)>(c), Ok((&c[1..], &"a"[..])));
901-
assert_eq!(alpha1::<_, (_, ErrorKind)>(d), Ok(("é12", &"az"[..])));
900+
assert_eq!(alpha1::<_, (_, ErrorKind)>(c), Ok((&c[1..], "a")));
901+
assert_eq!(alpha1::<_, (_, ErrorKind)>(d), Ok(("é12", "az")));
902902
assert_eq!(digit1(a), Err(Err::Error((a, ErrorKind::Digit))));
903903
assert_eq!(digit1::<_, (_, ErrorKind)>(b), Ok((empty, b)));
904904
assert_eq!(digit1(c), Err(Err::Error((c, ErrorKind::Digit))));
905905
assert_eq!(digit1(d), Err(Err::Error((d, ErrorKind::Digit))));
906906
assert_eq!(hex_digit1::<_, (_, ErrorKind)>(a), Ok((empty, a)));
907907
assert_eq!(hex_digit1::<_, (_, ErrorKind)>(b), Ok((empty, b)));
908908
assert_eq!(hex_digit1::<_, (_, ErrorKind)>(c), Ok((empty, c)));
909-
assert_eq!(hex_digit1::<_, (_, ErrorKind)>(d), Ok(("zé12", &"a"[..])));
909+
assert_eq!(hex_digit1::<_, (_, ErrorKind)>(d), Ok(("zé12", "a")));
910910
assert_eq!(hex_digit1(e), Err(Err::Error((e, ErrorKind::HexDigit))));
911911
assert_eq!(oct_digit1(a), Err(Err::Error((a, ErrorKind::OctDigit))));
912912
assert_eq!(oct_digit1::<_, (_, ErrorKind)>(b), Ok((empty, b)));
@@ -994,10 +994,7 @@ mod tests {
994994
);
995995

996996
let d: &[u8] = b"ab12cd";
997-
assert_eq!(
998-
not_line_ending::<_, (_, ErrorKind)>(d),
999-
Ok((&[][..], &d[..]))
1000-
);
997+
assert_eq!(not_line_ending::<_, (_, ErrorKind)>(d), Ok((&[][..], d)));
1001998
}
1002999

10031000
#[test]
@@ -1131,7 +1128,7 @@ mod tests {
11311128
assert_parse!(crlf("\r\na"), Ok(("a", "\r\n")));
11321129
assert_parse!(
11331130
crlf("\r"),
1134-
Err(Err::Error(error_position!(&"\r"[..], ErrorKind::CrLf)))
1131+
Err(Err::Error(error_position!("\r", ErrorKind::CrLf)))
11351132
);
11361133
assert_parse!(
11371134
crlf("\ra"),
@@ -1156,7 +1153,7 @@ mod tests {
11561153
assert_parse!(line_ending("\r\na"), Ok(("a", "\r\n")));
11571154
assert_parse!(
11581155
line_ending("\r"),
1159-
Err(Err::Error(error_position!(&"\r"[..], ErrorKind::CrLf)))
1156+
Err(Err::Error(error_position!("\r", ErrorKind::CrLf)))
11601157
);
11611158
assert_parse!(
11621159
line_ending("\ra"),

src/character/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub mod streaming;
1919
/// ```
2020
#[inline]
2121
pub fn is_alphabetic(chr: u8) -> bool {
22-
(chr >= 0x41 && chr <= 0x5A) || (chr >= 0x61 && chr <= 0x7A)
22+
matches!(chr, 0x41..=0x5A | 0x61..=0x7A)
2323
}
2424

2525
/// Tests if byte is ASCII digit: 0-9
@@ -33,7 +33,7 @@ pub fn is_alphabetic(chr: u8) -> bool {
3333
/// ```
3434
#[inline]
3535
pub fn is_digit(chr: u8) -> bool {
36-
chr >= 0x30 && chr <= 0x39
36+
matches!(chr, 0x30..=0x39)
3737
}
3838

3939
/// Tests if byte is ASCII hex digit: 0-9, A-F, a-f
@@ -49,7 +49,7 @@ pub fn is_digit(chr: u8) -> bool {
4949
/// ```
5050
#[inline]
5151
pub fn is_hex_digit(chr: u8) -> bool {
52-
(chr >= 0x30 && chr <= 0x39) || (chr >= 0x41 && chr <= 0x46) || (chr >= 0x61 && chr <= 0x66)
52+
matches!(chr, 0x30..=0x39 | 0x41..=0x46 | 0x61..=0x66)
5353
}
5454

5555
/// Tests if byte is ASCII octal digit: 0-7
@@ -64,7 +64,7 @@ pub fn is_hex_digit(chr: u8) -> bool {
6464
/// ```
6565
#[inline]
6666
pub fn is_oct_digit(chr: u8) -> bool {
67-
chr >= 0x30 && chr <= 0x37
67+
matches!(chr, 0x30..=0x37)
6868
}
6969

7070
/// Tests if byte is ASCII alphanumeric: A-Z, a-z, 0-9

src/character/streaming.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -836,8 +836,8 @@ mod tests {
836836
Err(Err::Incomplete(Needed::new(1)))
837837
);
838838
assert_eq!(alpha1(b), Err(Err::Error((b, ErrorKind::Alpha))));
839-
assert_eq!(alpha1::<_, (_, ErrorKind)>(c), Ok((&c[1..], &"a"[..])));
840-
assert_eq!(alpha1::<_, (_, ErrorKind)>(d), Ok(("é12", &"az"[..])));
839+
assert_eq!(alpha1::<_, (_, ErrorKind)>(c), Ok((&c[1..], "a")));
840+
assert_eq!(alpha1::<_, (_, ErrorKind)>(d), Ok(("é12", "az")));
841841
assert_eq!(digit1(a), Err(Err::Error((a, ErrorKind::Digit))));
842842
assert_eq!(
843843
digit1::<_, (_, ErrorKind)>(b),
@@ -857,7 +857,7 @@ mod tests {
857857
hex_digit1::<_, (_, ErrorKind)>(c),
858858
Err(Err::Incomplete(Needed::new(1)))
859859
);
860-
assert_eq!(hex_digit1::<_, (_, ErrorKind)>(d), Ok(("zé12", &"a"[..])));
860+
assert_eq!(hex_digit1::<_, (_, ErrorKind)>(d), Ok(("zé12", "a")));
861861
assert_eq!(hex_digit1(e), Err(Err::Error((e, ErrorKind::HexDigit))));
862862
assert_eq!(oct_digit1(a), Err(Err::Error((a, ErrorKind::OctDigit))));
863863
assert_eq!(

src/character/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ fn char_str() {
5454
char('c')(i)
5555
}
5656

57-
let a = &"abcd"[..];
57+
let a = "abcd";
5858
assert_eq!(f(a), Err(Err::Error(error_position!(a, ErrorKind::Char))));
5959

60-
let b = &"cde"[..];
61-
assert_eq!(f(b), Ok((&"de"[..], 'c')));
60+
let b = "cde";
61+
assert_eq!(f(b), Ok(("de", 'c')));
6262
}

src/combinator/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ fn test_verify_alloc() {
183183
s == &b"abc"[..]
184184
});
185185

186-
assert_eq!(parser1(&b"abcd"[..]), Ok((&b"d"[..], (&b"abc").to_vec())));
186+
assert_eq!(parser1(&b"abcd"[..]), Ok((&b"d"[..], b"abc".to_vec())));
187187
assert_eq!(
188188
parser1(&b"defg"[..]),
189189
Err(Err::Error((&b"defg"[..], ErrorKind::Verify)))

src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub trait FromExternalError<I, E> {
5454
}
5555

5656
/// default error type, only contains the error' location and code
57-
#[derive(Debug, PartialEq)]
57+
#[derive(Debug, Eq, PartialEq)]
5858
pub struct Error<I> {
5959
/// position of the error in the input data
6060
pub input: I,
@@ -147,7 +147,7 @@ pub fn append_error<I, E: ParseError<I>>(input: I, kind: ErrorKind, other: E) ->
147147
/// it can be used to display user friendly error messages
148148
#[cfg(feature = "alloc")]
149149
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))]
150-
#[derive(Clone, Debug, PartialEq)]
150+
#[derive(Clone, Debug, Eq, PartialEq)]
151151
pub struct VerboseError<I> {
152152
/// List of errors accumulated by `VerboseError`, containing the affected
153153
/// part of input data, and some context
@@ -156,7 +156,7 @@ pub struct VerboseError<I> {
156156

157157
#[cfg(feature = "alloc")]
158158
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))]
159-
#[derive(Clone, Debug, PartialEq)]
159+
#[derive(Clone, Debug, Eq, PartialEq)]
160160
/// Error context for `VerboseError`
161161
pub enum VerboseErrorKind {
162162
/// Static string added by the `context` function

0 commit comments

Comments
 (0)