Skip to content

Commit

Permalink
Add test for failing macros on enum variants
Browse files Browse the repository at this point in the history
It would have been security risk similar to #1 if that was accepted,
but it is not.

closes #5

Signed-off-by: Ahmed Abdelraoof <ahmed.abdelraoof@huawei.com>
  • Loading branch information
oddcoder committed Aug 19, 2024
1 parent 80111df commit f5090b5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions safe-discriminant-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,12 @@ pub fn repr(_: TokenStream, item: TokenStream) -> TokenStream {
pub fn do_nothing(_: TokenStream, item: TokenStream) -> TokenStream {
item
}

#[cfg(feature = "test-utils")]
/// This macro will remove the discriminant of any enum field it is applied to
#[proc_macro_attribute]
pub fn remove_disc(_: TokenStream, item: TokenStream) -> TokenStream {
let mut variant = parse_macro_input!(item as Variant);
variant.discriminant = None;
quote!(#variant).into()
}
11 changes: 11 additions & 0 deletions safe-discriminant/tests/fail/remove_disc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use safe_discriminant::Discriminant;
use safe_discriminant_derive::remove_disc;
#[derive(Discriminant)]
#[repr(u8)]
pub enum Foo {
#[remove_disc]
A = 0,
B = 1,
}

fn main() {}
5 changes: 5 additions & 0 deletions safe-discriminant/tests/fail/remove_disc.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: expected non-macro attribute, found attribute macro `remove_disc`
--> tests/fail/remove_disc.rs:6:7
|
6 | #[remove_disc]
| ^^^^^^^^^^^ not a non-macro attribute

0 comments on commit f5090b5

Please sign in to comment.