RS.CLIPPY.DERIVE_ORD_XOR_PARTIAL_ORD
Deriving `Ord` but implementing `PartialOrd` explicitly
This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: derive_ord_xor_partial_ord. Copyright ©2025 The Rust Team. All rights reserved.
What it does
Lints against manual PartialOrd and Ord implementations for types with a derived Ord
or PartialOrd implementation.
Why is this bad?
The implementation of these traits must agree (for
example for use with sort) so it's probably a bad idea to use a
default-generated Ord implementation with an explicitly defined
PartialOrd. In particular, the following must hold for any type
implementing Ord:
k1.cmp(&k2) == k1.partial_cmp(&k2).unwrap()
Example
#[derive(Ord, PartialEq, Eq)]
struct Foo;
impl PartialOrd for Foo {
...
}
Use instead:
#[derive(PartialEq, Eq)]
struct Foo;
impl PartialOrd for Foo {
fn partial_cmp(&self, other: &Foo) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Foo {
...
}
or, if you don't need a custom ordering:
#[derive(Ord, PartialOrd, PartialEq, Eq)]
struct Foo;