sea-orm icon indicating copy to clipboard operation
sea-orm copied to clipboard

[codegen] Struct / enum derive `PartialEq` should also derive `Eq`

Open billy1624 opened this issue 2 years ago • 13 comments

Motivation

New clippy lint was added in Rust 1.63.0. which checks for types that derive PartialEq and could implement Eq.

Example:

#[derive(PartialEq)]
struct Foo {
    i_am_eq: i32,
    i_am_eq_too: Vec<String>,
}

Do this instead:

#[derive(PartialEq, Eq)]
struct Foo {
    i_am_eq: i32,
    i_am_eq_too: Vec<String>,
}

Proposed Solutions

Check sea-orm-codegen where #[derive(PartialEq)] should be changed to #[derive(PartialEq, Eq)].

Additional Information

Clippy lints index: https://rust-lang.github.io/rust-clippy/master/index.html#derive_partial_eq_without_eq

billy1624 avatar Aug 12 '22 10:08 billy1624

Thanks! @baoyachi

billy1624 avatar Aug 12 '22 11:08 billy1624

Oh,I see you've done it。

baoyachi avatar Aug 12 '22 11:08 baoyachi

Nope, it's not

billy1624 avatar Aug 12 '22 11:08 billy1624

https://github.com/SeaQL/sea-orm/pull/967 doesn't touch code generator of codegen

billy1624 avatar Aug 12 '22 11:08 billy1624

It's seem's touch sea-query create

...
/// Value variants
///
/// We want Value to be exactly 1 pointer sized, so anything larger should be boxed.
+ #[derive(Clone, Debug, PartialEq)]
pub enum Value {
    Bool(Option<bool>),
    TinyInt(Option<i8>),
    SmallInt(Option<i16>),
    Int(Option<i32>),
...

baoyachi avatar Aug 12 '22 11:08 baoyachi

So,first need change sea-query?

baoyachi avatar Aug 12 '22 11:08 baoyachi

I don't think we need an option for this behaviour. It seems to be universally true that we want Eq and PartialEq wherever possible.

We can simply change the codegen for enum. For any struct (that contains a f64), implementing Eq is not possible.

tyt2y3 avatar Aug 13 '22 01:08 tyt2y3

There are only three place in the codegen we need to modify:

  • https://github.com/SeaQL/sea-orm/blob/d262501a44c048ed36a17bbf661ced3c5193f455/sea-orm-codegen/src/entity/active_enum.rs#L33-L42
  • https://github.com/SeaQL/sea-orm/blob/d262501a44c048ed36a17bbf661ced3c5193f455/sea-orm-codegen/src/entity/writer.rs#L387-L390
  • https://github.com/SeaQL/sea-orm/blob/d262501a44c048ed36a17bbf661ced3c5193f455/sea-orm-codegen/src/entity/writer.rs#L623-L633

Btw... both f32 and f64 does not implement Eq but implemented PartialEq

billy1624 avatar Aug 16 '22 09:08 billy1624

May I take this one? If I understand correctly, only the above files need to be changed?

w93163red avatar Aug 22 '22 18:08 w93163red

Hey @w93163red, sure! Please go ahead :)

billy1624 avatar Aug 24 '22 04:08 billy1624

But note that if we have any f32 / f64 fields in the model, it shouldn't derive Eq (PartialEq is allowed). Also, we need to add a test cases for this as well.

billy1624 avatar Aug 24 '22 04:08 billy1624

I submitted a PR. If you find anything wrong, please let me know. Thanks~

w93163red avatar Aug 24 '22 19:08 w93163red

Thanks! @w93163red Please check the comment: https://github.com/SeaQL/sea-orm/pull/988#pullrequestreview-1084965612

billy1624 avatar Aug 25 '22 11:08 billy1624