From 656a8b1c7d7010d1de0b5e6a081085951f9195ab Mon Sep 17 00:00:00 2001 From: Barrett Date: Wed, 21 Aug 2024 14:44:03 -0500 Subject: [PATCH] fix(parser): disallow `from` attributes with args fixes #16 --- macros/src/parser/field.rs | 15 +++++++++++++++ src/_doctests.rs | 13 +++++++++++++ src/lib.rs | 3 +++ 3 files changed, 31 insertions(+) create mode 100644 src/_doctests.rs diff --git a/macros/src/parser/field.rs b/macros/src/parser/field.rs index eb8fad6..cc41401 100644 --- a/macros/src/parser/field.rs +++ b/macros/src/parser/field.rs @@ -108,6 +108,14 @@ impl FromAttributeCheck { return Err(Self::err_too_many_from_attributes(field_span)); } + // check if the attr has some args + match attr.meta { + syn::Meta::List(_) | syn::Meta::NameValue(_) => { + return Err(Self::err_from_attribute_has_args(&attr.span())) + } + syn::Meta::Path(_) => (), // good. there are no args in `#[from]` + } + already_found_from_attribute = true; } } @@ -127,6 +135,13 @@ impl FromAttributeCheck { ) } + pub fn err_from_attribute_has_args(attribute_span: &Span) -> syn::Error { + syn::Error::new( + *attribute_span, + "The `#[from]` attribute does not take any arguments, but some were found.", + ) + } + /// Returns the created WrappedField, consuming `self`. pub fn finish(self) -> WrappedField { self.wrapped_field diff --git a/src/_doctests.rs b/src/_doctests.rs new file mode 100644 index 0000000..c3bb203 --- /dev/null +++ b/src/_doctests.rs @@ -0,0 +1,13 @@ +/** +```compile_fail +use super::Error; +use std::error::Error; + +#[derive(Debug, Error)] +enum FromAttrWithArgs { + #[error(transparent)] + ErrorVariant(#[from("this shouldn't work")] std::io::Error), +} +``` +*/ +pub fn from_attr_args_shouldnt_have_args() {} diff --git a/src/lib.rs b/src/lib.rs index d71de38..7175af5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,4 +45,7 @@ There are a few ground rules, though: The only note I have for now is to generate the README using `cargo-rdme`. The crate includes a configuration file for this tool, so it should just work. */ +#[cfg(doctest)] +pub mod _doctests; + pub use macros::Error;