Skip to content

Commit

Permalink
Fix clippy warnings (#1702)
Browse files Browse the repository at this point in the history
* Unnecessary clone of `i`. Just use `input`.

* Unnecessary return statement removed.

* Removed unnecessary clone calls for error kind parameter.
Replaced auto-deref with explicit deref().
Simplified boolean logic.
Used empty Ranges instead of backwards Ranges.

* clippy Ignore .idea/ folder.

* clippy cargo fmt.

* clippy Avoid suspicious_double_ref_op.

* clippy Remove empty range error.

* clippy Avoid single_match.

* clippy Avoid needless_lifetimes.

* clippy Avoid map_flatten.

* clippy Avoid extra_unused_lifetimes

* clippy Avoid more needless_lifetimes.

* clippy Avoid redundant_slicing.

* fix nostd imports

* fmt

---------

Co-authored-by: Mark English <[email protected]>
  • Loading branch information
Geal and thatmarkenglishguy authored Oct 21, 2023
1 parent d4318f6 commit 8c77435
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 100 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ realworld/
src/generator.rs
.DS_Store
private-docs/
.idea/
2 changes: 1 addition & 1 deletion examples/json2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ fn object<'a>() -> impl Parser<&'a str, Output = HashMap<String, JsonValue>, Err
)
}

fn json_value<'a>() -> JsonParser {
fn json_value() -> JsonParser {
JsonParser
}

