diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index 2e21d197cdf..498d5d69ba4 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -339,11 +339,19 @@ fn into_block(mut self, cx: &ExtCtxt<'_>, span: Span) -> P { // Converts it into an expression. fn into_expr(self, cx: &ExtCtxt<'_>, span: Span) -> P { if self.0.is_empty() { + // No statements. match self.1 { None => cx.expr_block(cx.block(span, vec![])), Some(expr) => expr, } + } else if self.0.len() == 1 + && let ast::StmtKind::Expr(expr) = &self.0[0].kind + && self.1.is_none() + { + // There's only a single statement expression. Pull it out. + expr.clone() } else { + // Multiple statements and/or expressions. cx.expr_block(self.into_block(cx, span)) } } diff --git a/src/test/ui/deriving/deriving-all-codegen.stdout b/src/test/ui/deriving/deriving-all-codegen.stdout index 0337ca2634f..0e222d131d7 100644 --- a/src/test/ui/deriving/deriving-all-codegen.stdout +++ b/src/test/ui/deriving/deriving-all-codegen.stdout @@ -697,9 +697,8 @@ impl ::core::fmt::Debug for Enum1 { impl ::core::hash::Hash for Enum1 { fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () { match self { - Enum1::Single { x: __self_0 } => { - ::core::hash::Hash::hash(__self_0, state) - } + Enum1::Single { x: __self_0 } => + ::core::hash::Hash::hash(__self_0, state), } } } @@ -870,10 +869,9 @@ impl ::core::default::Default for Fieldless { impl ::core::hash::Hash for Fieldless { fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () { match self { - _ => { + _ => ::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self), - state) - } + state), } } } @@ -992,10 +990,9 @@ impl ::core::hash::Hash for Mixed { ::core::hash::Hash::hash(__self_0, state); ::core::hash::Hash::hash(__self_1, state) } - _ => { + _ => ::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self), - state) - } + state), } } }