From 85b0944eac901c7bf9922469439579964a066842 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 27 Apr 2020 15:48:44 -0700 Subject: [PATCH] Reject transparent attr in field attribute position --- impl/src/valid.rs | 6 ++++++ tests/ui/bad-field-attr.rs | 7 +++++++ tests/ui/bad-field-attr.stderr | 5 +++++ 3 files changed, 18 insertions(+) create mode 100644 tests/ui/bad-field-attr.rs create mode 100644 tests/ui/bad-field-attr.stderr diff --git a/impl/src/valid.rs b/impl/src/valid.rs index b5da90d..0d5ba36 100644 --- a/impl/src/valid.rs +++ b/impl/src/valid.rs @@ -163,6 +163,12 @@ fn check_field_attrs(fields: &[Field]) -> Result<()> { backtrace_field = Some(field); has_backtrace = true; } + if let Some(transparent) = field.attrs.transparent { + return Err(Error::new_spanned( + transparent, + "#[error(transparent)] needs to go outside the enum or struct, not on an individual field", + )); + } has_backtrace |= field.is_backtrace(); } if let (Some(from_field), Some(source_field)) = (from_field, source_field) { diff --git a/tests/ui/bad-field-attr.rs b/tests/ui/bad-field-attr.rs new file mode 100644 index 0000000..d5429b2 --- /dev/null +++ b/tests/ui/bad-field-attr.rs @@ -0,0 +1,7 @@ +use thiserror::Error; + +#[derive(Error, Debug)] +#[error(transparent)] +pub struct Error(#[error(transparent)] std::io::Error); + +fn main() {} diff --git a/tests/ui/bad-field-attr.stderr b/tests/ui/bad-field-attr.stderr new file mode 100644 index 0000000..ecc4702 --- /dev/null +++ b/tests/ui/bad-field-attr.stderr @@ -0,0 +1,5 @@ +error: #[error(transparent)] needs to go outside the enum or struct, not on an individual field + --> $DIR/bad-field-attr.rs:5:18 + | +5 | pub struct Error(#[error(transparent)] std::io::Error); + | ^^^^^^^^^^^^^^^^^^^^^