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:
parent
9e240ee99a
commit
e02c3662fb
@ -568,6 +568,7 @@ impl Parse for Require {
|
|||||||
let mut path = input.parse::<Path>()?;
|
let mut path = input.parse::<Path>()?;
|
||||||
let mut last_segment_is_lower = false;
|
let mut last_segment_is_lower = false;
|
||||||
let mut is_constructor_call = false;
|
let mut is_constructor_call = false;
|
||||||
|
|
||||||
// Use the case of the type name to check if it's an enum
|
// 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
|
// This doesn't match everything that can be an enum according to the rust spec
|
||||||
// but it matches what clippy is OK with
|
// but it matches what clippy is OK with
|
||||||
@ -595,40 +596,32 @@ impl Parse for Require {
|
|||||||
|
|
||||||
let func = if input.peek(Token![=]) {
|
let func = if input.peek(Token![=]) {
|
||||||
// If there is an '=', then this is a "function style" require
|
// 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 expr: Expr = input.parse()?;
|
||||||
let tokens: TokenStream = quote::quote! (|| #expr).into();
|
Some(quote!(|| #expr ))
|
||||||
Some(TokenStream2::from(tokens))
|
|
||||||
} else if input.peek(Brace) {
|
} else if input.peek(Brace) {
|
||||||
// This is a "value style" named-struct-like require
|
// This is a "value style" named-struct-like require
|
||||||
let content;
|
let content;
|
||||||
braced!(content in input);
|
braced!(content in input);
|
||||||
let content = content.parse::<TokenStream2>()?;
|
let content = content.parse::<TokenStream2>()?;
|
||||||
let tokens: TokenStream = quote::quote! (|| #path { #content }).into();
|
Some(quote!(|| #path { #content }))
|
||||||
Some(TokenStream2::from(tokens))
|
|
||||||
} else if input.peek(Paren) {
|
} else if input.peek(Paren) {
|
||||||
// This is a "value style" tuple-struct-like require
|
// This is a "value style" tuple-struct-like require
|
||||||
let content;
|
let content;
|
||||||
parenthesized!(content in input);
|
parenthesized!(content in input);
|
||||||
let content = content.parse::<TokenStream2>()?;
|
let content = content.parse::<TokenStream2>()?;
|
||||||
is_constructor_call = last_segment_is_lower;
|
is_constructor_call = last_segment_is_lower;
|
||||||
let tokens: TokenStream = quote::quote! (|| #path (#content)).into();
|
Some(quote!(|| #path (#content)))
|
||||||
Some(TokenStream2::from(tokens))
|
|
||||||
} else if is_enum {
|
} else if is_enum {
|
||||||
// if this is an enum, then it is an inline enum component declaration
|
// if this is an enum, then it is an inline enum component declaration
|
||||||
let tokens: TokenStream = quote::quote! (|| #path).into();
|
Some(quote!(|| #path))
|
||||||
Some(TokenStream2::from(tokens))
|
|
||||||
} else {
|
} else {
|
||||||
// if this isn't any of the above, then it is a component ident, which will use Default
|
// if this isn't any of the above, then it is a component ident, which will use Default
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
if is_enum || is_constructor_call {
|
if is_enum || is_constructor_call {
|
||||||
let path_len = path.segments.len();
|
path.segments.pop();
|
||||||
path = Path {
|
path.segments.pop_punct();
|
||||||
leading_colon: path.leading_colon,
|
|
||||||
segments: Punctuated::from_iter(path.segments.into_iter().take(path_len - 1)),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
Ok(Require { path, func })
|
Ok(Require { path, func })
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user