mirror of
https://github.com/dtolnay/thiserror.git
synced 2025-04-06 22:37:38 +03:00
Reject source attribute not on a field
This commit is contained in:
parent
7c9dcaebbc
commit
10d1f640da
3 changed files with 26 additions and 0 deletions
|
@ -1,4 +1,5 @@
|
||||||
use crate::ast::{Enum, Field, Input, Struct, Variant};
|
use crate::ast::{Enum, Field, Input, Struct, Variant};
|
||||||
|
use crate::attr::Attrs;
|
||||||
use syn::{Error, Result};
|
use syn::{Error, Result};
|
||||||
|
|
||||||
pub(crate) const CHECKED: &str = "checked in validation";
|
pub(crate) const CHECKED: &str = "checked in validation";
|
||||||
|
@ -14,6 +15,7 @@ impl Input<'_> {
|
||||||
|
|
||||||
impl Struct<'_> {
|
impl Struct<'_> {
|
||||||
fn validate(&self) -> Result<()> {
|
fn validate(&self) -> Result<()> {
|
||||||
|
check_no_source(&self.attrs)?;
|
||||||
find_duplicate_source(&self.fields)?;
|
find_duplicate_source(&self.fields)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -21,6 +23,7 @@ impl Struct<'_> {
|
||||||
|
|
||||||
impl Enum<'_> {
|
impl Enum<'_> {
|
||||||
fn validate(&self) -> Result<()> {
|
fn validate(&self) -> Result<()> {
|
||||||
|
check_no_source(&self.attrs)?;
|
||||||
let has_display = self.has_display();
|
let has_display = self.has_display();
|
||||||
for variant in &self.variants {
|
for variant in &self.variants {
|
||||||
variant.validate()?;
|
variant.validate()?;
|
||||||
|
@ -37,11 +40,22 @@ impl Enum<'_> {
|
||||||
|
|
||||||
impl Variant<'_> {
|
impl Variant<'_> {
|
||||||
fn validate(&self) -> Result<()> {
|
fn validate(&self) -> Result<()> {
|
||||||
|
check_no_source(&self.attrs)?;
|
||||||
find_duplicate_source(&self.fields)?;
|
find_duplicate_source(&self.fields)?;
|
||||||
Ok(())
|
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<()> {
|
fn find_duplicate_source(fields: &[Field]) -> Result<()> {
|
||||||
let mut has_source = false;
|
let mut has_source = false;
|
||||||
for field in fields {
|
for field in fields {
|
||||||
|
|
7
tests/ui/unexpected-struct-source.rs
Normal file
7
tests/ui/unexpected-struct-source.rs
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
#[source]
|
||||||
|
pub struct Error;
|
||||||
|
|
||||||
|
fn main() {}
|
5
tests/ui/unexpected-struct-source.stderr
Normal file
5
tests/ui/unexpected-struct-source.stderr
Normal 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]
|
||||||
|
| ^^^^^^^^^
|
Loading…
Add table
Add a link
Reference in a new issue