From b78fe1891b1a68ac76d1044821aea0378595910f Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 12 Oct 2019 09:49:36 -0700 Subject: [PATCH] Detect duplicate attributes on the same syntax tree node --- impl/src/attr.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/impl/src/attr.rs b/impl/src/attr.rs index b74c834..32eac1a 100644 --- a/impl/src/attr.rs +++ b/impl/src/attr.rs @@ -3,7 +3,8 @@ use quote::{format_ident, quote, ToTokens}; use std::iter::once; use syn::parse::{Nothing, ParseStream}; use syn::{ - braced, bracketed, parenthesized, token, Attribute, Ident, Index, LitInt, LitStr, Result, Token, + braced, bracketed, parenthesized, token, Attribute, Error, Ident, Index, LitInt, LitStr, + Result, Token, }; pub struct Attrs { @@ -26,9 +27,18 @@ pub fn get(input: &[Attribute]) -> Result { for attr in input { if attr.path.is_ident("error") { let display = parse_display(attr)?; + if attrs.display.is_some() { + return Err(Error::new_spanned( + attr, + "only one #[error(...)] attribute is allowed", + )); + } attrs.display = Some(display); } else if attr.path.is_ident("source") { parse_source(attr)?; + if attrs.source { + return Err(Error::new_spanned(attr, "duplicate #[source] attribute")); + } attrs.source = true; } }