about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs60
-rw-r--r--crates/ide-assists/src/utils/gen_trait_fn_body.rs14
2 files changed, 70 insertions, 4 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 6fa15b28e4e..2854701c088 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
@@ -907,7 +907,34 @@ impl PartialEq for Foo {
     }
 
     #[test]
-    fn add_custom_impl_partial_eq_tuple_enum() {
+    fn add_custom_impl_partial_eq_single_variant_tuple_enum() {
+        check_assist(
+            replace_derive_with_manual_impl,
+            r#"
+//- minicore: eq, derive
+#[derive(Partial$0Eq)]
+enum Foo {
+    Bar(String),
+}
+"#,
+            r#"
+enum Foo {
+    Bar(String),
+}
+
+impl PartialEq for Foo {
+    $0fn eq(&self, other: &Self) -> bool {
+        match (self, other) {
+            (Self::Bar(l0), Self::Bar(r0)) => l0 == r0,
+        }
+    }
+}
+"#,
+        )
+    }
+
+    #[test]
+    fn add_custom_impl_partial_eq_partial_tuple_enum() {
         check_assist(
             replace_derive_with_manual_impl,
             r#"
@@ -937,6 +964,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,
+            _ => false,
+        }
+    }
+}
+"#,
+        )
+    }
+
+    #[test]
     fn add_custom_impl_partial_eq_record_enum() {
         check_assist(
             replace_derive_with_manual_impl,
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 6c87e66c134..f32e5ce97d2 100644
--- a/crates/ide-assists/src/utils/gen_trait_fn_body.rs
+++ b/crates/ide-assists/src/utils/gen_trait_fn_body.rs
@@ -516,10 +516,18 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
 
             let expr = match arms.len() {
                 0 => eq_check,
-                _ => {
-                    if n_cases > arms.len() {
+                arms_len => {
+                    // Generate the fallback arm when this enum has >1 variants.
+                    // The fallback arm will be `_ => false,` if we've already gone through every case where the variants of self and other match,
+                    // and `_ => std::mem::discriminant(self) == std::mem::discriminant(other),` otherwise.
+                    if n_cases > 1 {
                         let lhs = make::wildcard_pat().into();
-                        arms.push(make::match_arm(Some(lhs), None, eq_check));
+                        let rhs = if arms_len == n_cases {
+                            make::expr_literal("false").into()
+                        } else {
+                            eq_check
+                        };
+                        arms.push(make::match_arm(Some(lhs), None, rhs));
                     }
 
                     let match_target = make::expr_tuple(vec![lhs_name, rhs_name]);