Validate use of serde(transparent)

This commit is contained in:
David Tolnay
2018-05-20 13:35:55 -07:00
parent a64aaeeb3b
commit 0ea9d73fdf
+26
View File
@@ -20,6 +20,7 @@ pub fn check(cx: &Ctxt, cont: &Container) {
check_variant_skip_attrs(cx, cont);
check_internal_tag_field_name_conflict(cx, cont);
check_adjacent_tag_conflict(cx, cont);
check_transparent(cx, cont);
}
/// Getters are only allowed inside structs (not enums) with the `remote`
@@ -278,6 +279,31 @@ fn check_adjacent_tag_conflict(cx: &Ctxt, cont: &Container) {
}
}
/// Enums and unit structs cannot be transparent.
fn check_transparent(cx: &Ctxt, cont: &Container) {
if !cont.attrs.transparent() {
return;
}
if cont.attrs.type_from().is_some() {
cx.error("#[serde(transparent)] is not allowed with #[serde(from = \"...\")]");
}
if cont.attrs.type_into().is_some() {
cx.error("#[serde(transparent)] is not allowed with #[serde(into = \"...\")]");
}
match cont.data {
Data::Enum(_) => {
cx.error("#[serde(transparent)] is not allowed on an enum");
}
Data::Struct(Style::Unit, _) => {
cx.error("#[serde(transparent)] is not allowed on a unit struct");
}
_ => {}
}
}
fn member_message(member: &Member) -> String {
match *member {
Member::Named(ref ident) => format!("`{}`", ident),