From fd3d22406735dba2eca7d22507d9e69233070bc9 Mon Sep 17 00:00:00 2001 From: Kogia-sima Date: Sun, 7 Jun 2020 17:58:52 +0900 Subject: [PATCH] Refactor source code with rustfmt and clippy [skip ci] --- examples/include.rs | 4 ++-- integration-tests/tests/basic.rs | 16 ++++++++++--- sailfish-compiler/src/compiler.rs | 11 ++++++--- sailfish-compiler/src/config.rs | 4 ++-- sailfish-compiler/src/lib.rs | 2 -- sailfish-compiler/src/optimizer.rs | 1 + sailfish-compiler/src/parser.rs | 3 ++- sailfish-compiler/src/procmacro.rs | 2 +- sailfish-compiler/src/resolver.rs | 32 ++++++++++++++----------- sailfish-compiler/src/translator.rs | 8 +++---- sailfish/src/runtime/buffer.rs | 17 ++++++++++--- sailfish/src/runtime/escape/avx2.rs | 2 ++ sailfish/src/runtime/escape/fallback.rs | 14 ++++++----- sailfish/src/runtime/escape/mod.rs | 2 +- sailfish/src/runtime/escape/naive.rs | 2 +- sailfish/src/runtime/escape/sse2.rs | 2 ++ 16 files changed, 79 insertions(+), 43 deletions(-) diff --git a/examples/include.rs b/examples/include.rs index 131e460..ae81efb 100644 --- a/examples/include.rs +++ b/examples/include.rs @@ -7,13 +7,13 @@ use sailfish::TemplateOnce; #[template(path = "include.stpl")] struct Include { title: String, - name: String + name: String, } fn main() { let ctx = Include { title: "Website".to_owned(), - name: "Hanako".to_owned() + name: "Hanako".to_owned(), }; println!("{}", ctx.render_once().unwrap()); } diff --git a/integration-tests/tests/basic.rs b/integration-tests/tests/basic.rs index 8d7f1e4..f838ead 100644 --- a/integration-tests/tests/basic.rs +++ b/integration-tests/tests/basic.rs @@ -88,13 +88,18 @@ struct Include<'a> { #[test] fn test_include() { - assert_render("include", Include { strs: &["foo", "bar"] }); + assert_render( + "include", + Include { + strs: &["foo", "bar"], + }, + ); } #[derive(TemplateOnce)] #[template(path = "big-table.stpl")] struct BigTable { - table: Vec> + table: Vec>, } #[test] @@ -151,7 +156,12 @@ struct RmWhitespace<'a, 'b> { #[test] fn test_rm_whitespace() { - assert_render("rm_whitespace", RmWhitespace { messages: &["foo", "bar"] }); + assert_render( + "rm_whitespace", + RmWhitespace { + messages: &["foo", "bar"], + }, + ); } #[cfg(unix)] diff --git a/sailfish-compiler/src/compiler.rs b/sailfish-compiler/src/compiler.rs index b3ab976..3dd49b2 100644 --- a/sailfish-compiler/src/compiler.rs +++ b/sailfish-compiler/src/compiler.rs @@ -8,12 +8,12 @@ use crate::error::*; use crate::optimizer::Optimizer; use crate::parser::Parser; use crate::resolver::Resolver; -use crate::translator::{Translator, TranslatedSource}; +use crate::translator::{TranslatedSource, Translator}; use crate::util::{read_to_string, rustfmt_block}; #[derive(Default)] pub struct Compiler { - config: Config + config: Config, } impl Compiler { @@ -35,7 +35,12 @@ impl Compiler { translator.translate(stream) } - pub fn compile_file(&self, template_dir: &Path, input: &Path, output: &Path) -> Result<(), Error> { + pub fn compile_file( + &self, + template_dir: &Path, + input: &Path, + output: &Path, + ) -> Result<(), Error> { // TODO: introduce cache system let input = input.canonicalize()?; diff --git a/sailfish-compiler/src/config.rs b/sailfish-compiler/src/config.rs index 5af998e..2d64415 100644 --- a/sailfish-compiler/src/config.rs +++ b/sailfish-compiler/src/config.rs @@ -7,7 +7,7 @@ pub struct Config { pub cache_dir: PathBuf, pub rm_whitespace: bool, #[doc(hidden)] - pub _non_exhaustive: () + pub _non_exhaustive: (), } impl Default for Config { @@ -17,7 +17,7 @@ impl Default for Config { escape: true, cache_dir: Path::new(env!("OUT_DIR")).join("cache"), rm_whitespace: false, - _non_exhaustive: () + _non_exhaustive: (), } } } diff --git a/sailfish-compiler/src/lib.rs b/sailfish-compiler/src/lib.rs index eced6b8..bf2b94e 100644 --- a/sailfish-compiler/src/lib.rs +++ b/sailfish-compiler/src/lib.rs @@ -1,5 +1,3 @@ -#![allow(dead_code)] - #[macro_use] mod error; diff --git a/sailfish-compiler/src/optimizer.rs b/sailfish-compiler/src/optimizer.rs index 2de299c..4693803 100644 --- a/sailfish-compiler/src/optimizer.rs +++ b/sailfish-compiler/src/optimizer.rs @@ -4,6 +4,7 @@ use syn::visit_mut::VisitMut; use syn::{Block, Expr, ExprMacro, Ident, LitStr, Stmt, Token}; struct RenderTextMacroArgument { + #[allow(dead_code)] context: Ident, arg: LitStr, } diff --git a/sailfish-compiler/src/parser.rs b/sailfish-compiler/src/parser.rs index ccabdb5..bb09acb 100644 --- a/sailfish-compiler/src/parser.rs +++ b/sailfish-compiler/src/parser.rs @@ -103,6 +103,7 @@ pub struct ParseStream<'a> { delimiter: char, } +#[allow(dead_code)] impl<'a> ParseStream<'a> { /// Returns an empty `ParseStream` containing no tokens pub fn new() -> Self { @@ -220,7 +221,7 @@ impl<'a> ParseStream<'a> { let end = self .source .find(&*self.block_delimiter.0) - .unwrap_or(self.source.len()); + .unwrap_or_else(|| self.source.len()); let token = Token { content: self.take_n(end), offset, diff --git a/sailfish-compiler/src/procmacro.rs b/sailfish-compiler/src/procmacro.rs index 89ca973..5e5669f 100644 --- a/sailfish-compiler/src/procmacro.rs +++ b/sailfish-compiler/src/procmacro.rs @@ -6,8 +6,8 @@ use syn::parse::{Parse, ParseStream, Result as ParseResult}; use syn::punctuated::Punctuated; use syn::{Fields, Ident, ItemStruct, Lifetime, LitBool, LitChar, LitStr, Token}; -use crate::config::Config; use crate::compiler::Compiler; +use crate::config::Config; use crate::error::*; enum GenericParamName { diff --git a/sailfish-compiler/src/resolver.rs b/sailfish-compiler/src/resolver.rs index 0bda85e..abef9d1 100644 --- a/sailfish-compiler/src/resolver.rs +++ b/sailfish-compiler/src/resolver.rs @@ -23,13 +23,6 @@ macro_rules! return_if_some { }; } -fn empty_block() -> Block { - Block { - brace_token: Default::default(), - stmts: Vec::new(), - } -} - struct ResolverImpl<'s, 'h> { template_dir: &'s Path, path_stack: Vec, @@ -66,10 +59,11 @@ impl<'s, 'h> VisitMut for ResolverImpl<'s, 'h> { // resolve include! for rust file if arg.ends_with(".rs") { - if !arg.starts_with("/") { - let absolute_path = self.path_stack.last().unwrap().parent().unwrap().join(arg); + if !arg.starts_with('/') { + let absolute_path = + self.path_stack.last().unwrap().parent().unwrap().join(arg); let absolute_path_str = absolute_path.to_string_lossy(); - em.mac.tokens = quote!{ include!(#absolute_path_str) }; + em.mac.tokens = quote! { include!(#absolute_path_str) }; } return; } @@ -81,7 +75,12 @@ impl<'s, 'h> VisitMut for ResolverImpl<'s, 'h> { self.template_dir.join(&arg[1..]) } else { // relative include - self.path_stack.last().unwrap().parent().unwrap().join(arg.clone()) + self.path_stack + .last() + .unwrap() + .parent() + .unwrap() + .join(arg.clone()) }; // parse and translate the child template @@ -135,13 +134,18 @@ impl<'h> Resolver<'h> { } #[inline] - pub fn resolve(&self, template_dir: &Path, input_file: &Path, ast: &mut Block) -> Result<(), Error> { + pub fn resolve( + &self, + template_dir: &Path, + input_file: &Path, + ast: &mut Block, + ) -> Result<(), Error> { let mut child = ResolverImpl { - template_dir: template_dir, + template_dir, path_stack: vec![input_file.to_owned()], deps: Vec::new(), error: None, - include_handler: Arc::clone(&self.include_handler) + include_handler: Arc::clone(&self.include_handler), }; child.visit_block_mut(ast); diff --git a/sailfish-compiler/src/translator.rs b/sailfish-compiler/src/translator.rs index 79291fd..095b863 100644 --- a/sailfish-compiler/src/translator.rs +++ b/sailfish-compiler/src/translator.rs @@ -18,10 +18,10 @@ pub struct SourceMap { } impl SourceMap { - #[inline] - pub fn entries(&self) -> &[SourceMapEntry] { - &*self.entries - } + // #[inline] + // pub fn entries(&self) -> &[SourceMapEntry] { + // &*self.entries + // } pub fn reverse_mapping(&self, offset: usize) -> Option { // find entry which satisfies entry.new <= offset < entry.new + entry.length diff --git a/sailfish/src/runtime/buffer.rs b/sailfish/src/runtime/buffer.rs index 1c2f10a..ce817a3 100644 --- a/sailfish/src/runtime/buffer.rs +++ b/sailfish/src/runtime/buffer.rs @@ -58,9 +58,20 @@ impl Buffer { self.capacity } + /// Force the length of buffer to `new_len` + /// + /// # Safety + /// + /// - `new_len` must be less than or equal to `capacity()` + /// - The elements at `old_len..new_len` must be initialized #[inline] - pub unsafe fn set_len(&mut self, new: usize) { - self.len = new; + pub unsafe fn set_len(&mut self, new_len: usize) { + self.len = new_len; + } + + #[inline] + pub fn is_empty(&self) -> bool { + self.len == 0 } pub fn reserve(&mut self, size: usize) { @@ -145,7 +156,7 @@ impl From for Buffer { Buffer { data: other.as_mut_ptr(), len: other.len(), - capacity: other.capacity() + capacity: other.capacity(), } } else { Buffer::new() diff --git a/sailfish/src/runtime/escape/avx2.rs b/sailfish/src/runtime/escape/avx2.rs index b714dc8..deca64f 100644 --- a/sailfish/src/runtime/escape/avx2.rs +++ b/sailfish/src/runtime/escape/avx2.rs @@ -1,3 +1,5 @@ +#![allow(clippy::cast_ptr_alignment)] + #[cfg(target_arch = "x86")] use std::arch::x86::*; #[cfg(target_arch = "x86_64")] diff --git a/sailfish/src/runtime/escape/fallback.rs b/sailfish/src/runtime/escape/fallback.rs index 9e75372..f3b5852 100644 --- a/sailfish/src/runtime/escape/fallback.rs +++ b/sailfish/src/runtime/escape/fallback.rs @@ -1,3 +1,5 @@ +#![allow(clippy::cast_ptr_alignment)] + use super::naive; #[cfg(target_pointer_width = "16")] @@ -13,8 +15,8 @@ const USIZE_ALIGN: usize = USIZE_BYTES - 1; #[inline(always)] fn contains_zero_byte(x: usize) -> bool { - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; + const LO_U64: u64 = 0x0101_0101_0101_0101; + const HI_U64: u64 = 0x8080_8080_8080_8080; const LO_USIZE: usize = LO_U64 as usize; const HI_USIZE: usize = HI_U64 as usize; @@ -23,10 +25,10 @@ fn contains_zero_byte(x: usize) -> bool { #[inline] fn contains_key(x: usize) -> bool { - const INDEPENDENTS1: usize = 0x0404040404040404_u64 as usize; - const INDEPENDENTS2: usize = 0x0202020202020202_u64 as usize; - const KEY1: usize = 0x2626262626262626_u64 as usize; - const KEY2: usize = 0x3e3e3e3e3e3e3e3e_u64 as usize; + const INDEPENDENTS1: usize = 0x0404_0404_0404_0404_u64 as usize; + const INDEPENDENTS2: usize = 0x0202_0202_0202_0202_u64 as usize; + const KEY1: usize = 0x2626_2626_2626_2626_u64 as usize; + const KEY2: usize = 0x3e3e_3e3e_3e3e_3e3e_u64 as usize; let y1 = x | INDEPENDENTS1; let y2 = x | INDEPENDENTS2; diff --git a/sailfish/src/runtime/escape/mod.rs b/sailfish/src/runtime/escape/mod.rs index 7d7436c..c13ea39 100644 --- a/sailfish/src/runtime/escape/mod.rs +++ b/sailfish/src/runtime/escape/mod.rs @@ -22,7 +22,7 @@ static ESCAPE_LUT: [u8; 256] = [ 9, 9, 9, 9, ]; -const ESCAPED: [&'static str; 4] = [""", "&", "<", ">"]; +const ESCAPED: [&str; 4] = [""", "&", "<", ">"]; const ESCAPED_LEN: usize = 4; /// write the escaped contents with custom function diff --git a/sailfish/src/runtime/escape/naive.rs b/sailfish/src/runtime/escape/naive.rs index a2b513f..6087ef5 100644 --- a/sailfish/src/runtime/escape/naive.rs +++ b/sailfish/src/runtime/escape/naive.rs @@ -42,5 +42,5 @@ pub(super) unsafe fn proceed( debug_assert_eq!(ptr, end_ptr); debug_assert!(start_ptr <= ptr); - return start_ptr; + start_ptr } diff --git a/sailfish/src/runtime/escape/sse2.rs b/sailfish/src/runtime/escape/sse2.rs index 681fe7f..31a7168 100644 --- a/sailfish/src/runtime/escape/sse2.rs +++ b/sailfish/src/runtime/escape/sse2.rs @@ -1,3 +1,5 @@ +#![allow(clippy::cast_ptr_alignment)] + #[cfg(target_arch = "x86")] use std::arch::x86::*; #[cfg(target_arch = "x86_64")]