Skip to content

Commit fed74c8

Browse files
committed
fix: add fallback case in generated PartialEq impl
1 parent df07c8f commit fed74c8

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ impl PartialEq for Foo {
907907
}
908908

909909
#[test]
910-
fn add_custom_impl_partial_eq_tuple_enum() {
910+
fn add_custom_impl_partial_eq_partial_tuple_enum() {
911911
check_assist(
912912
replace_derive_with_manual_impl,
913913
r#"
@@ -936,6 +936,37 @@ impl PartialEq for Foo {
936936
)
937937
}
938938

939+
#[test]
940+
fn add_custom_impl_partial_eq_tuple_enum() {
941+
check_assist(
942+
replace_derive_with_manual_impl,
943+
r#"
944+
//- minicore: eq, derive
945+
#[derive(Partial$0Eq)]
946+
enum Foo {
947+
Bar(String),
948+
Baz(i32),
949+
}
950+
"#,
951+
r#"
952+
enum Foo {
953+
Bar(String),
954+
Baz(i32),
955+
}
956+
957+
impl PartialEq for Foo {
958+
$0fn eq(&self, other: &Self) -> bool {
959+
match (self, other) {
960+
(Self::Bar(l0), Self::Bar(r0)) => l0 == r0,
961+
(Self::Baz(l0), Self::Baz(r0)) => l0 == r0,
962+
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
963+
}
964+
}
965+
}
966+
"#,
967+
)
968+
}
969+
939970
#[test]
940971
fn add_custom_impl_partial_eq_record_enum() {
941972
check_assist(

crates/ide-assists/src/utils/gen_trait_fn_body.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,8 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
439439
let eq_check =
440440
make::expr_bin_op(lhs, BinaryOp::CmpOp(CmpOp::Eq { negated: false }), rhs);
441441

442-
let mut n_cases = 0;
443442
let mut arms = vec![];
444443
for variant in enum_.variant_list()?.variants() {
445-
n_cases += 1;
446444
match variant.field_list() {
447445
// => (Self::Bar { bin: l_bin }, Self::Bar { bin: r_bin }) => l_bin == r_bin,
448446
Some(ast::FieldList::RecordFieldList(list)) => {
@@ -517,10 +515,7 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
517515
let expr = match arms.len() {
518516
0 => eq_check,
519517
_ => {
520-
if n_cases > arms.len() {
521-
let lhs = make::wildcard_pat().into();
522-
arms.push(make::match_arm(Some(lhs), None, eq_check));
523-
}
518+
arms.push(make::match_arm(Some(make::wildcard_pat().into()), None, eq_check));
524519

525520
let match_target = make::expr_tuple(vec![lhs_name, rhs_name]);
526521
let list = make::match_arm_list(arms).indent(ast::edit::IndentLevel(1));

0 commit comments

Comments
 (0)