From 192d60412266d3a8307f18c64774130b2b9ef86a Mon Sep 17 00:00:00 2001 From: toyboot4e Date: Tue, 27 Sep 2022 21:48:44 +0900 Subject: [PATCH 1/2] remove `?` operators from proc-macro generated code --- fyrox-core-derive/src/reflect.rs | 6 +++++- fyrox-core-derive/src/visit.rs | 30 +++++++++++++++++--------- fyrox-core-derive/src/visit/utils.rs | 32 ++++++++++++++++------------ 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/fyrox-core-derive/src/reflect.rs b/fyrox-core-derive/src/reflect.rs index c0fc69341..5da31dac5 100644 --- a/fyrox-core-derive/src/reflect.rs +++ b/fyrox-core-derive/src/reflect.rs @@ -300,7 +300,11 @@ fn gen_impl( } fn set(&mut self, value: Box) -> Result, Box> { - let this = std::mem::replace(self, value.take()?); + let value = match value.take() { + Ok(x) => x, + Err(err) => return Err(err), + }; + let this = std::mem::replace(self, value); Ok(Box::new(this)) } diff --git a/fyrox-core-derive/src/visit.rs b/fyrox-core-derive/src/visit.rs index 765d68bc9..4221ac392 100644 --- a/fyrox-core-derive/src/visit.rs +++ b/fyrox-core-derive/src/visit.rs @@ -23,13 +23,16 @@ fn impl_visit_struct( let visit_fn_body = if field_args.style == ast::Style::Unit { quote! { Ok(()) } } else { - // `field.visit(..);` parts + // `field.visit(..)?;` parts let field_visits = - utils::create_field_visits(None, field_args.fields.iter(), field_args.style); + utils::create_field_visits(true, field_args.fields.iter(), field_args.style); quote! { - let mut region = visitor.enter_region(name)?; - #(self.#field_visits)* + let mut region = match visitor.enter_region(name) { + Ok(x) => x, + Err(err) => return Err(err), + }; + #(#field_visits)* Ok(()) } }; @@ -152,7 +155,7 @@ fn impl_visit_enum(ty_args: &args::TypeArgs, variant_args: &[args::VariantArgs]) match style { ast::Style::Struct => { - let field_visits = utils::create_field_visits(None, fields.iter(), style); + let field_visits = utils::create_field_visits(false, fields.iter(), style); let idents = fields.iter().map(|field| { let ident = &field.ident; @@ -166,8 +169,7 @@ fn impl_visit_enum(ty_args: &args::TypeArgs, variant_args: &[args::VariantArgs]) } } ast::Style::Tuple => { - let field_visits = - utils::create_field_visits(parse_quote!(f), fields.iter(), style); + let field_visits = utils::create_field_visits(false, fields.iter(), style); let idents = (0..fields.len()).map(|i| format_ident!("f{}", Index::from(i))); @@ -187,13 +189,21 @@ fn impl_visit_enum(ty_args: &args::TypeArgs, variant_args: &[args::VariantArgs]) ty_args, variant_args.iter().flat_map(|v| v.fields.iter()).cloned(), quote! { - let mut region = visitor.enter_region(name)?; + let mut region = match visitor.enter_region(name) { + Ok(x) => x, + Err(err) => return Err(err), + }; let mut id = id(self); - id.visit("Id", &mut region)?; + if let Err(err) = id.visit("Id", &mut region) { + return Err(err); + }; if region.is_reading() { - *self = from_id(id)?; + *self = match from_id(id) { + Ok(x) => x, + Err(s) => return Err(s.into()), + }; } match self { diff --git a/fyrox-core-derive/src/visit/utils.rs b/fyrox-core-derive/src/visit/utils.rs index 5c70bdc6d..b3f2b25b5 100644 --- a/fyrox-core-derive/src/visit/utils.rs +++ b/fyrox-core-derive/src/visit/utils.rs @@ -47,15 +47,15 @@ fn create_impl_generics( generics } -/// `field.visit("name", visitor);` +/// `field.visit("name", visitor)?;` pub fn create_field_visits<'a>( - // None or `f` when bindings tuple variants. NOTE: We can't use `prefix: Ident` - prefix: Option, + // false if enum variant + is_struct: bool, fields: impl Iterator, field_style: ast::Style, ) -> Vec { if field_style == ast::Style::Unit { - // `Unit` (struct/enum variant) has no field to visit. + // `Unit` struct/enum variant has no field to visit. // We won't even enter this region: return vec![]; } @@ -76,14 +76,14 @@ pub fn create_field_visits<'a>( } // `Tuple(f32, ..)` ast::Style::Tuple => { - let ident = Index::from(field_index); - - let ident = match prefix { - Some(ref prefix) => { - let ident = format_ident!("{}{}", prefix, ident); - quote!(#ident) - } - None => quote!(#ident), + let index = Index::from(field_index); + let ident = if is_struct { + // accessed with `self.` + quote!(#index) + } else { + // named as `f` + let ident = format_ident!("f{}", index); + quote!(#ident) }; (ident, format!("{}", field_index)) @@ -114,16 +114,20 @@ pub fn create_field_visits<'a>( } } + let prefix = if is_struct { Some(quote!(self.)) } else { None }; + visit_args .iter() .map(|(ident, name, optional)| { if *optional { quote! { - #ident.visit(#name, &mut region).ok(); + #prefix #ident.visit(#name, &mut region).ok(); } } else { quote! { - #ident.visit(#name, &mut region)?; + if let Err(err) = #prefix #ident.visit(#name, &mut region) { + return Err(err); + } } } }) From ab2a44af580a98e0ab12915d8f2a0aedfc38086c Mon Sep 17 00:00:00 2001 From: toyboot4e Date: Tue, 27 Sep 2022 22:27:06 +0900 Subject: [PATCH 2/2] allow `clippy-question` --- fyrox-core-derive/src/visit/utils.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/fyrox-core-derive/src/visit/utils.rs b/fyrox-core-derive/src/visit/utils.rs index b3f2b25b5..995c96422 100644 --- a/fyrox-core-derive/src/visit/utils.rs +++ b/fyrox-core-derive/src/visit/utils.rs @@ -18,6 +18,7 @@ pub fn create_impl( let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); quote! { + #[allow(clippy::question_mark)] impl #impl_generics Visit for #ty_ident #ty_generics #where_clause { fn visit( &mut self,