Skip to content

Commit

Permalink
Address clippy lints.
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioBenitez committed Mar 20, 2024
1 parent 328651c commit 4b7d70c
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 44 deletions.
21 changes: 11 additions & 10 deletions codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ use proc_macro2_diagnostics::{Diagnostic, SpanDiagnosticExt};
use crate::parser::*;

fn parse_marker_ident(span: proc_macro2::Span) -> syn::Ident {
const PARSE_MARKER_IDENT: &'static str = "____parse_parse_marker";
const PARSE_MARKER_IDENT: &str = "____parse_parse_marker";
syn::Ident::new(PARSE_MARKER_IDENT, span)
}

fn parser_info_ident(span: proc_macro2::Span) -> syn::Ident {
const PARSE_INFO_IDENT: &'static str = "____parse_parser_info";
const PARSE_INFO_IDENT: &str = "____parse_parser_info";
syn::Ident::new(PARSE_INFO_IDENT, span)
}

Expand Down Expand Up @@ -71,7 +71,7 @@ impl VisitMut for ParserTransformer {
}

fn visit_macro_mut(&mut self, m: &mut syn::Macro) {
if let Some(ref segment) = m.path.segments.last() {
if let Some(segment) = m.path.segments.last() {
let name = segment.ident.to_string();
if name == "switch" || name.starts_with("parse_") {
let (input, output) = (&self.input, &self.output);
Expand All @@ -90,8 +90,6 @@ impl VisitMut for ParserTransformer {

let parser_info = quote!([#info; #input; #mark; #output]);
m.tokens = quote_spanned!(m.span() => #parser_info #tokens);
} else {
return
}
}
}
Expand Down Expand Up @@ -124,7 +122,7 @@ fn wrapping_fn_block(
args: &AttrArgs,
ret_ty: &syn::Type,
) -> PResult<syn::Block> {
let (input, input_ty) = extract_input_ident_ty(&function)?;
let (input, input_ty) = extract_input_ident_ty(function)?;
let fn_block = &function.block;

let span = function.span();
Expand All @@ -138,8 +136,8 @@ fn wrapping_fn_block(
),
false => quote_spanned!(span => (
|#info_ident: &#scope::input::ParserInfo, #mark_ident: &mut <#input_ty as #scope::input::Input>::Marker| {
use #scope::result::AsResult;
AsResult::as_result(#fn_block)
use #scope::result::IntoResult;
IntoResult::into_result(#fn_block)
}
))
};
Expand Down Expand Up @@ -182,7 +180,7 @@ fn wrapping_fn_block(
};

syn::parse(new_block_tokens.into())
.map_err(|e| function.span().error(format!("bad function: {}", e)).into())
.map_err(|e| function.span().error(format!("bad function: {}", e)))
}

fn parser_attribute(input: proc_macro::TokenStream, args: &AttrArgs) -> PResult<TokenStream> {
Expand All @@ -209,7 +207,10 @@ fn parser_attribute(input: proc_macro::TokenStream, args: &AttrArgs) -> PResult<
function.block = Box::new(wrapping_fn_block(&function, scope, args, &ret_ty)?);
function.attrs.extend(inline);

Ok(quote!(#function))
Ok(quote! {
#[allow(clippy::all, clippy::pedantic, clippy::nursery)]
#function
})
}

impl Case {
Expand Down
3 changes: 2 additions & 1 deletion examples/group/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(dead_code)]
#![warn(rust_2018_idioms)]

use pear::input::{Pear, Text};
Expand Down Expand Up @@ -30,7 +31,7 @@ fn is_whitespace(&byte: &char) -> bool {

#[inline]
fn is_ident_char(&byte: &char) -> bool {
match byte { '0'..='9' | 'a'..='z' | 'A'..='Z' => true, _ => false }
matches!(byte, '0'..='9' | 'a'..='z' | 'A'..='Z')
}

#[inline]
Expand Down
6 changes: 3 additions & 3 deletions examples/ini/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ impl fmt::Display for IniConfig<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for section in self.sections.iter() {
if let Some(name) = section.name {
write!(f, "[({})]\n", name)?;
writeln!(f, "[({})]", name)?;
}

for property in section.properties.iter() {
write!(f, "({})=({})\n", property.name, property.value)?;
writeln!(f, "({})=({})", property.name, property.value)?;
}
}

Expand All @@ -63,7 +63,7 @@ fn is_whitespace(&byte: &char) -> bool {

#[inline]
fn is_num_char(&byte: &char) -> bool {
match byte { '0'..='9' | '.' => true, _ => false }
matches!(byte, '0'..='9' | '.')
}

parse_declare!(Input<'a>(Token = char, Slice = &'a str, Many = &'a str));
Expand Down
2 changes: 0 additions & 2 deletions lib/src/combinators.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::default::Default;

use crate::input::{Pear, Input, Rewind, Token, Result};
use crate::macros::parser;
use crate::parsers::*;
Expand Down
7 changes: 3 additions & 4 deletions lib/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::collections::HashMap;
use inlinable_string::InlinableString;

use crate::input::{Show, Input, Debugger, ParserInfo};
use crate::macros::is_parse_debug;

type Index = usize;

Expand Down Expand Up @@ -35,7 +34,7 @@ impl<T> Tree<T> {
// If the stack indicates we have a parent, add to its children.
if !self.stack.is_empty() {
let parent = self.stack[self.stack.len() - 1];
self.children.entry(parent).or_insert(vec![]).push(index);
self.children.entry(parent).or_default().push(index);
}

// Make this the new parent.
Expand Down Expand Up @@ -134,8 +133,8 @@ pub struct TreeDebugger {
tree: Tree<Info>,
}

impl TreeDebugger {
pub fn new() -> Self {
impl Default for TreeDebugger {
fn default() -> Self {
Self { tree: Tree::new() }
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/input/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl<'a, T: Clone> Indexable for &'a [T] {
type Iter = std::iter::Cloned<std::slice::Iter<'a, T>>;

fn head(&self) -> Option<Self::One> {
self.get(0).cloned()
self.first().cloned()
}

fn length_of(_: Self::One) -> usize {
Expand Down
4 changes: 1 addition & 3 deletions lib/src/input/pear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ impl<I> fmt::Debug for Options<I> {
impl<I: Input> Default for Options<I> {
#[cfg(debug_assertions)]
fn default() -> Self {
use crate::debug::TreeDebugger;
let debugger: Box<dyn Debugger<I>> = Box::new(TreeDebugger::new());
Options {
stacked_context: true,
debugger: Some(debugger),
debugger: Some(Box::<crate::debug::TreeDebugger>::default()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/input/string.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub use crate::input::{Input, Token, Slice, ParserInfo};
pub use crate::input::{Input, ParserInfo};

impl<'a> Input for &'a str {
type Token = char;
Expand Down
8 changes: 3 additions & 5 deletions lib/src/input/text.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub use crate::input::{Input, Rewind, Token, Slice, Show, ParserInfo};
pub use crate::input::{Input, Rewind, Show, ParserInfo};

#[cfg(feature = "color")]
use yansi::Paint;
Expand Down Expand Up @@ -166,7 +166,7 @@ impl<'a> Input for Text<'a> {
fn context(&mut self, mark: Self::Marker) -> Self::Context {
let cursor = self.token();
let bytes_read = self.start.len() - self.current.len();
let pos = if bytes_read == 0 {
if bytes_read == 0 {
Span { start: (1, 1, 0), end: (1, 1, 0), snippet: None, cursor }
} else {
let start_offset = mark;
Expand All @@ -187,9 +187,7 @@ impl<'a> Input for Text<'a> {
};

Span { start, end, cursor, snippet }
};

pos
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ macro_rules! parse {
(move || {
let result = $parser(input)?;
$crate::parsers::eof(input).map_err(|e| e.into())?;
$crate::result::AsResult::as_result(result)
$crate::result::IntoResult::into_result(result)
})()
});
($parser:ident : $e:expr) => (parse!($parser(): $e));
Expand All @@ -51,7 +51,7 @@ macro_rules! parse {
(move || {
let result = $parser(&mut input $(, $x)*)?;
$crate::parsers::eof(&mut input).map_err(|e| e.into())?;
$crate::result::AsResult::as_result(result)
$crate::result::IntoResult::into_result(result)
})()
})
}
Expand Down
24 changes: 12 additions & 12 deletions lib/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,35 @@ use crate::error::ParseError;
pub type Result<T, C, E> = std::result::Result<T, ParseError<C, E>>;

#[doc(hidden)]
pub trait AsResult<T, C, E> {
fn as_result(self) -> Result<T, C, E>;
pub trait IntoResult<T, C, E> {
fn into_result(self) -> Result<T, C, E>;
}

impl<T, C, E> AsResult<T, C, E> for T {
impl<T, C, E> IntoResult<T, C, E> for T {
#[inline(always)]
fn as_result(self) -> Result<T, C, E> {
fn into_result(self) -> Result<T, C, E> {
Ok(self)
}
}

impl<T, C, E> AsResult<T, C, E> for Result<T, C, E> {
impl<T, C, E> IntoResult<T, C, E> for Result<T, C, E> {
#[inline(always)]
fn as_result(self) -> Result<T, C, E> {
fn into_result(self) -> Result<T, C, E> {
self
}
}

// // This one will result in inference issues when `Ok(T)` is returned.
// impl<T, I: Input, E: ::std::fmt::Display> AsResult<T, I> for ::std::result::Result<T, E> {
// fn as_result(self) -> Result<T, I> {
// impl<T, I: Input, E: ::std::fmt::Display> IntoResult<T, I> for ::std::result::Result<T, E> {
// fn into_result(self) -> Result<T, I> {
// let name = unsafe { ::std::intrinsics::type_name::<E>() };
// self.map_err(|e| ParseError::new(name, e.to_string()))
// }
// }

// // This one won't but makes some things uglier to write.
// impl<T, I: Input, E2, E1: Into<E2>> AsResult<T, I, E2> for Result<T, I, E1> {
// fn as_result(self) -> Result<T, I, E2> {
// impl<T, I: Input, E2, E1: Into<E2>> IntoResult<T, I, E2> for Result<T, I, E1> {
// fn into_result(self) -> Result<T, I, E2> {
// match self {
// Ok(v) => Ok(v),
// Err(e) => Err(ParseError {
Expand All @@ -50,8 +50,8 @@ impl<T, C, E> AsResult<T, C, E> for Result<T, C, E> {
// }

// // This one won't but makes some things uglier to write.
// impl<T, I: Input, E> AsResult<T, I, B> for Result<T, I, A> {
// fn as_result(self) -> Result<T, I, B> {
// impl<T, I: Input, E> IntoResult<T, I, B> for Result<T, I, A> {
// fn into_result(self) -> Result<T, I, B> {
// self
// }
// }

0 comments on commit 4b7d70c

Please sign in to comment.