From 2fff019b6b965a97cd92bc7551f00c2a3baaab87 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Wed, 11 Aug 2021 20:33:13 +0200 Subject: [PATCH] improve codegen --- .../replace_derive_with_manual_impl.rs | 22 ++++++------------- .../src/utils/gen_trait_fn_body.rs | 20 +++++------------ 2 files changed, 12 insertions(+), 30 deletions(-) diff --git a/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs b/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs index 30d291bff9b..d29a312eba5 100644 --- a/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs +++ b/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs @@ -726,13 +726,9 @@ enum Foo { impl PartialEq for Foo { $0fn eq(&self, other: &Self) -> bool { - if core::mem::discriminant(self) == core::mem::discriminant(other) { - match (self, other) { - (Self::Bar(l0), Self::Bar(r0)) => l0 == r0, - _ => true, - } - } else { - false + match (self, other) { + (Self::Bar(l0), Self::Bar(r0)) => l0 == r0, + _ => core::mem::discriminant(self) == core::mem::discriminant(other), } } } @@ -774,14 +770,10 @@ enum Foo { impl PartialEq for Foo { $0fn eq(&self, other: &Self) -> bool { - if core::mem::discriminant(self) == core::mem::discriminant(other) { - match (self, other) { - (Self::Bar { bin: l_bin }, Self::Bar { bin: r_bin }) => l_bin == r_bin, - (Self::Baz { qux: l_qux, fez: l_fez }, Self::Baz { qux: r_qux, fez: r_fez }) => l_qux == r_qux && l_fez == r_fez, - _ => true, - } - } else { - false + match (self, other) { + (Self::Bar { bin: l_bin }, Self::Bar { bin: r_bin }) => l_bin == r_bin, + (Self::Baz { qux: l_qux, fez: l_fez }, Self::Baz { qux: r_qux, fez: r_fez }) => l_qux == r_qux && l_fez == r_fez, + _ => core::mem::discriminant(self) == core::mem::discriminant(other), } } } diff --git a/crates/ide_assists/src/utils/gen_trait_fn_body.rs b/crates/ide_assists/src/utils/gen_trait_fn_body.rs index 701e763bcea..65d39c370ae 100644 --- a/crates/ide_assists/src/utils/gen_trait_fn_body.rs +++ b/crates/ide_assists/src/utils/gen_trait_fn_body.rs @@ -449,27 +449,17 @@ fn gen_tuple_field(field_name: &String) -> ast::Pat { } } - if !arms.is_empty() && case_count > arms.len() { - let lhs = make::wildcard_pat().into(); - arms.push(make::match_arm(Some(lhs), None, make::expr_literal("true").into())); - } - let expr = match arms.len() { 0 => eq_check, _ => { - let condition = make::condition(eq_check, None); + if case_count > arms.len() { + let lhs = make::wildcard_pat().into(); + arms.push(make::match_arm(Some(lhs), None, eq_check)); + } let match_target = make::expr_tuple(vec![self_name, other_name]); let list = make::match_arm_list(arms).indent(ast::edit::IndentLevel(1)); - let match_expr = Some(make::expr_match(match_target, list)); - let then_branch = make::block_expr(None, match_expr); - let then_branch = then_branch.indent(ast::edit::IndentLevel(1)); - - let else_branche = make::expr_literal("false"); - let else_branche = make::block_expr(None, Some(else_branche.into())) - .indent(ast::edit::IndentLevel(1)); - - make::expr_if(condition, then_branch, Some(else_branche.into())) + make::expr_match(match_target, list) } }; -- 2.44.0