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 <oorbecktim@Tims-MacBook-Pro.local>
This commit is contained in:
Tim Overbeek 2025-04-02 22:09:04 +02:00 committed by GitHub
parent 9e240ee99a
commit e02c3662fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -568,6 +568,7 @@ impl Parse for Require {
let mut path = input.parse::<Path>()?;
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::<Token![=]>()?;
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::<TokenStream2>()?;
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::<TokenStream2>()?;
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 })
}