fix: add fallback case in generated PartialEq impl

This commit is contained in:
rami3l 2022-12-06 21:40:45 +08:00
parent df07c8fafa
commit fed74c8b71
2 changed files with 33 additions and 7 deletions

View file

@ -907,7 +907,7 @@ impl PartialEq for Foo {
}
#[test]
fn add_custom_impl_partial_eq_tuple_enum() {
fn add_custom_impl_partial_eq_partial_tuple_enum() {
check_assist(
replace_derive_with_manual_impl,
r#"
@ -936,6 +936,37 @@ impl PartialEq for Foo {
)
}
#[test]
fn add_custom_impl_partial_eq_tuple_enum() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: eq, derive
#[derive(Partial$0Eq)]
enum Foo {
Bar(String),
Baz(i32),
}
"#,
r#"
enum Foo {
Bar(String),
Baz(i32),
}
impl PartialEq for Foo {
$0fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Bar(l0), Self::Bar(r0)) => l0 == r0,
(Self::Baz(l0), Self::Baz(r0)) => l0 == r0,
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
}
}
}
"#,
)
}
#[test]
fn add_custom_impl_partial_eq_record_enum() {
check_assist(

View file

@ -439,10 +439,8 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
let eq_check =
make::expr_bin_op(lhs, BinaryOp::CmpOp(CmpOp::Eq { negated: false }), rhs);
let mut n_cases = 0;
let mut arms = vec![];
for variant in enum_.variant_list()?.variants() {
n_cases += 1;
match variant.field_list() {
// => (Self::Bar { bin: l_bin }, Self::Bar { bin: r_bin }) => l_bin == r_bin,
Some(ast::FieldList::RecordFieldList(list)) => {
@ -517,10 +515,7 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
let expr = match arms.len() {
0 => eq_check,
_ => {
if n_cases > arms.len() {
let lhs = make::wildcard_pat().into();
arms.push(make::match_arm(Some(lhs), None, eq_check));
}
arms.push(make::match_arm(Some(make::wildcard_pat().into()), None, eq_check));
let match_target = make::expr_tuple(vec![lhs_name, rhs_name]);
let list = make::match_arm_list(arms).indent(ast::edit::IndentLevel(1));