From e02c3662fb6b2c801118f21e3caa89fe5371a4a6 Mon Sep 17 00:00:00 2001 From: Tim Overbeek <158390905+Bleachfuel@users.noreply.github.com> Date: Wed, 2 Apr 2025 22:09:04 +0200 Subject: [PATCH] Code quality cleanup pass for #[require] (#18621) #18555 improved syntax for required components. However some code was a bit redundant after the new parsing and struct initializing would not give proper errors. This PR fixes that. --------- Co-authored-by: Tim Overbeek --- crates/bevy_ecs/macros/src/component.rs | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/crates/bevy_ecs/macros/src/component.rs b/crates/bevy_ecs/macros/src/component.rs index ae6f815aff..d3199c0909 100644 --- a/crates/bevy_ecs/macros/src/component.rs +++ b/crates/bevy_ecs/macros/src/component.rs @@ -568,6 +568,7 @@ impl Parse for Require { let mut path = input.parse::()?; let mut last_segment_is_lower = false; let mut is_constructor_call = false; + // Use the case of the type name to check if it's an enum // This doesn't match everything that can be an enum according to the rust spec // but it matches what clippy is OK with @@ -595,40 +596,32 @@ impl Parse for Require { let func = if input.peek(Token![=]) { // If there is an '=', then this is a "function style" require - let _t: syn::Token![=] = input.parse()?; + input.parse::()?; let expr: Expr = input.parse()?; - let tokens: TokenStream = quote::quote! (|| #expr).into(); - Some(TokenStream2::from(tokens)) + Some(quote!(|| #expr )) } else if input.peek(Brace) { // This is a "value style" named-struct-like require let content; braced!(content in input); let content = content.parse::()?; - let tokens: TokenStream = quote::quote! (|| #path { #content }).into(); - Some(TokenStream2::from(tokens)) + Some(quote!(|| #path { #content })) } else if input.peek(Paren) { // This is a "value style" tuple-struct-like require let content; parenthesized!(content in input); let content = content.parse::()?; is_constructor_call = last_segment_is_lower; - let tokens: TokenStream = quote::quote! (|| #path (#content)).into(); - Some(TokenStream2::from(tokens)) + Some(quote!(|| #path (#content))) } else if is_enum { // if this is an enum, then it is an inline enum component declaration - let tokens: TokenStream = quote::quote! (|| #path).into(); - Some(TokenStream2::from(tokens)) + Some(quote!(|| #path)) } else { // if this isn't any of the above, then it is a component ident, which will use Default None }; - if is_enum || is_constructor_call { - let path_len = path.segments.len(); - path = Path { - leading_colon: path.leading_colon, - segments: Punctuated::from_iter(path.segments.into_iter().take(path_len - 1)), - }; + path.segments.pop(); + path.segments.pop_punct(); } Ok(Require { path, func }) }