Skip to content

Commit c001b9c

Browse files
committed
Auto merge of #12821 - SpecialMike:fix-partial-eq-default, r=Veykril
fix: Correctly generate default `PartialEq::ne` Fixes #12779 For the `Generate default members` assist on the `PartialEq` trait, the assist will now give the default implementation instead of generating a function.
2 parents 0e71356 + 1c32fcf commit c001b9c

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

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

+26
Original file line numberDiff line numberDiff line change
@@ -1305,4 +1305,30 @@ impl Trait<u32> for Impl {
13051305
}"#,
13061306
);
13071307
}
1308+
1309+
#[test]
1310+
fn test_default_partial_eq() {
1311+
check_assist(
1312+
add_missing_default_members,
1313+
r#"
1314+
//- minicore: eq
1315+
struct SomeStruct {
1316+
data: usize,
1317+
field: (usize, usize),
1318+
}
1319+
impl PartialEq for SomeStruct {$0}
1320+
"#,
1321+
r#"
1322+
struct SomeStruct {
1323+
data: usize,
1324+
field: (usize, usize),
1325+
}
1326+
impl PartialEq for SomeStruct {
1327+
$0fn ne(&self, other: &Self) -> bool {
1328+
!self.eq(other)
1329+
}
1330+
}
1331+
"#,
1332+
);
1333+
}
13081334
}

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ fn gen_hash_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
394394

395395
/// Generate a `PartialEq` impl based on the fields and members of the target type.
396396
fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
397+
if func.name().map_or(false, |name| name.text() == "ne") {
398+
return None;
399+
}
397400
fn gen_eq_chain(expr: Option<ast::Expr>, cmp: ast::Expr) -> Option<ast::Expr> {
398401
match expr {
399402
Some(expr) => Some(make::expr_bin_op(expr, BinaryOp::LogicOp(LogicOp::And), cmp)),
@@ -424,7 +427,7 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
424427
// generate this code `Self` for the time being.
425428

426429
let body = match adt {
427-
// `Hash` cannot be derived for unions, so no default impl can be provided.
430+
// `PartialEq` cannot be derived for unions, so no default impl can be provided.
428431
ast::Adt::Union(_) => return None,
429432

430433
ast::Adt::Enum(enum_) => {

crates/test-utils/src/minicore.rs

+3
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,9 @@ pub mod cmp {
354354
#[lang = "eq"]
355355
pub trait PartialEq<Rhs: ?Sized = Self> {
356356
fn eq(&self, other: &Rhs) -> bool;
357+
fn ne(&self, other: &Rhs) -> bool {
358+
!self.eq(other)
359+
}
357360
}
358361

359362
pub trait Eq: PartialEq<Self> {}

0 commit comments

Comments
 (0)