Skip to content

Commit dc92ff8

Browse files
authored
Merge pull request #170 from wackbyte/deny-attrs-on-variants
Deny derive attributes on enum variants
2 parents 87cef15 + 567511d commit dc92ff8

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

derive/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ fn gen_arbitrary_method(
216216
.iter()
217217
.enumerate()
218218
.map(|(i, variant)| {
219+
check_variant_attrs(variant)?;
219220
let idx = i as u64;
220221
let variant_name = &variant.ident;
221222
construct(&variant.fields, |_, field| gen_constructor_for_field(field))
@@ -401,3 +402,18 @@ fn gen_constructor_for_field(field: &Field) -> Result<TokenStream> {
401402
};
402403
Ok(ctor)
403404
}
405+
406+
fn check_variant_attrs(variant: &Variant) -> Result<()> {
407+
for attr in &variant.attrs {
408+
if attr.path().is_ident(ARBITRARY_ATTRIBUTE_NAME) {
409+
return Err(Error::new_spanned(
410+
attr,
411+
format!(
412+
"invalid `{}` attribute. it is unsupported on enum variants. try applying it to a field of the variant instead",
413+
ARBITRARY_ATTRIBUTE_NAME
414+
),
415+
));
416+
}
417+
}
418+
Ok(())
419+
}

src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,5 +1666,14 @@ mod test {
16661666
/// x: T,
16671667
/// }
16681668
/// ```
1669+
///
1670+
/// Attempt to use the derive attribute on an enum variant:
1671+
/// ```compile_fail
1672+
/// #[derive(::arbitrary::Arbitrary)]
1673+
/// enum Enum<T: Default> {
1674+
/// #[arbitrary(default)]
1675+
/// Variant(T),
1676+
/// }
1677+
/// ```
16691678
#[cfg(all(doctest, feature = "derive"))]
16701679
pub struct CompileFailTests;

0 commit comments

Comments
 (0)