diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index 73c15b02..a2dbebf3 100644 --- a/serde_derive/src/de.rs +++ b/serde_derive/src/de.rs @@ -191,13 +191,17 @@ fn build_generics(cont: &Container, borrowed: &BorrowedLifetimes) -> syn::Generi } } -// Fields with a `skip_deserializing` or `deserialize_with` attribute are not -// deserialized by us so we do not generate a bound. Fields with a `bound` -// attribute specify their own bound so we do not generate one. All other fields -// may need a `T: Deserialize` bound where T is the type of the field. +// Fields with a `skip_deserializing` or `deserialize_with` attribute, or which +// belong to a variant with a `skip_deserializing` or `deserialize_with` +// attribute, are not deserialized by us so we do not generate a bound. Fields +// with a `bound` attribute specify their own bound so we do not generate one. +// All other fields may need a `T: Deserialize` bound where T is the type of the +// field. fn needs_deserialize_bound(field: &attr::Field, variant: Option<&attr::Variant>) -> bool { !field.skip_deserializing() && field.deserialize_with().is_none() && field.de_bound().is_none() - && variant.map_or(true, |variant| variant.deserialize_with().is_none()) + && variant.map_or(true, |variant| { + !variant.skip_deserializing() && variant.deserialize_with().is_none() + }) } // Fields with a `default` attribute (not `default=...`), and fields with a diff --git a/serde_derive/src/ser.rs b/serde_derive/src/ser.rs index c7c9a6f5..465491b3 100644 --- a/serde_derive/src/ser.rs +++ b/serde_derive/src/ser.rs @@ -150,13 +150,15 @@ fn build_generics(cont: &Container) -> syn::Generics { } // Fields with a `skip_serializing` or `serialize_with` attribute, or which -// belong to a variant with a `serialize_with` attribute, are not serialized by -// us so we do not generate a bound. Fields with a `bound` attribute specify -// their own bound so we do not generate one. All other fields may need a `T: -// Serialize` bound where T is the type of the field. +// belong to a variant with a 'skip_serializing` or `serialize_with` attribute, +// are not serialized by us so we do not generate a bound. Fields with a `bound` +// attribute specify their own bound so we do not generate one. All other fields +// may need a `T: Serialize` bound where T is the type of the field. fn needs_serialize_bound(field: &attr::Field, variant: Option<&attr::Variant>) -> bool { !field.skip_serializing() && field.serialize_with().is_none() && field.ser_bound().is_none() - && variant.map_or(true, |variant| variant.serialize_with().is_none()) + && variant.map_or(true, |variant| { + !variant.skip_serializing() && variant.serialize_with().is_none() + }) } fn serialize_body(cont: &Container, params: &Parameters) -> Fragment { diff --git a/test_suite/tests/test_gen.rs b/test_suite/tests/test_gen.rs index 371cc231..3b9d2a5e 100644 --- a/test_suite/tests/test_gen.rs +++ b/test_suite/tests/test_gen.rs @@ -592,6 +592,16 @@ fn test_gen() { #[derive(Deserialize)] #[serde(tag = "t", content = "c")] enum AdjacentlyTaggedVoid {} + + #[derive(Serialize, Deserialize)] + enum SkippedVariant { + #[serde(skip)] + #[allow(dead_code)] + T(T), + Unit, + } + + assert::>(); } //////////////////////////////////////////////////////////////////////////