Skip to content

#[derive(TryInto)] without #[try_into(repr)] silently doesn't do anything #457

Closed
@akonradi-signal

Description

@akonradi-signal

The top-level implementation doesn't complain when there is no #[try_from] attribute at all:

attr: ItemAttribute::parse_attrs(&input.attrs, &format_ident!("try_from"))?
.map(|attr| {
if matches!(attr.item, ItemAttribute::Types(_)) {
Err(syn::Error::new(
attr.span,
"`#[try_from(repr(...))]` attribute is not supported yet",
))
} else {
Ok(attr.item)
}
})
.transpose()?,
ident: input.ident.clone(),
generics: input.generics.clone(),
variants: data.variants.clone().into_iter().collect(),
}
.into_token_stream()),
syn::Data::Union(data) => Err(syn::Error::new(
data.union_token.span(),
"`TryFrom` cannot be derived for unions",
)),
}
}
/// Representation of a [`TryFrom`] derive macro struct item attribute.
///
/// ```rust,ignore
/// #[try_from(repr)]

But then the actual ToTokens impl doesn't actually do anything when the attribute is not present:

if self.attr.is_none() {
return;
}

If the attribute is required, its absence should generate a compile error. If it's not required, then omitting it should not result in doing nothing.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions