RS.CLIPPY.NON_CANONICAL_PARTIAL_ORD_IMPL

Non-canonical implementation of `PartialOrd` on an `Ord` type

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: non_canonical_partial_ord_impl. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for non-canonical implementations of PartialOrd when Ord is already implemented.

Why is this bad?

If both PartialOrd and Ord are implemented, they must agree. This is commonly done by wrapping the result of cmp in Some for partial_cmp. Not doing this may silently introduce an error upon refactoring.

Known issues

Code that calls the .into() method instead will be flagged, despite .into() wrapping it in Some.

Example

#[derive(Eq, PartialEq)]
struct A(u32);

impl Ord for A {
    fn cmp(&self, other: &Self) -> Ordering {
        // ...
    }
}

impl PartialOrd for A {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        // ...
    }
}

Use instead:

#[derive(Eq, PartialEq)]
struct A(u32);

impl Ord for A {
    fn cmp(&self, other: &Self) -> Ordering {
        // ...
    }
}

impl PartialOrd for A {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))   // or self.cmp(other).into()
    }
}

Past names

  • incorrect_partial_ord_impl_on_ord_type