Skip to content

Commit 2fff019

Browse files
committed
improve codegen
1 parent 6c4a94b commit 2fff019

File tree

2 files changed

+12
-30
lines changed

2 files changed

+12
-30
lines changed

crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -726,13 +726,9 @@ enum Foo {
726726
727727
impl PartialEq for Foo {
728728
$0fn eq(&self, other: &Self) -> bool {
729-
if core::mem::discriminant(self) == core::mem::discriminant(other) {
730-
match (self, other) {
731-
(Self::Bar(l0), Self::Bar(r0)) => l0 == r0,
732-
_ => true,
733-
}
734-
} else {
735-
false
729+
match (self, other) {
730+
(Self::Bar(l0), Self::Bar(r0)) => l0 == r0,
731+
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
736732
}
737733
}
738734
}
@@ -774,14 +770,10 @@ enum Foo {
774770
775771
impl PartialEq for Foo {
776772
$0fn eq(&self, other: &Self) -> bool {
777-
if core::mem::discriminant(self) == core::mem::discriminant(other) {
778-
match (self, other) {
779-
(Self::Bar { bin: l_bin }, Self::Bar { bin: r_bin }) => l_bin == r_bin,
780-
(Self::Baz { qux: l_qux, fez: l_fez }, Self::Baz { qux: r_qux, fez: r_fez }) => l_qux == r_qux && l_fez == r_fez,
781-
_ => true,
782-
}
783-
} else {
784-
false
773+
match (self, other) {
774+
(Self::Bar { bin: l_bin }, Self::Bar { bin: r_bin }) => l_bin == r_bin,
775+
(Self::Baz { qux: l_qux, fez: l_fez }, Self::Baz { qux: r_qux, fez: r_fez }) => l_qux == r_qux && l_fez == r_fez,
776+
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
785777
}
786778
}
787779
}

crates/ide_assists/src/utils/gen_trait_fn_body.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -449,27 +449,17 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
449449
}
450450
}
451451

452-
if !arms.is_empty() && case_count > arms.len() {
453-
let lhs = make::wildcard_pat().into();
454-
arms.push(make::match_arm(Some(lhs), None, make::expr_literal("true").into()));
455-
}
456-
457452
let expr = match arms.len() {
458453
0 => eq_check,
459454
_ => {
460-
let condition = make::condition(eq_check, None);
455+
if case_count > arms.len() {
456+
let lhs = make::wildcard_pat().into();
457+
arms.push(make::match_arm(Some(lhs), None, eq_check));
458+
}
461459

462460
let match_target = make::expr_tuple(vec![self_name, other_name]);
463461
let list = make::match_arm_list(arms).indent(ast::edit::IndentLevel(1));
464-
let match_expr = Some(make::expr_match(match_target, list));
465-
let then_branch = make::block_expr(None, match_expr);
466-
let then_branch = then_branch.indent(ast::edit::IndentLevel(1));
467-
468-
let else_branche = make::expr_literal("false");
469-
let else_branche = make::block_expr(None, Some(else_branche.into()))
470-
.indent(ast::edit::IndentLevel(1));
471-
472-
make::expr_if(condition, then_branch, Some(else_branche.into()))
462+
make::expr_match(match_target, list)
473463
}
474464
};
475465

0 commit comments

Comments
 (0)