Skip to content

Commit ca3f431

Browse files
committed
Add clippy into CI and fix clippy warnings
1 parent 3645656 commit ca3f431

File tree

10 files changed

+80
-49
lines changed

10 files changed

+80
-49
lines changed

.github/workflows/ci.yml

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141

4242
steps:
4343
- name: Checkout sources
44-
uses: actions/checkout@v2
44+
uses: actions/checkout@v3
4545

4646
- name: Install rust (${{ matrix.rust }})
4747
uses: actions-rs/toolchain@v1
@@ -71,7 +71,7 @@ jobs:
7171

7272
steps:
7373
- name: Checkout sources
74-
uses: actions/checkout@v2
74+
uses: actions/checkout@v3
7575

7676
- name: Install rust (${{ env.RUST_MINVERSION }})
7777
uses: actions-rs/toolchain@v1
@@ -95,7 +95,7 @@ jobs:
9595

9696
steps:
9797
- name: Checkout sources
98-
uses: actions/checkout@v2
98+
uses: actions/checkout@v3
9999

100100
- name: Install rust
101101
uses: actions-rs/toolchain@v1
@@ -125,7 +125,7 @@ jobs:
125125

126126
steps:
127127
- name: Checkout sources
128-
uses: actions/checkout@v2
128+
uses: actions/checkout@v3
129129

130130
- name: Install rust
131131
uses: actions-rs/toolchain@v1
@@ -145,7 +145,7 @@ jobs:
145145

146146
steps:
147147
- name: Checkout sources
148-
uses: actions/checkout@v2
148+
uses: actions/checkout@v3
149149

150150
- name: Install rust
151151
uses: actions-rs/toolchain@v1
@@ -162,13 +162,36 @@ jobs:
162162
command: fmt
163163
args: -- --check
164164

165+
clippy:
166+
name: Check clippy
167+
runs-on: ubuntu-latest
168+
169+
steps:
170+
- name: Checkout sources
171+
uses: actions/checkout@v3
172+
173+
- name: Install rust
174+
uses: actions-rs/toolchain@v1
175+
with:
176+
toolchain: stable
177+
components: clippy
178+
profile: minimal
179+
override: true
180+
181+
- name: cargo clippy -- -D warnings
182+
continue-on-error: true
183+
uses: actions-rs/cargo@v1
184+
with:
185+
command: clippy
186+
args: -- -D warnings
187+
165188
coverage:
166189
name: Coverage
167190
runs-on: ubuntu-latest
168191

169192
steps:
170193
- name: Checkout sources
171-
uses: actions/checkout@v2
194+
uses: actions/checkout@v3
172195

173196
- name: Install rust
174197
uses: actions-rs/toolchain@v1

src/bytes/complete.rs

Lines changed: 2 additions & 0 deletions
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>

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/character/complete.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,23 +414,23 @@ where
414414
/// assert_eq!(parser("c1"), Err(Err::Error(Error::new("c1", ErrorKind::Digit))));
415415
/// assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::Digit))));
416416
/// ```
417-
///
417+
///
418418
/// ## Parsing an integer
419419
/// You can use `digit1` in combination with [`map_res`] to parse an integer:
420-
///
420+
///
421421
/// ```
422422
/// # use nom::{Err, error::{Error, ErrorKind}, IResult, Needed};
423423
/// # use nom::combinator::map_res;
424424
/// # use nom::character::complete::digit1;
425425
/// fn parser(input: &str) -> IResult<&str, u32> {
426426
/// map_res(digit1, str::parse)(input)
427427
/// }
428-
///
428+
///
429429
/// assert_eq!(parser("416"), Ok(("", 416)));
430430
/// assert_eq!(parser("12b"), Ok(("b", 12)));
431431
/// assert!(parser("b").is_err());
432432
/// ```
433-
///
433+
///
434434
/// [`map_res`]: crate::combinator::map_res
435435
pub fn digit1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
436436
where

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+
(0x41..=0x5A).contains(&chr) || (0x61..=0x7A).contains(&chr)
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+
(0x30..=0x39).contains(&chr)
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+
(0x30..=0x39).contains(&chr) || (0x41..=0x46).contains(&chr) || (0x61..=0x66).contains(&chr)
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+
(0x30..=0x37).contains(&chr)
6868
}
6969

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

src/error.rs

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

