Skip to content

Commit 81cebab

Browse files
committed
Disallow serde(bound ...)
1 parent c45b5d9 commit 81cebab

File tree

4 files changed

+46
-28
lines changed

4 files changed

+46
-28
lines changed

src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,12 @@ impl Error {
307307
syn::Error::new(serde, "Found unused `#[serde(...)]`")
308308
}
309309

310+
/// Conflicting `serde(bound ...)` when deriving `De/Serialize`.
311+
#[cfg(feature = "serde")]
312+
pub fn serde_bound(serde: Span) -> syn::Error {
313+
syn::Error::new(serde, "Found conflicting `#[serde(bound ...)]`")
314+
}
315+
310316
/// List of available [`Trait`](crate::Trait)s.
311317
fn trait_list() -> String {
312318
[

src/trait_/serde.rs

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,40 @@ pub fn parse_derive_trait(
3535
if let Ok(nested) =
3636
list.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)
3737
{
38-
if nested.len() != 1 {
39-
continue;
40-
}
41-
42-
let meta = &nested[0];
38+
for meta in nested {
39+
if meta.path().is_ident("bound") {
40+
return Err(Error::serde_bound(meta.span()));
41+
}
4342

44-
if !meta.path().is_ident("crate") {
45-
continue;
46-
}
43+
if !meta.path().is_ident("crate") {
44+
continue;
45+
}
4746

48-
match &meta {
49-
Meta::NameValue(name_value) => {
50-
// Check for duplicate `crate` option.
51-
if crate_.is_none() {
52-
let path = match &name_value.value {
53-
Expr::Lit(ExprLit {
54-
lit: Lit::Str(lit_str),
55-
..
56-
}) => match lit_str.parse::<Path>() {
57-
Ok(path) => path,
58-
Err(error) => return Err(Error::path(lit_str.span(), error)),
59-
},
60-
_ => return Err(Error::option_syntax(name_value.value.span())),
61-
};
47+
match &meta {
48+
Meta::NameValue(name_value) => {
49+
// Check for duplicate `crate` option.
50+
if crate_.is_none() {
51+
let path = match &name_value.value {
52+
Expr::Lit(ExprLit {
53+
lit: Lit::Str(lit_str),
54+
..
55+
}) => match lit_str.parse::<Path>() {
56+
Ok(path) => path,
57+
Err(error) => {
58+
return Err(Error::path(lit_str.span(), error))
59+
}
60+
},
61+
_ => return Err(Error::option_syntax(name_value.value.span())),
62+
};
6263

63-
crate_ = Some(path);
64-
} else {
65-
return Err(Error::option_duplicate(name_value.span(), "crate"));
64+
crate_ = Some(path);
65+
} else {
66+
return Err(Error::option_duplicate(name_value.span(), "crate"));
67+
}
68+
}
69+
_ => {
70+
return Err(Error::option_syntax(meta.span()));
6671
}
67-
}
68-
_ => {
69-
return Err(Error::option_syntax(meta.span()));
7072
}
7173
}
7274
}

tests/ui/serde/serde.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ use derive_where::derive_where;
66
#[serde(crate = "serde_")]
77
struct InvalidSerde<T>(PhantomData<T>);
88

9+
#[derive_where(Deserialize)]
10+
#[serde(bound = "")]
11+
struct ConflictingBound<T>(PhantomData<T>);
12+
913
fn main() {}

tests/ui/serde/serde.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ error: Found unused `#[serde(...)]`
33
|
44
6 | #[serde(crate = "serde_")]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: Found conflicting `#[serde(bound ...)]`
8+
--> tests/ui/serde/serde.rs:10:9
9+
|
10+
10 | #[serde(bound = "")]
11+
| ^^^^^^^^^^

0 commit comments

Comments
 (0)