From b74a780418efdfa76d6636f615abb38b85debc34 Mon Sep 17 00:00:00 2001 From: Thibeau Vercruyssen <74682871+tvercruyssen@users.noreply.github.com> Date: Wed, 5 Oct 2022 00:05:26 +0200 Subject: [PATCH] Avoid branch in `Display` `match` statement for empty enums (#196) This removes a match arm that will never be triggered for non empty enums. So now only if their are no arms in the enum will the code for empty enums be generated. This changes no functionally and is a bug fix. --- src/display.rs | 7 +++---- tests/display.rs | 7 +++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/display.rs b/src/display.rs index 4a045078..48ee24c8 100644 --- a/src/display.rs +++ b/src/display.rs @@ -68,9 +68,8 @@ pub fn expand(input: &syn::DeriveInput, trait_name: &str) -> Result fn fmt(&self, _derive_more_display_formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { #helper_struct - match self { + match *self { #arms - _ => Ok(()) // This is needed for empty enums } } } @@ -200,7 +199,7 @@ impl<'a, 'b> State<'a, 'b> { let fields: TokenStream = (0..fields.unnamed.len()) .map(|n| { let i = Ident::new(&format!("_{}", n), Span::call_site()); - quote!(#i,) + quote!(ref #i,) }) .collect(); quote!((#fields)) @@ -211,7 +210,7 @@ impl<'a, 'b> State<'a, 'b> { .iter() .map(|f| { let i = f.ident.as_ref().unwrap(); - quote!(#i,) + quote!(ref #i,) }) .collect(); quote!({#fields}) diff --git a/tests/display.rs b/tests/display.rs index e778afc8..e8a4cc93 100644 --- a/tests/display.rs +++ b/tests/display.rs @@ -160,6 +160,13 @@ fn check_display() { assert_eq!(DebugStructAsDisplay.to_string(), "DebugStructAsDisplay"); } +#[test] +fn empty_enum_impls_display() { + trait S: std::fmt::Display {} + + impl S for EmptyEnum {} +} + mod generic { #[derive(Display)] #[display(fmt = "Generic {}", field)]