Skip to content

Commit

Permalink
fix(parser): disallow from attributes with args
Browse files Browse the repository at this point in the history
fixes #16
  • Loading branch information
onkoe committed Aug 21, 2024
1 parent 4a7a171 commit 656a8b1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
15 changes: 15 additions & 0 deletions macros/src/parser/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/_doctests.rs
Original file line number Diff line number Diff line change
@@ -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() {}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 656a8b1

Please sign in to comment.