Skip to content

Commit

Permalink
Merge pull request #170 from wackbyte/deny-attrs-on-variants
Browse files Browse the repository at this point in the history
Deny derive attributes on enum variants
  • Loading branch information
fitzgen authored Jan 16, 2024
2 parents 87cef15 + 567511d commit dc92ff8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
16 changes: 16 additions & 0 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ fn gen_arbitrary_method(
.iter()
.enumerate()
.map(|(i, variant)| {
check_variant_attrs(variant)?;
let idx = i as u64;
let variant_name = &variant.ident;
construct(&variant.fields, |_, field| gen_constructor_for_field(field))
Expand Down Expand Up @@ -401,3 +402,18 @@ fn gen_constructor_for_field(field: &Field) -> Result<TokenStream> {
};
Ok(ctor)
}

fn check_variant_attrs(variant: &Variant) -> Result<()> {
for attr in &variant.attrs {
if attr.path().is_ident(ARBITRARY_ATTRIBUTE_NAME) {
return Err(Error::new_spanned(
attr,
format!(
"invalid `{}` attribute. it is unsupported on enum variants. try applying it to a field of the variant instead",
ARBITRARY_ATTRIBUTE_NAME
),
));
}
}
Ok(())
}
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1666,5 +1666,14 @@ mod test {
/// x: T,
/// }
/// ```
///
/// Attempt to use the derive attribute on an enum variant:
/// ```compile_fail
/// #[derive(::arbitrary::Arbitrary)]
/// enum Enum<T: Default> {
/// #[arbitrary(default)]
/// Variant(T),
/// }
/// ```
#[cfg(all(doctest, feature = "derive"))]
pub struct CompileFailTests;

0 comments on commit dc92ff8

Please sign in to comment.