From 9433004307ea5c8e0e4ab2ef9acc9b024f3315d6 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 9 May 2020 20:55:55 -0700 Subject: [PATCH] Omit missing content match if not needed --- serde_derive/src/de.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index a4e15b55..c9838924 100644 --- a/serde_derive/src/de.rs +++ b/serde_derive/src/de.rs @@ -1377,8 +1377,11 @@ fn deserialize_adjacently_tagged_enum( } }; + let mut missing_content = quote! { + _serde::export::Err(<__A::Error as _serde::de::Error>::missing_field(#content)) + }; let mut missing_content_fallthrough = quote!(); - let missing_content_arms = variants + let mut missing_content_arms = variants .iter() .enumerate() .filter(|&(_, variant)| !variant.attrs.skip_deserializing()) @@ -1398,22 +1401,23 @@ fn deserialize_adjacently_tagged_enum( } } _ => { - missing_content_fallthrough = quote! { - _ => _serde::export::Err(<__A::Error as _serde::de::Error>::missing_field(#content)) - }; + missing_content_fallthrough = quote!(_ => #missing_content); return None; } }; Some(quote! { __Field::#variant_index => #arm, }) - }); - let missing_content = quote! { - match __field { - #(#missing_content_arms)* - #missing_content_fallthrough - } - }; + }) + .peekable(); + if missing_content_arms.peek().is_some() { + missing_content = quote! { + match __field { + #(#missing_content_arms)* + #missing_content_fallthrough + } + }; + } // Advance the map by one key, returning early in case of error. let next_key = quote! {