Skip to content

Commit f1d9e2b

Browse files
committed
Avoid some unnecessary blocks in derive output.
1 parent 56178d4 commit f1d9e2b

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,19 @@ impl BlockOrExpr {
339339
// Converts it into an expression.
340340
fn into_expr(self, cx: &ExtCtxt<'_>, span: Span) -> P<Expr> {
341341
if self.0.is_empty() {
342+
// No statements.
342343
match self.1 {
343344
None => cx.expr_block(cx.block(span, vec![])),
344345
Some(expr) => expr,
345346
}
347+
} else if self.0.len() == 1
348+
&& let ast::StmtKind::Expr(expr) = &self.0[0].kind
349+
&& self.1.is_none()
350+
{
351+
// There's only a single statement expression. Pull it out.
352+
expr.clone()
346353
} else {
354+
// Multiple statements and/or expressions.
347355
cx.expr_block(self.into_block(cx, span))
348356
}
349357
}

src/test/ui/deriving/deriving-all-codegen.stdout

+6-9
Original file line numberDiff line numberDiff line change
@@ -697,9 +697,8 @@ impl ::core::fmt::Debug for Enum1 {
697697
impl ::core::hash::Hash for Enum1 {
698698
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
699699
match self {
700-
Enum1::Single { x: __self_0 } => {
701-
::core::hash::Hash::hash(__self_0, state)
702-
}
700+
Enum1::Single { x: __self_0 } =>
701+
::core::hash::Hash::hash(__self_0, state),
703702
}
704703
}
705704
}
@@ -870,10 +869,9 @@ impl ::core::default::Default for Fieldless {
870869
impl ::core::hash::Hash for Fieldless {
871870
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
872871
match self {
873-
_ => {
872+
_ =>
874873
::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self),
875-
state)
876-
}
874+
state),
877875
}
878876
}
879877
}
@@ -992,10 +990,9 @@ impl ::core::hash::Hash for Mixed {
992990
::core::hash::Hash::hash(__self_0, state);
993991
::core::hash::Hash::hash(__self_1, state)
994992
}
995-
_ => {
993+
_ =>
996994
::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self),
997-
state)
998-
}
995+
state),
999996
}
1000997
}
1001998
}

0 commit comments

Comments
 (0)