Browse Source

pass clippy

Evan Almloff 3 years ago
parent
commit
ffc9bf763a

+ 23 - 19
packages/rsx/src/ifmt.rs

@@ -1,3 +1,5 @@
+use std::str::FromStr;
+
 use proc_macro2::{Span, TokenStream};
 
 use quote::{quote, ToTokens};
@@ -12,7 +14,7 @@ pub fn format_args_f_impl(input: IfmtInput) -> Result<TokenStream> {
     let mut expr_counter = 0;
     for segment in input.segments.iter() {
         match segment {
-            Segment::Literal(s) => format_literal += &s,
+            Segment::Literal(s) => format_literal += s,
             Segment::Formatted {
                 format_args,
                 segment,
@@ -35,24 +37,24 @@ pub fn format_args_f_impl(input: IfmtInput) -> Result<TokenStream> {
     }
 
     let positional_args = input.segments.iter().filter_map(|seg| {
-        if let Segment::Formatted { segment, .. } = seg {
-            if let FormattedSegment::Expr(expr) = segment {
-                Some(expr)
-            } else {
-                None
-            }
+        if let Segment::Formatted {
+            segment: FormattedSegment::Expr(expr),
+            ..
+        } = seg
+        {
+            Some(expr)
         } else {
             None
         }
     });
 
     let named_args = input.segments.iter().filter_map(|seg| {
-        if let Segment::Formatted { segment, .. } = seg {
-            if let FormattedSegment::Ident(ident) = segment {
-                Some(quote! {#ident = #ident})
-            } else {
-                None
-            }
+        if let Segment::Formatted {
+            segment: FormattedSegment::Ident(ident),
+            ..
+        } = seg
+        {
+            Some(quote! {#ident = #ident})
         } else {
             None
         }
@@ -73,8 +75,10 @@ pub struct IfmtInput {
     pub segments: Vec<Segment>,
 }
 
-impl IfmtInput {
-    pub fn from_str(input: &str) -> Result<Self> {
+impl FromStr for IfmtInput {
+    type Err = syn::Error;
+
+    fn from_str(input: &str) -> Result<Self> {
         let mut chars = input.chars().peekable();
         let mut segments = Vec::new();
         let mut current_literal = String::new();
@@ -90,7 +94,7 @@ impl IfmtInput {
                 while let Some(c) = chars.next() {
                     if c == ':' {
                         let mut current_format_args = String::new();
-                        while let Some(c) = chars.next() {
+                        for c in chars.by_ref() {
                             if c == '}' {
                                 segments.push(Segment::Formatted {
                                     format_args: current_format_args,
@@ -131,20 +135,20 @@ pub enum Segment {
 
 #[derive(Debug)]
 pub enum FormattedSegment {
-    Expr(Expr),
+    Expr(Box<Expr>),
     Ident(Ident),
 }
 
 impl FormattedSegment {
     fn parse(input: &str) -> Result<Self> {
         if let Ok(ident) = parse_str::<Ident>(input) {
-            if &ident.to_string() == input {
+            if ident == input {
                 return Ok(Self::Ident(ident));
             }
         }
         // if let Ok(expr) = parse_str(&("{".to_string() + input + "}")) {
         if let Ok(expr) = parse_str(input) {
-            Ok(Self::Expr(expr))
+            Ok(Self::Expr(Box::new(expr)))
         } else {
             Err(Error::new(
                 Span::call_site(),

+ 1 - 4
packages/rsx/src/node.rs

@@ -25,10 +25,7 @@ pub enum BodyNode {
 
 impl BodyNode {
     pub fn is_litstr(&self) -> bool {
-        match self {
-            BodyNode::Text(_) => true,
-            _ => false,
-        }
+        matches!(self, BodyNode::Text(_))
     }
 }
 

+ 5 - 4
packages/rsx_interpreter/src/captuered_context.rs

@@ -26,8 +26,10 @@ impl CapturedContextBuilder {
     }
 
     pub fn from_call_body(body: CallBody) -> Result<Self> {
-        let mut new = Self::default();
-        new.custom_context = body.custom_context;
+        let mut new = Self {
+            custom_context: body.custom_context,
+            ..Default::default()
+        };
         for node in body.roots {
             new.extend(Self::find_captured(node)?);
         }
@@ -102,8 +104,7 @@ impl ToTokens for CapturedContextBuilder {
         });
         let captured: Vec<_> = ifmted
             .iter()
-            .map(|input| input.segments.iter())
-            .flatten()
+            .flat_map(|input| input.segments.iter())
             .filter_map(|seg| match seg {
                 Segment::Formatted {
                     format_args,

+ 3 - 3
packages/rsx_interpreter/src/elements.rs

@@ -65,9 +65,9 @@ macro_rules! builder_constructors {
         ];
     };
 }
-pub const ELEMENTS_WITH_MAPPED_ATTRIBUTES: &'static [(
-    &'static str,
-    &'static [(&'static str, &'static str)],
+pub const ELEMENTS_WITH_MAPPED_ATTRIBUTES: &[(
+    &str,
+    &[(&str, &str)],
 )] = &[
     ("script", &[("r#type", "type"), ("r#script", "script")]),
     ("button", &[("r#type", "type")]),

+ 17 - 21
packages/rsx_interpreter/src/interperter.rs

@@ -2,6 +2,7 @@ use dioxus_core::{Attribute, AttributeValue, NodeFactory, VNode};
 use dioxus_rsx::{BodyNode, CallBody, ElementAttr, IfmtInput, Segment};
 use quote::ToTokens;
 use quote::__private::Span;
+use std::str::FromStr;
 use syn::{parse2, parse_str, Expr};
 
 use crate::attributes::attrbute_to_static_str;
@@ -55,7 +56,7 @@ pub fn build<'a>(
     }
 
     if children_built.len() == 1 {
-        return Ok(children_built.pop().unwrap());
+        Ok(children_built.pop().unwrap())
     } else {
         Ok(factory.fragment_from_iter(children_built.iter()))
     }
@@ -158,29 +159,24 @@ fn build_node<'a>(
             }
             let listeners = bump.alloc(Vec::new());
             for attr in el.attributes {
-                match attr.attr {
-                    ElementAttr::EventTokens { .. } => {
-                        let expr: Expr = parse2(attr.to_token_stream()).map_err(|err| {
-                            Error::ParseError(ParseError::new(err, ctx.location.clone()))
-                        })?;
-                        if let Some(idx) = ctx.listeners.iter().position(|(code, _)| {
-                            if let Ok(parsed) = parse_str::<Expr>(*code) {
-                                parsed == expr
-                            } else {
-                                false
-                            }
-                        }) {
-                            let (_, listener) = ctx.listeners.remove(idx);
-                            listeners.push(listener)
+                if let ElementAttr::EventTokens { .. } = attr.attr {
+                    let expr: Expr = parse2(attr.to_token_stream()).map_err(|err| {
+                        Error::ParseError(ParseError::new(err, ctx.location.clone()))
+                    })?;
+                    if let Some(idx) = ctx.listeners.iter().position(|(code, _)| {
+                        if let Ok(parsed) = parse_str::<Expr>(*code) {
+                            parsed == expr
                         } else {
-                            return Err(Error::RecompileRequiredError(
-                                RecompileReason::CapturedListener(
-                                    expr.to_token_stream().to_string(),
-                                ),
-                            ));
+                            false
                         }
+                    }) {
+                        let (_, listener) = ctx.listeners.remove(idx);
+                        listeners.push(listener)
+                    } else {
+                        return Err(Error::RecompileRequiredError(
+                            RecompileReason::CapturedListener(expr.to_token_stream().to_string()),
+                        ));
                     }
-                    _ => (),
                 }
             }
             let tag = bump.alloc(el.name.to_string());

+ 4 - 4
packages/rsx_interpreter/tests/render.rs

@@ -1,12 +1,12 @@
-use std::cell::{Cell, RefCell};
 
-use bumpalo::boxed::Box;
-use dioxus_core::{prelude::*, AnyEvent};
+
+
+use dioxus_core::{prelude::*};
 use dioxus_core_macro::*;
 use dioxus_html as dioxus_elements;
 use dioxus_rsx_interpreter::{
     captuered_context::{CapturedContext, FormattedArg, IfmtArgs},
-    CodeLocation, ErrorHandler,
+    CodeLocation,
 };
 
 #[test]