-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Derived PartialOrd and implemented Ord #1621
Comments
Derived Also, I think writing |
Specifically, I think this is the best approach about it:
|
I would like to work on this. Specifically, I would like to create a new |
…=matthiaskrgr Add derive_ord_xor_partial_ord lint Fix #1621 Some remarks: This PR follows the example of the analogous derive_hash_xor_partial_eq lint where possible. I initially tried using the `match_path` function to identify `Ord` implementation like the derive_hash_xor_partial_eq lint currently does, for `Hash` implementations but that didn't work. Specifically, the structs at the top level were getting paths that matched `&["$crate", "cmp", "Ord"]` instead of `&["std", "cmp", "Ord"]`. While trying to figure out what to do instead I saw the comment at the top of [clippy_lints/src/utils/paths.rs](https://github.com/rust-lang/rust-clippy/blob/f5d429cd762423901f946abd800dc2cd91366ccf/clippy_lints/src/utils/paths.rs#L5) that mentioned [this issue](#5393) and suggested to use diagnostic items instead of hardcoded paths whenever possible. I looked for a way to identify `Ord` implementations with diagnostic items, but (possibly because this was the first time I had heard of diagnostic items,) I was unable to find one. Eventually I tried using `get_trait_def_id` and comparing `DefId` values directly and that seems to work as expected. Maybe there's a better approach however? changelog: new lint: derive_ord_xor_partial_ord
…=matthiaskrgr Add derive_ord_xor_partial_ord lint Fix #1621 Some remarks: This PR follows the example of the analogous derive_hash_xor_partial_eq lint where possible. I initially tried using the `match_path` function to identify `Ord` implementation like the derive_hash_xor_partial_eq lint currently does, for `Hash` implementations but that didn't work. Specifically, the structs at the top level were getting paths that matched `&["$crate", "cmp", "Ord"]` instead of `&["std", "cmp", "Ord"]`. While trying to figure out what to do instead I saw the comment at the top of [clippy_lints/src/utils/paths.rs](https://github.com/rust-lang/rust-clippy/blob/f5d429cd762423901f946abd800dc2cd91366ccf/clippy_lints/src/utils/paths.rs#L5) that mentioned [this issue](#5393) and suggested to use diagnostic items instead of hardcoded paths whenever possible. I looked for a way to identify `Ord` implementations with diagnostic items, but (possibly because this was the first time I had heard of diagnostic items,) I was unable to find one. Eventually I tried using `get_trait_def_id` and comparing `DefId` values directly and that seems to work as expected. Maybe there's a better approach however? changelog: new lint: derive_ord_xor_partial_ord
…ord-lint, r=matthiaskrgr Add derive_ord_xor_partial_ord lint Fix rust-lang#1621 Some remarks: This PR follows the example of the analogous derive_hash_xor_partial_eq lint where possible. I initially tried using the `match_path` function to identify `Ord` implementation like the derive_hash_xor_partial_eq lint currently does, for `Hash` implementations but that didn't work. Specifically, the structs at the top level were getting paths that matched `&["$crate", "cmp", "Ord"]` instead of `&["std", "cmp", "Ord"]`. While trying to figure out what to do instead I saw the comment at the top of [clippy_lints/src/utils/paths.rs](https://github.com/rust-lang/rust-clippy/blob/f5d429cd762423901f946abd800dc2cd91366ccf/clippy_lints/src/utils/paths.rs#L5) that mentioned [this issue](rust-lang#5393) and suggested to use diagnostic items instead of hardcoded paths whenever possible. I looked for a way to identify `Ord` implementations with diagnostic items, but (possibly because this was the first time I had heard of diagnostic items,) I was unable to find one. Eventually I tried using `get_trait_def_id` and comparing `DefId` values directly and that seems to work as expected. Maybe there's a better approach however? changelog: new lint: derive_ord_xor_partial_ord
This is important because deriving `PartialOrd` means that it looks at all the struct's fields in order. For this type we want to compare the `timestamp`, but do not care about the `digits`! More importantly, someone could unintentionally change this behaviour by reordering the fields. We also remove the `Ord` derived implementation because: - It's unused. - Deriving `Ord` when manually implementing `PartialOrd` is a very bad idea[1]. [1]: rust-lang/rust-clippy#1621. Co-authored-by: Daniel Karzel <daniel.karzel@coblox.tech>
This is important because deriving `PartialOrd` normally means that it looks at all the struct's fields _in order_. For this type we want to compare the `timestamp`, but do not care about the `digits`! More importantly, someone could unintentionally change this behaviour by reordering the fields. We also have to ignore the `digits` field for other derived traits, because inconsistencies there could cause trouble[1]. [1]: rust-lang/rust-clippy#1621. Co-authored-by: Daniel Karzel <daniel.karzel@coblox.tech>
This is important because deriving `PartialOrd` normally means that it looks at all the struct's fields _in order_. For this type we want to compare the `timestamp`, but do not care about the `digits`! More importantly, someone could unintentionally change this behaviour by reordering the fields. We also have to ignore the `digits` field for other derived traits, because inconsistencies there could cause trouble[1]. [1]: rust-lang/rust-clippy#1621. Co-authored-by: Daniel Karzel <daniel.karzel@coblox.tech>
This is important because deriving `PartialOrd` normally means that it looks at all the struct's fields _in order_. For this type we want to compare the `timestamp`, but do not care about the `digits`! More importantly, someone could unintentionally change this behaviour by reordering the fields. We also have to ignore the `digits` field for other derived traits, because inconsistencies there could cause trouble[1]. [1]: rust-lang/rust-clippy#1621. Co-authored-by: Daniel Karzel <daniel.karzel@coblox.tech>
This is important because deriving `PartialOrd` normally means that it looks at all the struct's fields _in order_. For this type we want to compare the `timestamp`, but do not care about the `digits`! More importantly, someone could unintentionally change this behaviour by reordering the fields. We also have to ignore the `digits` field for other derived traits, because inconsistencies there could cause trouble[1]. [1]: rust-lang/rust-clippy#1621. Co-authored-by: Daniel Karzel <daniel.karzel@coblox.tech>
This is important because deriving `PartialOrd` normally means that it looks at all the struct's fields _in order_. For this type we want to compare the `timestamp`, but do not care about the `digits`! More importantly, someone could unintentionally change this behaviour by reordering the fields. We also have to ignore the `digits` field for other derived traits, because inconsistencies there could cause trouble[1]. [1]: rust-lang/rust-clippy#1621. Co-authored-by: Daniel Karzel <daniel.karzel@coblox.tech>
This is important because deriving `PartialOrd` normally means that it looks at all the struct's fields _in order_. For this type we want to compare the `timestamp`, but do not care about the `digits`! More importantly, someone could unintentionally change this behaviour by reordering the fields. We also have to ignore the `digits` field for other derived traits, because inconsistencies there could cause trouble[1]. [1]: rust-lang/rust-clippy#1621. Co-authored-by: Daniel Karzel <daniel.karzel@coblox.tech>
Suggestion: if a user writes
#[derive(PartialOrd)]
and then implementsOrd
manually, we should issue a warning. This is almost certainly wrong and will lead to problems.The same applies to
PartialEq
andEq
.See rust-lang/rust#40192 for an example.
The text was updated successfully, but these errors were encountered: