Skip to content

Commit

Permalink
Resolve all clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhoo committed Mar 9, 2020
1 parent 82ddd42 commit b9c14da
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 36 deletions.
65 changes: 31 additions & 34 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub enum EvalResult {
macro_rules! result_opt (
(fn $n:ident: $e:ident -> $t:ty) => (
#[allow(dead_code)]
#[allow(clippy::wrong_self_convention)]
fn $n(self) -> Option<$t> {
if let EvalResult::$e(v) = self {
Some(v)
Expand All @@ -76,6 +77,7 @@ impl EvalResult {
result_opt!(fn as_char: Char -> CChar);
result_opt!(fn as_str: Str -> Vec<u8>);

#[allow(clippy::wrong_self_convention)]
fn as_numeric(self) -> Option<EvalResult> {
match self {
EvalResult::Int(_) | EvalResult::Float(_) => Some(self),
Expand Down Expand Up @@ -149,22 +151,19 @@ fn one_of_punctuation(c: &'static [&'static str]) -> impl Fn(&[Token]) -> CResul
.map(|opt| opt.len())
.min()
.expect("at least one option");
let res: CResult<'_, &[u8]> = Err(crate::nom::Err::Incomplete(Needed::Size(min)));
res
Err(crate::nom::Err::Incomplete(Needed::Size(min)))
} else if input[0].kind == TokenKind::Punctuation
&& c.iter().any(|opt| opt.as_bytes() == &input[0].raw[..])
{
Ok((&input[1..], &input[0].raw[..]))
} else {
if input[0].kind == TokenKind::Punctuation
&& c.iter().any(|opt| opt.as_bytes() == &input[0].raw[..])
{
Ok((&input[1..], &input[0].raw[..]))
} else {
Err(crate::nom::Err::Error(
(
input,
crate::ErrorKind::ExactTokens(TokenKind::Punctuation, c),
)
.into(),
))
}
Err(crate::nom::Err::Error(
(
input,
crate::ErrorKind::ExactTokens(TokenKind::Punctuation, c),
)
.into(),
))
}
}
}
Expand Down Expand Up @@ -304,7 +303,7 @@ where
}

impl<'a> PRef<'a> {
fn unary<'t>(&self, input: &'t [Token]) -> CResult<'t, EvalResult> {
fn unary(self, input: &'_ [Token]) -> CResult<'_, EvalResult> {
alt((
delimited(p("("), |i| self.numeric_expr(i), p(")")),
numeric(|i| self.literal(i)),
Expand All @@ -316,7 +315,7 @@ impl<'a> PRef<'a> {
))(input)
}

fn mul_div_rem<'t>(&self, input: &'t [Token]) -> CResult<'t, EvalResult> {
fn mul_div_rem(self, input: &'_ [Token]) -> CResult<'_, EvalResult> {
let (input, acc) = self.unary(input)?;
fold_many0(
pair(complete(one_of_punctuation(&["*", "/", "%"][..])), |i| {
Expand All @@ -335,7 +334,7 @@ impl<'a> PRef<'a> {
)(input)
}

fn add_sub<'t>(&self, input: &'t [Token]) -> CResult<'t, EvalResult> {
fn add_sub(self, input: &'_ [Token]) -> CResult<'_, EvalResult> {
let (input, acc) = self.mul_div_rem(input)?;
fold_many0(
pair(complete(one_of_punctuation(&["+", "-"][..])), |i| {
Expand All @@ -353,7 +352,7 @@ impl<'a> PRef<'a> {
)(input)
}

fn shl_shr<'t>(&self, input: &'t [Token]) -> CResult<'t, EvalResult> {
fn shl_shr(self, input: &'_ [Token]) -> CResult<'_, EvalResult> {
let (input, acc) = self.add_sub(input)?;
numeric(fold_many0(
pair(complete(one_of_punctuation(&["<<", ">>"][..])), |i| {
Expand All @@ -371,7 +370,7 @@ impl<'a> PRef<'a> {
))(input)
}

fn and<'t>(&self, input: &'t [Token]) -> CResult<'t, EvalResult> {
fn and(self, input: &'_ [Token]) -> CResult<'_, EvalResult> {
let (input, acc) = self.shl_shr(input)?;
numeric(fold_many0(
preceded(complete(p("&")), |i| self.shl_shr(i)),
Expand All @@ -383,7 +382,7 @@ impl<'a> PRef<'a> {
))(input)
}

fn xor<'t>(&self, input: &'t [Token]) -> CResult<'t, EvalResult> {
fn xor(self, input: &'_ [Token]) -> CResult<'_, EvalResult> {
let (input, acc) = self.and(input)?;
numeric(fold_many0(
preceded(complete(p("^")), |i| self.and(i)),
Expand All @@ -395,7 +394,7 @@ impl<'a> PRef<'a> {
))(input)
}

fn or<'t>(&self, input: &'t [Token]) -> CResult<'t, EvalResult> {
fn or(self, input: &'_ [Token]) -> CResult<'_, EvalResult> {
let (input, acc) = self.xor(input)?;
numeric(fold_many0(
preceded(complete(p("|")), |i| self.xor(i)),
Expand All @@ -408,7 +407,7 @@ impl<'a> PRef<'a> {
}

#[inline(always)]
fn numeric_expr<'t>(&self, input: &'t [Token]) -> CResult<'t, EvalResult> {
fn numeric_expr(self, input: &'_ [Token]) -> CResult<'_, EvalResult> {
self.or(input)
}
}
Expand All @@ -418,7 +417,7 @@ impl<'a> PRef<'a> {
// =======================================================

impl<'a> PRef<'a> {
fn identifier<'t>(&self, input: &'t [Token]) -> CResult<'t, EvalResult> {
fn identifier(self, input: &'_ [Token]) -> CResult<'_, EvalResult> {
match input.split_first() {
None => Err(Err::Incomplete(Needed::Size(1))),
Some((
Expand All @@ -442,7 +441,7 @@ impl<'a> PRef<'a> {
}
}

fn literal<'t>(&self, input: &'t [Token]) -> CResult<'t, EvalResult> {
fn literal(self, input: &'_ [Token]) -> CResult<'_, EvalResult> {
match input.split_first() {
None => Err(Err::Incomplete(Needed::Size(1))),
Some((
Expand All @@ -461,7 +460,7 @@ impl<'a> PRef<'a> {
}
}

fn string<'t>(&self, input: &'t [Token]) -> CResult<'t, Vec<u8>> {
fn string(self, input: &'_ [Token]) -> CResult<'_, Vec<u8>> {
alt((
map_opt(|i| self.literal(i), EvalResult::as_str),
map_opt(|i| self.identifier(i), EvalResult::as_str),
Expand All @@ -470,7 +469,7 @@ impl<'a> PRef<'a> {
}

// "string1" "string2" etc...
fn concat_str<'t>(&self, input: &'t [Token]) -> CResult<'t, EvalResult> {
fn concat_str(self, input: &'_ [Token]) -> CResult<'_, EvalResult> {
map(
pair(|i| self.string(i), many0(complete(|i| self.string(i)))),
|(first, v)| {
Expand All @@ -485,7 +484,7 @@ impl<'a> PRef<'a> {
.to_cexpr_result()
}

fn expr<'t>(&self, input: &'t [Token]) -> CResult<'t, EvalResult> {
fn expr(self, input: &'_ [Token]) -> CResult<'_, EvalResult> {
alt((
|i| self.numeric_expr(i),
delimited(p("("), |i| self.expr(i), p(")")),
Expand All @@ -496,7 +495,7 @@ impl<'a> PRef<'a> {
.to_cexpr_result()
}

fn macro_definition<'t>(&self, input: &'t [Token]) -> CResult<'t, (&'t [u8], EvalResult)> {
fn macro_definition(self, input: &'_ [Token]) -> CResult<'_, (&'_ [u8], EvalResult)> {
pair(typed_token!(Identifier), |i| self.expr(i))(input)
}
}
Expand All @@ -517,9 +516,7 @@ impl<'ident> IdentifierParser<'ident> {
/// a known identifier is encountered during parsing, it is substituted
/// for the value specified.
pub fn new(identifiers: &HashMap<Vec<u8>, EvalResult>) -> IdentifierParser<'_> {
IdentifierParser {
identifiers: identifiers,
}
IdentifierParser { identifiers }
}

/// Parse and evalute an expression of a list of tokens.
Expand Down Expand Up @@ -559,7 +556,7 @@ impl<'ident> IdentifierParser<'ident> {
///
/// Returns an error if the input is not a valid expression or if the token
/// stream contains comments, keywords or identifiers.
pub fn expr<'a>(input: &'a [Token]) -> CResult<'a, EvalResult> {
pub fn expr(input: &[Token]) -> CResult<'_, EvalResult> {
IdentifierParser::new(&HashMap::new()).expr(input)
}

Expand All @@ -571,7 +568,7 @@ pub fn expr<'a>(input: &'a [Token]) -> CResult<'a, EvalResult> {
/// Returns an error if the replacement is not a valid expression, if called
/// on a function-like macro, or if the token stream contains comments,
/// keywords or identifiers.
pub fn macro_definition<'a>(input: &'a [Token]) -> CResult<'a, (&'a [u8], EvalResult)> {
pub fn macro_definition(input: &[Token]) -> CResult<'_, (&'_ [u8], EvalResult)> {
IdentifierParser::new(&HashMap::new()).macro_definition(input)
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ where
{
match result.to_cexpr_result() {
Ok((rem, output)) => {
if rem.len() == 0 {
if rem.is_empty() {
Ok((rem, output))
} else {
Err(nom::Err::Error((rem, ErrorKind::Partial).into()))
Expand Down
2 changes: 1 addition & 1 deletion src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct Token {
impl<'a> From<(Kind, &'a [u8])> for Token {
fn from((kind, value): (Kind, &'a [u8])) -> Token {
Token {
kind: kind,
kind,
raw: value.to_owned().into_boxed_slice(),
}
}
Expand Down

0 comments on commit b9c14da

Please sign in to comment.