Allow using bool for optional diagnostics

This commit is contained in:
clubby789
2023-02-27 12:54:11 +00:00
parent 49b9cc5139
commit ef434f04e9
4 changed files with 77 additions and 45 deletions

View File

@@ -50,6 +50,11 @@ pub(crate) fn type_is_unit(ty: &Type) -> bool {
if let Type::Tuple(TypeTuple { elems, .. }) = ty { elems.is_empty() } else { false }
}
/// Checks whether the type `ty` is `bool`.
pub(crate) fn type_is_bool(ty: &Type) -> bool {
type_matches_path(ty, &["bool"])
}
/// Reports a type error for field with `attr`.
pub(crate) fn report_type_error(
attr: &Attribute,
@@ -192,6 +197,11 @@ impl<'ty> FieldInnerTy<'ty> {
#inner
}
},
FieldInnerTy::Plain(t) if type_is_bool(t) => quote! {
if #binding {
#inner
}
},
FieldInnerTy::Plain(..) => quote! { #inner },
}
}