5656
/// default error type, only contains the error' location and code
57+
#[allow(clippy::derive_partial_eq_without_eq)]
5758
#[derive(Debug, PartialEq)]
5859
pub struct Error<I> {
5960
/// position of the error in the input data
@@ -151,11 +152,12 @@ pub fn append_error<I, E: ParseError<I>>(input: I, kind: ErrorKind, other: E) ->
151152
pub struct VerboseError<I> {
152153
/// List of errors accumulated by `VerboseError`, containing the affected
153154
/// part of input data, and some context
154-
pub errors: crate::lib::std::vec::Vec<(I, VerboseErrorKind)>,
155+
pub errors: Vec<(I, VerboseErrorKind)>,
155156
}
156157

157158
#[cfg(feature = "alloc")]
158159
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))]
160+
#[allow(clippy::derive_partial_eq_without_eq)]
159161
#[derive(Clone, Debug, PartialEq)]
160162
/// Error context for `VerboseError`
161163
pub enum VerboseErrorKind {

src/internal.rs

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,7 @@ pub enum Err<E> {
108108
impl<E> Err<E> {
109109
/// Tests if the result is Incomplete
110110
pub fn is_incomplete(&self) -> bool {
111-
if let Err::Incomplete(_) = self {
112-
true
113-
} else {
114-
false
115-
}
111+
matches!(self, Err::Incomplete(_))
116112
}
117113

118114
/// Applies the given function to the inner error
@@ -344,7 +340,7 @@ pub struct Map<F, G, O1> {
344340
phantom: core::marker::PhantomData<O1>,
345341
}
346342

347-
impl<'a, I, O1, O2, E, F: Parser<I, O1, E>, G: Fn(O1) -> O2> Parser<I, O2, E> for Map<F, G, O1> {
343+
impl<I, O1, O2, E, F: Parser<I, O1, E>, G: Fn(O1) -> O2> Parser<I, O2, E> for Map<F, G, O1> {
348344
fn parse(&mut self, i: I) -> IResult<I, O2, E> {
349345
match self.f.parse(i) {
350346
Err(e) => Err(e),
@@ -361,7 +357,7 @@ pub struct FlatMap<F, G, O1> {
361357
phantom: core::marker::PhantomData<O1>,
362358
}
363359

364-
impl<'a, I, O1, O2, E, F: Parser<I, O1, E>, G: Fn(O1) -> H, H: Parser<I, O2, E>> Parser<I, O2, E>
360+
impl<I, O1, O2, E, F: Parser<I, O1, E>, G: Fn(O1) -> H, H: Parser<I, O2, E>> Parser<I, O2, E>
365361
for FlatMap<F, G, O1>
366362
{
367363
fn parse(&mut self, i: I) -> IResult<I, O2, E> {
@@ -378,7 +374,7 @@ pub struct AndThen<F, G, O1> {
378374
phantom: core::marker::PhantomData<O1>,
379375
}
380376

381-
impl<'a, I, O1, O2, E, F: Parser<I, O1, E>, G: Parser<O1, O2, E>> Parser<I, O2, E>
377+
impl<I, O1, O2, E, F: Parser<I, O1, E>, G: Parser<O1, O2, E>> Parser<I, O2, E>
382378
for AndThen<F, G, O1>
383379
{
384380
fn parse(&mut self, i: I) -> IResult<I, O2, E> {
@@ -395,9 +391,7 @@ pub struct And<F, G> {
395391
g: G,
396392
}
397393

398-
impl<'a, I, O1, O2, E, F: Parser<I, O1, E>, G: Parser<I, O2, E>> Parser<I, (O1, O2), E>
399-
for And<F, G>
400-
{
394+
impl<I, O1, O2, E, F: Parser<I, O1, E>, G: Parser<I, O2, E>> Parser<I, (O1, O2), E> for And<F, G> {
401395
fn parse(&mut self, i: I) -> IResult<I, (O1, O2), E> {
402396
let (i, o1) = self.f.parse(i)?;
403397
let (i, o2) = self.g.parse(i)?;
@@ -412,8 +406,8 @@ pub struct Or<F, G> {
412406
g: G,
413407
}
414408

415-
impl<'a, I: Clone, O, E: crate::error::ParseError<I>, F: Parser<I, O, E>, G: Parser<I, O, E>>
416-
Parser<I, O, E> for Or<F, G>
409+
impl<I: Clone, O, E: error::ParseError<I>, F: Parser<I, O, E>, G: Parser<I, O, E>> Parser<I, O, E>
410+
for Or<F, G>
417411
{
418412
fn parse(&mut self, i: I) -> IResult<I, O, E> {
419413
match self.f.parse(i.clone()) {
@@ -436,15 +430,8 @@ pub struct Into<F, O1, O2: From<O1>, E1, E2: From<E1>> {
436430
phantom_err2: core::marker::PhantomData<E2>,
437431
}
438432

439-
impl<
440-
'a,
441-
I: Clone,
442-
O1,
443-
O2: From<O1>,
444-
E1,
445-
E2: crate::error::ParseError<I> + From<E1>,
446-
F: Parser<I, O1, E1>,
447-
> Parser<I, O2, E2> for Into<F, O1, O2, E1, E2>
433+
impl<I: Clone, O1, O2: From<O1>, E1, E2: error::ParseError<I> + From<E1>, F: Parser<I, O1, E1>>
434+
Parser<I, O2, E2> for Into<F, O1, O2, E1, E2>
448435
{
449436
fn parse(&mut self, i: I) -> IResult<I, O2, E2> {
450437
match self.f.parse(i) {

src/number/complete.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1515,7 +1515,7 @@ where
15151515
}
15161516
}
15171517

1518-
let position = position.unwrap_or(i.input_len());
1518+
let position = position.unwrap_or_else(|| i.input_len());
15191519

15201520
let index = if zero_count == 0 {
15211521
position

src/sequence/tests.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::*;
22
use crate::bytes::streaming::{tag, take};
3-
use crate::error::{ErrorKind, Error};
3+
use crate::error::{Error, ErrorKind};
44
use crate::internal::{Err, IResult, Needed};
55
use crate::number::streaming::be_u16;
66

@@ -275,7 +275,16 @@ fn tuple_test() {
275275

276276
#[test]
277277
fn unit_type() {
278-
assert_eq!(tuple::<&'static str, (), Error<&'static str>, ()>(())("abxsbsh"), Ok(("abxsbsh", ())));
279-
assert_eq!(tuple::<&'static str, (), Error<&'static str>, ()>(())("sdfjakdsas"), Ok(("sdfjakdsas", ())));
280-
assert_eq!(tuple::<&'static str, (), Error<&'static str>, ()>(())(""), Ok(("", ())));
278+
assert_eq!(
279+
tuple::<&'static str, (), Error<&'static str>, ()>(())("abxsbsh"),
280+
Ok(("abxsbsh", ()))
281+
);
282+
assert_eq!(
283+
tuple::<&'static str, (), Error<&'static str>, ()>(())("sdfjakdsas"),
284+
Ok(("sdfjakdsas", ()))
285+
);
286+
assert_eq!(
287+
tuple::<&'static str, (), Error<&'static str>, ()>(())(""),
288+
Ok(("", ()))
289+
);
281290
}

src/traits.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,24 +148,31 @@ as_bytes_array_impls! {
148148
}
149149

150150
/// Transforms common types to a char for basic token parsing
151+
#[allow(clippy::len_without_is_empty)]
151152
pub trait AsChar {
152153
/// makes a char from self
154+
#[allow(clippy::wrong_self_convention)]
153155
fn as_char(self) -> char;
154156

155157
/// Tests that self is an alphabetic character
156158
///
157159
/// Warning: for `&str` it recognizes alphabetic
158160
/// characters outside of the 52 ASCII letters
161+
#[allow(clippy::wrong_self_convention)]
159162
fn is_alpha(self) -> bool;
160163

161164
/// Tests that self is an alphabetic character
162165
/// or a decimal digit
166+
#[allow(clippy::wrong_self_convention)]
163167
fn is_alphanum(self) -> bool;
164168
/// Tests that self is a decimal digit
169+
#[allow(clippy::wrong_self_convention)]
165170
fn is_dec_digit(self) -> bool;
166171
/// Tests that self is an hex digit
172+
#[allow(clippy::wrong_self_convention)]
167173
fn is_hex_digit(self) -> bool;
168174
/// Tests that self is an octal digit
175+
#[allow(clippy::wrong_self_convention)]
169176
fn is_oct_digit(self) -> bool;
170177
/// Gets the len in bytes for self
171178
fn len(self) -> usize;
@@ -178,25 +185,23 @@ impl AsChar for u8 {
178185
}
179186
#[inline]
180187
fn is_alpha(self) -> bool {
181-
(self >= 0x41 && self <= 0x5A) || (self >= 0x61 && self <= 0x7A)
188+
(0x41..=0x5A).contains(&self) || (0x61..=0x7A).contains(&self)
182189
}
183190
#[inline]
184191
fn is_alphanum(self) -> bool {
185192
self.is_alpha() || self.is_dec_digit()
186193
}
187194
#[inline]
188195
fn is_dec_digit(self) -> bool {
189-
self >= 0x30 && self <= 0x39
196+
(0x30..=0x39).contains(&self)
190197
}
191198
#[inline]
192199
fn is_hex_digit(self) -> bool {
193-
(self >= 0x30 && self <= 0x39)
194-
|| (self >= 0x41 && self <= 0x46)
195-
|| (self >= 0x61 && self <= 0x66)
200+
(0x30..=0x39).contains(&self) || (0x41..=0x46).contains(&self) || (0x61..=0x66).contains(&self)
196201
}
197202
#[inline]
198203
fn is_oct_digit(self) -> bool {
199-
self >= 0x30 && self <= 0x37
204+
(0x30..=0x37).contains(&self)
200205
}
201206
#[inline]
202207
fn len(self) -> usize {
@@ -703,6 +708,7 @@ impl<'a> InputTakeAtPosition for &'a str {
703708

704709
/// Indicates whether a comparison was successful, an error, or
705710
/// if more data was needed
711+
#[allow(clippy::derive_partial_eq_without_eq)]
706712
#[derive(Debug, PartialEq)]
707713
pub enum CompareResult {
708714
/// Comparison was successful
@@ -1381,7 +1387,7 @@ impl HexDisplay for [u8] {
13811387
v.push(b'\t');
13821388

13831389
for &byte in chunk {
1384-
if (byte >= 32 && byte <= 126) || byte >= 128 {
1390+
if (32..=126).contains(&byte) || byte >= 128 {
13851391
v.push(byte);
13861392
} else {
13871393
v.push(b'.');

0 commit comments

Comments
 (0)