Expand Down
55 changes: 21 additions & 34 deletions examples/json_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,16 @@ impl<'a, 'b: 'a> JsonValue<'a, 'b> {
// if we ignored one of the items, skip over the value
if v.offset.get() == previous {
println!("skipping value");
match value(v.data()) {
Ok((i, _)) => {
v.offset(i);
}
Err(_) => {}
if let Ok((i, _)) = value(v.data()) {
v.offset(i);
}
}

match tag::<_, _, ()>("]")(v.data()) {
Ok((i, _)) => {
println!("]");
v.offset(i);
done = true;
return None;
}
Err(_) => {}
if let Ok((i, _)) = tag::<_, _, ()>("]")(v.data()) {
println!("]");
v.offset(i);
done = true;
return None;
};

if first {
Expand Down Expand Up @@ -158,22 +152,16 @@ impl<'a, 'b: 'a> JsonValue<'a, 'b> {
// if we ignored one of the items, skip over the value
if v.offset.get() == previous {
println!("skipping value");
match value(v.data()) {
Ok((i, _)) => {
v.offset(i);
}
Err(_) => {}
if let Ok((i, _)) = value(v.data()) {
v.offset(i);
}
}

match tag::<_, _, ()>("}")(v.data()) {
Ok((i, _)) => {
println!("}}");
v.offset(i);
done = true;
return None;
}
Err(_) => {}
if let Ok((i, _)) = tag::<_, _, ()>("}")(v.data()) {
println!("}}");
v.offset(i);
done = true;
return None;
};

if first {
Expand Down Expand Up @@ -225,18 +213,18 @@ fn parse_str<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, &'a str
escaped(alphanumeric, '\\', one_of("\"n\\"))(i)
}

fn string<'a>(i: &'a str) -> IResult<&'a str, &'a str> {
fn string(i: &str) -> IResult<&str, &str> {
context(
"string",
preceded(char('\"'), cut(terminated(parse_str, char('\"')))),
)(i)
}

fn boolean<'a>(input: &'a str) -> IResult<&'a str, bool> {
fn boolean(input: &str) -> IResult<&str, bool> {
alt((map(tag("false"), |_| false), map(tag("true"), |_| true))).parse(input)
}

fn array<'a>(i: &'a str) -> IResult<&'a str, ()> {
fn array(i: &str) -> IResult<&str, ()> {
context(
"array",
preceded(
Expand All @@ -249,11 +237,11 @@ fn array<'a>(i: &'a str) -> IResult<&'a str, ()> {
)(i)
}

fn key_value<'a>(i: &'a str) -> IResult<&'a str, (&'a str, ())> {
fn key_value(i: &str) -> IResult<&str, (&str, ())> {
separated_pair(preceded(sp, string), cut(preceded(sp, char(':'))), value).parse(i)
}

fn hash<'a>(i: &'a str) -> IResult<&'a str, ()> {
fn hash(i: &str) -> IResult<&str, ()> {
context(
"map",
preceded(
Expand All @@ -266,7 +254,7 @@ fn hash<'a>(i: &'a str) -> IResult<&'a str, ()> {
)(i)
}

fn value<'a>(i: &'a str) -> IResult<&'a str, ()> {
fn value(i: &str) -> IResult<&str, ()> {
preceded(
sp,
alt((
Expand Down Expand Up @@ -308,11 +296,10 @@ fn main() {
.filter_map(|(_, v)| v.object())
.flatten()
.filter_map(|(user, v)| v.object().map(|o| (user, o)))
.map(|(user, o)| {
.flat_map(|(user, o)| {
o.filter(|(k, _)| *k == "city")
.filter_map(move |(_, v)| v.string().map(|s| (user, s)))
})
.flatten()
.collect();

println!("res = {:?}", s);
Expand Down
28 changes: 8 additions & 20 deletions src/bytes/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,31 +548,19 @@ mod tests {
take_while_m_n(m, n, |c: char| c.len() > 1)(s)
}

assert_eq!(
multi_byte_chars("€ latin", 0, 64),
Ok((&" latin"[..], &"€"[..]))
);
assert_eq!(
multi_byte_chars("𝄠 latin", 0, 1),
Ok((&" latin"[..], &"𝄠"[..]))
);
assert_eq!(
multi_byte_chars("باب latin", 0, 64),
Ok((&" latin"[..], &"باب"[..]))
);
assert_eq!(multi_byte_chars("€ latin", 0, 64), Ok((" latin", "€")));
assert_eq!(multi_byte_chars("𝄠 latin", 0, 1), Ok((" latin", "𝄠")));
assert_eq!(multi_byte_chars("باب latin", 0, 64), Ok((" latin", "باب")));
assert_eq!(
multi_byte_chars("💣💢ᾠ latin", 3, 3),
Ok((&" latin"[..], &"💣💢ᾠ"[..]))
);
assert_eq!(
multi_byte_chars("latin", 0, 64),
Ok((&"latin"[..], &""[..]))
Ok((" latin", "💣💢ᾠ"))
);
assert_eq!(multi_byte_chars("باب", 1, 3), Ok((&""[..], &"باب"[..])));
assert_eq!(multi_byte_chars("باب", 1, 2), Ok((&"ب"[..], &"با"[..])));
assert_eq!(multi_byte_chars("latin", 0, 64), Ok(("latin", "")));
assert_eq!(multi_byte_chars("باب", 1, 3), Ok(("", "باب")));
assert_eq!(multi_byte_chars("باب", 1, 2), Ok(("ب", "با")));
assert_eq!(
multi_byte_chars("latin", 1, 64),
Err(Err::Error(Error::new(&"latin"[..], ErrorKind::TakeWhileMN)))
Err(Err::Error(Error::new("latin", ErrorKind::TakeWhileMN)))
);
}
}
12 changes: 5 additions & 7 deletions src/bytes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,11 +955,9 @@ where
let mut index = 0;
let mut res = OM::Output::bind(|| input.new_builder());

let i = input.clone();

while index < i.input_len() {
let current_len = i.input_len();
let remainder = i.take_from(index);
while index < input.input_len() {
let current_len = input.input_len();
let remainder = input.take_from(index);
match self.normal.process::<OM>(remainder.clone()) {
Ok((i2, o)) => {
res = OM::Output::combine(o, res, |o, mut res| {
Expand Down Expand Up @@ -994,7 +992,7 @@ where
})));
}
} else {
match self.transform.process::<OM>(i.take_from(next)) {
match self.transform.process::<OM>(input.take_from(next)) {
Ok((i2, o)) => {
res = OM::Output::combine(o, res, |o, mut res| {
o.extend_into(&mut res);
Expand All @@ -1004,7 +1002,7 @@ where
if OM::Incomplete::is_streaming() {
return Err(Err::Incomplete(Needed::Unknown));
} else {
return Ok((i.take_from(i.input_len()), res));
return Ok((input.take_from(input.input_len()), res));
}
} else {
index = input.offset(&i2);
Expand Down
7 changes: 2 additions & 5 deletions src/character/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ mod tests {
assert_eq!(oct_digit1(d), Err(Err::Error((d, ErrorKind::OctDigit))));
assert_eq!(bin_digit1(a), Err(Err::Error((a, ErrorKind::BinDigit))));
assert_eq!(
bin_digit1::<_, (_, ErrorKind)>(b),
bin_digit1::<_, (_, ErrorKind)>(b),
Ok((&b"234"[..], &b"1"[..]))
);
assert_eq!(bin_digit1(c), Err(Err::Error((c, ErrorKind::BinDigit))));
Expand Down Expand Up @@ -959,10 +959,7 @@ mod tests {
assert_eq!(oct_digit1(c), Err(Err::Error((c, ErrorKind::OctDigit))));
assert_eq!(oct_digit1(d), Err(Err::Error((d, ErrorKind::OctDigit))));
assert_eq!(bin_digit1(a), Err(Err::Error((a, ErrorKind::BinDigit))));
assert_eq!(
bin_digit1::<_, (_, ErrorKind)>(b),
Ok(("234", "1"))
);
assert_eq!(bin_digit1::<_, (_, ErrorKind)>(b), Ok(("234", "1")));
assert_eq!(bin_digit1(c), Err(Err::Error((c, ErrorKind::BinDigit))));
assert_eq!(bin_digit1(d), Err(Err::Error((d, ErrorKind::BinDigit))));
assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(a), Ok((empty, a)));
Expand Down
5 changes: 1 addition & 4 deletions src/character/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,10 +908,7 @@ mod tests {
assert_eq!(oct_digit1(c), Err(Err::Error((c, ErrorKind::OctDigit))));
assert_eq!(oct_digit1(d), Err(Err::Error((d, ErrorKind::OctDigit))));
assert_eq!(bin_digit1(a), Err(Err::Error((a, ErrorKind::BinDigit))));
assert_eq!(
bin_digit1::<_, (_, ErrorKind)>(b),
Ok(("234", "1"))
);
assert_eq!(bin_digit1::<_, (_, ErrorKind)>(b), Ok(("234", "1")));
assert_eq!(bin_digit1(c), Err(Err::Error((c, ErrorKind::BinDigit))));
assert_eq!(bin_digit1(d), Err(Err::Error((d, ErrorKind::BinDigit))));
assert_eq!(
Expand Down
8 changes: 3 additions & 5 deletions src/multi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,11 +813,9 @@ where
.parser
.process::<OutputM<Check, Check, OM::Incomplete>>(input.clone())
{
Err(Err::Error(_)) => {
return Err(Err::Error(OM::Error::bind(move || {
<F as Parser<I>>::Error::from_error_kind(input, ErrorKind::Many1Count)
})))
}
Err(Err::Error(_)) => Err(Err::Error(OM::Error::bind(move || {
<F as Parser<I>>::Error::from_error_kind(input, ErrorKind::Many1Count)
}))),
Err(Err::Failure(e)) => Err(Err::Failure(e)),
Err(Err::Incomplete(i)) => Err(Err::Incomplete(i)),
Ok((mut input, _)) => {
Expand Down
5 changes: 3 additions & 2 deletions src/multi/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
character::streaming::digit1 as digit,
error::{ErrorKind, ParseError},
internal::{Err, IResult, Needed},
lib::std::ops::Range,
lib::std::str::{self, FromStr},
number::streaming::{be_u16, be_u8},
sequence::pair,
Expand Down Expand Up @@ -572,7 +573,7 @@ fn many_test() {
);

fn many_invalid(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
many(2..=1, tag("a")).parse(i)
many(Range::default(), tag("a")).parse(i)
}

let a = &b"a"[..];
Expand Down Expand Up @@ -727,7 +728,7 @@ fn fold_test() {
);

fn fold_invalid(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
fold(2..=1, tag("a"), Vec::new, fold_into_vec).parse(i)
fold(Range::default(), tag("a"), Vec::new, fold_into_vec).parse(i)
}

let a = &b"a"[..];
Expand Down
1 change: 1 addition & 0 deletions src/number/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,7 @@ mod tests {
use crate::error::ErrorKind;
use crate::internal::Err;

#[cfg(feature = "std")]
macro_rules! assert_parse(
($left: expr, $right: expr) => {
let res: $crate::IResult<_, _, (_, ErrorKind)> = $left;
Expand Down
32 changes: 12 additions & 20 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,17 +332,13 @@ impl<'a> Input for &'a [u8] {
P: Fn(Self::Item) -> bool,
{
match self.iter().position(|c| predicate(*c)) {
Some(0) => Err(Err::Error(OM::Error::bind(|| {
E::from_error_kind(self, e)
}))),
Some(0) => Err(Err::Error(OM::Error::bind(|| E::from_error_kind(self, e)))),
Some(n) => Ok((self.take_from(n), OM::Output::bind(|| self.take(n)))),
None => {
if OM::Incomplete::is_streaming() {
Err(Err::Incomplete(Needed::new(1)))
} else if self.is_empty() {
Err(Err::Error(OM::Error::bind(|| {
E::from_error_kind(self, e)
})))
Err(Err::Error(OM::Error::bind(|| E::from_error_kind(self, e))))
} else {
Ok((
self.take_from(self.len()),
Expand Down Expand Up @@ -529,9 +525,7 @@ impl<'a> Input for &'a str {
P: Fn(Self::Item) -> bool,
{
match self.find(predicate) {
Some(0) => Err(Err::Error(OM::Error::bind(|| {
E::from_error_kind(self, e)
}))),
Some(0) => Err(Err::Error(OM::Error::bind(|| E::from_error_kind(self, e)))),
Some(n) => unsafe {
// find() returns a byte index that is already in the slice at a char boundary
Ok((
Expand All @@ -542,10 +536,8 @@ impl<'a> Input for &'a str {
None => {
if OM::Incomplete::is_streaming() {
Err(Err::Incomplete(Needed::new(1)))
} else if self.len() == 0 {
Err(Err::Error(OM::Error::bind(|| {
E::from_error_kind(self, e)
})))
} else if self.is_empty() {
Err(Err::Error(OM::Error::bind(|| E::from_error_kind(self, e))))
} else {
// the end of slice is a char boundary
unsafe {
Expand Down Expand Up @@ -669,7 +661,7 @@ impl AsBytes for [u8] {
impl<'a, const N: usize> AsBytes for &'a [u8; N] {
#[inline(always)]
fn as_bytes(&self) -> &[u8] {
*self
self.as_slice()
}
}

Expand Down Expand Up @@ -734,7 +726,7 @@ impl AsChar for u8 {
}
#[inline]
fn is_bin_digit(self) -> bool {
matches!(self, 0x30..=0x31)
matches!(self, 0x30..=0x31)
}
#[inline]
fn len(self) -> usize {
Expand Down Expand Up @@ -1472,20 +1464,20 @@ impl NomRange<usize> for Range<usize> {
}

fn is_inverted(&self) -> bool {
!(self.start < self.end)
self.start >= self.end
}

fn saturating_iter(&self) -> Self::Saturating {
if self.end == 0 {
1..0
Range::default()
} else {
0..self.end - 1
}
}

fn bounded_iter(&self) -> Self::Bounded {
if self.end == 0 {
1..0
Range::default()
} else {
0..self.end - 1
}
Expand Down Expand Up @@ -1560,15 +1552,15 @@ impl NomRange<usize> for RangeTo<usize> {

fn saturating_iter(&self) -> Self::Saturating {
if self.end == 0 {
1..0
Range::default()
} else {
0..self.end - 1
}
}

fn bounded_iter(&self) -> Self::Bounded {
if self.end == 0 {
1..0
Range::default()
} else {
0..self.end - 1
}
Expand Down
Loading

0 comments on commit 8c77435

Please sign in to comment.