Skip to content

Commit 1376ece

Browse files
Merge #9863
9863: feat: Generate default trait fn impl when generating `PartialEq` r=yoshuawuyts a=yoshuawuyts Implements a default trait function body when generating the `PartialEq` trait for a type. Thanks! r? `@veykril` Co-authored-by: Yoshua Wuyts <[email protected]>
2 parents 2511e1b + 97ec6a2 commit 1376ece

File tree

4 files changed

+418
-20
lines changed

4 files changed

+418
-20
lines changed

crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,177 @@ impl Clone for Foo {
606606
}
607607
}
608608
}
609+
"#,
610+
)
611+
}
612+
613+
#[test]
614+
fn add_custom_impl_partial_eq_record_struct() {
615+
check_assist(
616+
replace_derive_with_manual_impl,
617+
r#"
618+
//- minicore: eq
619+
#[derive(Partial$0Eq)]
620+
struct Foo {
621+
bin: usize,
622+
bar: usize,
623+
}
624+
"#,
625+
r#"
626+
struct Foo {
627+
bin: usize,
628+
bar: usize,
629+
}
630+
631+
impl PartialEq for Foo {
632+
$0fn eq(&self, other: &Self) -> bool {
633+
self.bin == other.bin && self.bar == other.bar
634+
}
635+
}
636+
"#,
637+
)
638+
}
639+
640+
#[test]
641+
fn add_custom_impl_partial_eq_tuple_struct() {
642+
check_assist(
643+
replace_derive_with_manual_impl,
644+
r#"
645+
//- minicore: eq
646+
#[derive(Partial$0Eq)]
647+
struct Foo(usize, usize);
648+
"#,
649+
r#"
650+
struct Foo(usize, usize);
651+
652+
impl PartialEq for Foo {
653+
$0fn eq(&self, other: &Self) -> bool {
654+
self.0 == other.0 && self.1 == other.1
655+
}
656+
}
657+
"#,
658+
)
659+
}
660+
661+
#[test]
662+
fn add_custom_impl_partial_eq_empty_struct() {
663+
check_assist(
664+
replace_derive_with_manual_impl,
665+
r#"
666+
//- minicore: eq
667+
#[derive(Partial$0Eq)]
668+
struct Foo;
669+
"#,
670+
r#"
671+
struct Foo;
672+
673+
impl PartialEq for Foo {
674+
$0fn eq(&self, other: &Self) -> bool {
675+
true
676+
}
677+
}
678+
"#,
679+
)
680+
}
681+
682+
#[test]
683+
fn add_custom_impl_partial_eq_enum() {
684+
check_assist(
685+
replace_derive_with_manual_impl,
686+
r#"
687+
//- minicore: eq
688+
#[derive(Partial$0Eq)]
689+
enum Foo {
690+
Bar,
691+
Baz,
692+
}
693+
"#,
694+
r#"
695+
enum Foo {
696+
Bar,
697+
Baz,
698+
}
699+
700+
impl PartialEq for Foo {
701+
$0fn eq(&self, other: &Self) -> bool {
702+
core::mem::discriminant(self) == core::mem::discriminant(other)
703+
}
704+
}
705+
"#,
706+
)
707+
}
708+
709+
#[test]
710+
fn add_custom_impl_partial_eq_tuple_enum() {
711+
check_assist(
712+
replace_derive_with_manual_impl,
713+
r#"
714+
//- minicore: eq
715+
#[derive(Partial$0Eq)]
716+
enum Foo {
717+
Bar(String),
718+
Baz,
719+
}
720+
"#,
721+
r#"
722+
enum Foo {
723+
Bar(String),
724+
Baz,
725+
}
726+
727+
impl PartialEq for Foo {
728+
$0fn eq(&self, other: &Self) -> bool {
729+
match (self, other) {
730+
(Self::Bar(l0), Self::Bar(r0)) => l0 == r0,
731+
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
732+
}
733+
}
734+
}
735+
"#,
736+
)
737+
}
738+
739+
#[test]
740+
fn add_custom_impl_partial_eq_record_enum() {
741+
check_assist(
742+
replace_derive_with_manual_impl,
743+
r#"
744+
//- minicore: eq
745+
#[derive(Partial$0Eq)]
746+
enum Foo {
747+
Bar {
748+
bin: String,
749+
},
750+
Baz {
751+
qux: String,
752+
fez: String,
753+
},
754+
Qux {},
755+
Bin,
756+
}
757+
"#,
758+
r#"
759+
enum Foo {
760+
Bar {
761+
bin: String,
762+
},
763+
Baz {
764+
qux: String,
765+
fez: String,
766+
},
767+
Qux {},
768+
Bin,
769+
}
770+
771+
impl PartialEq for Foo {
772+
$0fn eq(&self, other: &Self) -> bool {
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),
777+
}
778+
}
779+
}
609780
"#,
610781
)
611782
}

0 commit comments

Comments
 (0)