From 4b7d70c4891c485596b03af6c0baac058af1597e Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Wed, 20 Mar 2024 00:15:44 -0700 Subject: [PATCH] Address clippy lints. --- codegen/src/lib.rs | 21 +++++++++++---------- examples/group/src/main.rs | 3 ++- examples/ini/src/main.rs | 6 +++--- lib/src/combinators.rs | 2 -- lib/src/debug.rs | 7 +++---- lib/src/input/cursor.rs | 2 +- lib/src/input/pear.rs | 4 +--- lib/src/input/string.rs | 2 +- lib/src/input/text.rs | 8 +++----- lib/src/macros.rs | 4 ++-- lib/src/result.rs | 24 ++++++++++++------------ 11 files changed, 39 insertions(+), 44 deletions(-) diff --git a/codegen/src/lib.rs b/codegen/src/lib.rs index a32200e..688fb81 100644 --- a/codegen/src/lib.rs +++ b/codegen/src/lib.rs @@ -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) } @@ -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); @@ -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 } } } @@ -124,7 +122,7 @@ fn wrapping_fn_block( args: &AttrArgs, ret_ty: &syn::Type, ) -> PResult { - 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(); @@ -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) } )) }; @@ -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 { @@ -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 { diff --git a/examples/group/src/main.rs b/examples/group/src/main.rs index d7a5fbf..adcbf6d 100644 --- a/examples/group/src/main.rs +++ b/examples/group/src/main.rs @@ -1,3 +1,4 @@ +#![allow(dead_code)] #![warn(rust_2018_idioms)] use pear::input::{Pear, Text}; @@ -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] diff --git a/examples/ini/src/main.rs b/examples/ini/src/main.rs index aee158d..e45321c 100644 --- a/examples/ini/src/main.rs +++ b/examples/ini/src/main.rs @@ -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)?; } } @@ -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)); diff --git a/lib/src/combinators.rs b/lib/src/combinators.rs index d8bed55..71f8628 100644 --- a/lib/src/combinators.rs +++ b/lib/src/combinators.rs @@ -1,5 +1,3 @@ -use std::default::Default; - use crate::input::{Pear, Input, Rewind, Token, Result}; use crate::macros::parser; use crate::parsers::*; diff --git a/lib/src/debug.rs b/lib/src/debug.rs index 6f22429..e030953 100644 --- a/lib/src/debug.rs +++ b/lib/src/debug.rs @@ -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; @@ -35,7 +34,7 @@ impl Tree { // 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. @@ -134,8 +133,8 @@ pub struct TreeDebugger { tree: Tree, } -impl TreeDebugger { - pub fn new() -> Self { +impl Default for TreeDebugger { + fn default() -> Self { Self { tree: Tree::new() } } } diff --git a/lib/src/input/cursor.rs b/lib/src/input/cursor.rs index 9552c5b..84157a7 100644 --- a/lib/src/input/cursor.rs +++ b/lib/src/input/cursor.rs @@ -144,7 +144,7 @@ impl<'a, T: Clone> Indexable for &'a [T] { type Iter = std::iter::Cloned>; fn head(&self) -> Option { - self.get(0).cloned() + self.first().cloned() } fn length_of(_: Self::One) -> usize { diff --git a/lib/src/input/pear.rs b/lib/src/input/pear.rs index fbe7ff3..3082b56 100644 --- a/lib/src/input/pear.rs +++ b/lib/src/input/pear.rs @@ -24,11 +24,9 @@ impl fmt::Debug for Options { impl Default for Options { #[cfg(debug_assertions)] fn default() -> Self { - use crate::debug::TreeDebugger; - let debugger: Box> = Box::new(TreeDebugger::new()); Options { stacked_context: true, - debugger: Some(debugger), + debugger: Some(Box::::default()), } } diff --git a/lib/src/input/string.rs b/lib/src/input/string.rs index d487c96..c64aedf 100644 --- a/lib/src/input/string.rs +++ b/lib/src/input/string.rs @@ -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; diff --git a/lib/src/input/text.rs b/lib/src/input/text.rs index fa3e8ec..140703e 100644 --- a/lib/src/input/text.rs +++ b/lib/src/input/text.rs @@ -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; @@ -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; @@ -187,9 +187,7 @@ impl<'a> Input for Text<'a> { }; Span { start, end, cursor, snippet } - }; - - pos + } } } diff --git a/lib/src/macros.rs b/lib/src/macros.rs index 6fe4bd0..f8ba166 100644 --- a/lib/src/macros.rs +++ b/lib/src/macros.rs @@ -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)); @@ -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) })() }) } diff --git a/lib/src/result.rs b/lib/src/result.rs index 3db4854..2af067c 100644 --- a/lib/src/result.rs +++ b/lib/src/result.rs @@ -10,35 +10,35 @@ use crate::error::ParseError; pub type Result = std::result::Result>; #[doc(hidden)] -pub trait AsResult { - fn as_result(self) -> Result; +pub trait IntoResult { + fn into_result(self) -> Result; } -impl AsResult for T { +impl IntoResult for T { #[inline(always)] - fn as_result(self) -> Result { + fn into_result(self) -> Result { Ok(self) } } -impl AsResult for Result { +impl IntoResult for Result { #[inline(always)] - fn as_result(self) -> Result { + fn into_result(self) -> Result { self } } // // This one will result in inference issues when `Ok(T)` is returned. -// impl AsResult for ::std::result::Result { -// fn as_result(self) -> Result { +// impl IntoResult for ::std::result::Result { +// fn into_result(self) -> Result { // let name = unsafe { ::std::intrinsics::type_name::() }; // self.map_err(|e| ParseError::new(name, e.to_string())) // } // } // // This one won't but makes some things uglier to write. -// impl> AsResult for Result { -// fn as_result(self) -> Result { +// impl> IntoResult for Result { +// fn into_result(self) -> Result { // match self { // Ok(v) => Ok(v), // Err(e) => Err(ParseError { @@ -50,8 +50,8 @@ impl AsResult for Result { // } // // This one won't but makes some things uglier to write. -// impl AsResult for Result { -// fn as_result(self) -> Result { +// impl IntoResult for Result { +// fn into_result(self) -> Result { // self // } // }