diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/valid.rs | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/src/valid.rs b/src/valid.rs index ab20423..cbd9f41 100644 --- a/src/valid.rs +++ b/src/valid.rs @@ -188,10 +188,10 @@ fn check_field_attrs(fields: &[Field]) -> Result<()> { } } if let Some(source_field) = source_field.or(from_field) { - if contains_non_static_lifetime(source_field) { + if contains_non_static_lifetime(&source_field.ty) { return Err(Error::new_spanned( - source_field.original, - "non-static lifetimes are not allowed in the source of an error", + &source_field.original.ty, + "non-static lifetimes are not allowed in the source of an error, because std::error::Error requires the source is dyn Error + 'static", )); } } @@ -206,21 +206,28 @@ fn same_member(one: &Field, two: &Field) -> bool { } } -fn contains_non_static_lifetime(field: &Field) -> bool { - let ty = match field.ty { - Type::Path(ty) => ty, - _ => return false, // maybe implement later if there are common other cases - }; - let bracketed = match &ty.path.segments.last().unwrap().arguments { - PathArguments::AngleBracketed(bracketed) => bracketed, - _ => return false, - }; - for arg in &bracketed.args { - if let GenericArgument::Lifetime(lifetime) = arg { - if lifetime.ident != "static" { - return true; +fn contains_non_static_lifetime(ty: &Type) -> bool { + match ty { + Type::Path(ty) => { + let bracketed = match &ty.path.segments.last().unwrap().arguments { + PathArguments::AngleBracketed(bracketed) => bracketed, + _ => return false, + }; + for arg in &bracketed.args { + match arg { + GenericArgument::Type(ty) if contains_non_static_lifetime(ty) => return true, + GenericArgument::Lifetime(lifetime) if lifetime.ident != "static" => { + return true + } + _ => {} + } } + false } + Type::Reference(ty) => ty + .lifetime + .as_ref() + .map_or(false, |lifetime| lifetime.ident != "static"), + _ => false, // maybe implement later if there are common other cases } - false } |