-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #5848 - Ryan1729:add-derive_ord_xor_partial_ord-lint, r…
…=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
- Loading branch information
Showing
6 changed files
with
264 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#![warn(clippy::derive_ord_xor_partial_ord)] | ||
|
||
use std::cmp::Ordering; | ||
|
||
#[derive(PartialOrd, Ord, PartialEq, Eq)] | ||
struct DeriveBoth; | ||
|
||
impl PartialEq<u64> for DeriveBoth { | ||
fn eq(&self, _: &u64) -> bool { | ||
true | ||
} | ||
} | ||
|
||
impl PartialOrd<u64> for DeriveBoth { | ||
fn partial_cmp(&self, _: &u64) -> Option<Ordering> { | ||
Some(Ordering::Equal) | ||
} | ||
} | ||
|
||
#[derive(Ord, PartialEq, Eq)] | ||
struct DeriveOrd; | ||
|
||
impl PartialOrd for DeriveOrd { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(other.cmp(self)) | ||
} | ||
} | ||
|
||
#[derive(Ord, PartialEq, Eq)] | ||
struct DeriveOrdWithExplicitTypeVariable; | ||
|
||
impl PartialOrd<DeriveOrdWithExplicitTypeVariable> for DeriveOrdWithExplicitTypeVariable { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(other.cmp(self)) | ||
} | ||
} | ||
|
||
#[derive(PartialOrd, PartialEq, Eq)] | ||
struct DerivePartialOrd; | ||
|
||
impl std::cmp::Ord for DerivePartialOrd { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
Ordering::Less | ||
} | ||
} | ||
|
||
#[derive(PartialOrd, PartialEq, Eq)] | ||
struct ImplUserOrd; | ||
|
||
trait Ord {} | ||
|
||
// We don't want to lint on user-defined traits called `Ord` | ||
impl Ord for ImplUserOrd {} | ||
|
||
mod use_ord { | ||
use std::cmp::{Ord, Ordering}; | ||
|
||
#[derive(PartialOrd, PartialEq, Eq)] | ||
struct DerivePartialOrdInUseOrd; | ||
|
||
impl Ord for DerivePartialOrdInUseOrd { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
Ordering::Less | ||
} | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
error: you are deriving `Ord` but have implemented `PartialOrd` explicitly | ||
--> $DIR/derive_ord_xor_partial_ord.rs:20:10 | ||
| | ||
LL | #[derive(Ord, PartialEq, Eq)] | ||
| ^^^ | ||
| | ||
= note: `-D clippy::derive-ord-xor-partial-ord` implied by `-D warnings` | ||
note: `PartialOrd` implemented here | ||
--> $DIR/derive_ord_xor_partial_ord.rs:23:1 | ||
| | ||
LL | / impl PartialOrd for DeriveOrd { | ||
LL | | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
LL | | Some(other.cmp(self)) | ||
LL | | } | ||
LL | | } | ||
| |_^ | ||
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: you are deriving `Ord` but have implemented `PartialOrd` explicitly | ||
--> $DIR/derive_ord_xor_partial_ord.rs:29:10 | ||
| | ||
LL | #[derive(Ord, PartialEq, Eq)] | ||
| ^^^ | ||
| | ||
note: `PartialOrd` implemented here | ||
--> $DIR/derive_ord_xor_partial_ord.rs:32:1 | ||
| | ||
LL | / impl PartialOrd<DeriveOrdWithExplicitTypeVariable> for DeriveOrdWithExplicitTypeVariable { | ||
LL | | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
LL | | Some(other.cmp(self)) | ||
LL | | } | ||
LL | | } | ||
| |_^ | ||
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: you are implementing `Ord` explicitly but have derived `PartialOrd` | ||
--> $DIR/derive_ord_xor_partial_ord.rs:41:1 | ||
| | ||
LL | / impl std::cmp::Ord for DerivePartialOrd { | ||
LL | | fn cmp(&self, other: &Self) -> Ordering { | ||
LL | | Ordering::Less | ||
LL | | } | ||
LL | | } | ||
| |_^ | ||
| | ||
note: `PartialOrd` implemented here | ||
--> $DIR/derive_ord_xor_partial_ord.rs:38:10 | ||
| | ||
LL | #[derive(PartialOrd, PartialEq, Eq)] | ||
| ^^^^^^^^^^ | ||
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: you are implementing `Ord` explicitly but have derived `PartialOrd` | ||
--> $DIR/derive_ord_xor_partial_ord.rs:61:5 | ||
| | ||
LL | / impl Ord for DerivePartialOrdInUseOrd { | ||
LL | | fn cmp(&self, other: &Self) -> Ordering { | ||
LL | | Ordering::Less | ||
LL | | } | ||
LL | | } | ||
| |_____^ | ||
| | ||
note: `PartialOrd` implemented here | ||
--> $DIR/derive_ord_xor_partial_ord.rs:58:14 | ||
| | ||
LL | #[derive(PartialOrd, PartialEq, Eq)] | ||
| ^^^^^^^^^^ | ||
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: aborting due to 4 previous errors | ||
|