Reject source attribute not on a field

This commit is contained in:
David Tolnay 2019-10-12 13:35:25 -07:00
parent 7c9dcaebbc
commit 10d1f640da
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
3 changed files with 26 additions and 0 deletions

View File

@ -1,4 +1,5 @@
use crate::ast::{Enum, Field, Input, Struct, Variant};
use crate::attr::Attrs;
use syn::{Error, Result};
pub(crate) const CHECKED: &str = "checked in validation";
@ -14,6 +15,7 @@ impl Input<'_> {
impl Struct<'_> {
fn validate(&self) -> Result<()> {
check_no_source(&self.attrs)?;
find_duplicate_source(&self.fields)?;
Ok(())
}
@ -21,6 +23,7 @@ impl Struct<'_> {
impl Enum<'_> {
fn validate(&self) -> Result<()> {
check_no_source(&self.attrs)?;
let has_display = self.has_display();
for variant in &self.variants {
variant.validate()?;
@ -37,11 +40,22 @@ impl Enum<'_> {
impl Variant<'_> {
fn validate(&self) -> Result<()> {
check_no_source(&self.attrs)?;
find_duplicate_source(&self.fields)?;
Ok(())
}
}
fn check_no_source(attrs: &Attrs) -> Result<()> {
if let Some(source) = &attrs.source {
return Err(Error::new_spanned(
source.original,
"not expected here; the #[source] attribute belongs on a specific field",
));
}
Ok(())
}
fn find_duplicate_source(fields: &[Field]) -> Result<()> {
let mut has_source = false;
for field in fields {

View File

@ -0,0 +1,7 @@
use thiserror::Error;
#[derive(Error, Debug)]
#[source]
pub struct Error;
fn main() {}

View File

@ -0,0 +1,5 @@
error: not expected here; the #[source] attribute belongs on a specific field
--> $DIR/unexpected-struct-source.rs:4:1
|
4 | #[source]
| ^^^^^^^^^