Skip to content

Commit

Permalink
Do not automatically inject bounds (#879)
Browse files Browse the repository at this point in the history
Fixes #878
  • Loading branch information
maurer authored Mar 27, 2023
1 parent 7e45b74 commit 74770c8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
16 changes: 3 additions & 13 deletions zeroize/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote};
use syn::{
parse::{Parse, ParseStream},
parse_quote,
punctuated::Punctuated,
token::Comma,
Attribute, Data, DeriveInput, Expr, ExprLit, Field, Fields, GenericParam, Lit, Meta, Result,
Variant, WherePredicate,
Attribute, Data, DeriveInput, Expr, ExprLit, Field, Fields, Lit, Meta, Result, Variant,
WherePredicate,
};

/// Name of zeroize-related attributes
Expand All @@ -39,16 +38,7 @@ fn derive_zeroize_impl(input: DeriveInput) -> TokenStream {

let extra_bounds = match attributes.bound {
Some(bounds) => bounds.0,
None => {
let mut out: Punctuated<WherePredicate, Comma> = Default::default();
for param in &input.generics.params {
if let GenericParam::Type(type_param) = param {
let type_name = &type_param.ident;
out.push(parse_quote! { #type_name: Zeroize })
}
}
out
}
None => Default::default(),
};

let mut generics = input.generics.clone();
Expand Down
26 changes: 26 additions & 0 deletions zeroize/tests/zeroize_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,29 @@ fn derive_zeroize_on_drop_generic() {
#[derive(ZeroizeOnDrop)]
struct Z<T: Zeroize>(Vec<T>);
}

#[test]
fn derive_zeroize_unused_param() {
#[derive(Zeroize)]
struct Z<T> {
arr: [u32; 5],
#[zeroize(skip)]
skipped: T,
}
}

#[test]
// Issue #878
fn derive_zeroize_with_marker() {
#[derive(ZeroizeOnDrop, Zeroize)]
struct Test<A: Marker> {
#[zeroize(skip)]
field: Option<A>,
}

trait Secret: ZeroizeOnDrop + Zeroize {}

impl<A: Marker> Secret for Test<A> {}

trait Marker {}
}

0 comments on commit 74770c8

Please sign in to comment.