rust-lang / rust-lang/rust-clippy

`clippy::non_canonical_partial_ord_impl` warns for canonical forwarding implementation

Open
#14,149 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Lint Name

clippy::non_canonical_partial_ord_impl

Reproducer
use std::cmp::Ordering;

struct A {
    inner: i32,
    cache: i32,
}

impl PartialEq for A {
    fn eq(&self, other: &A) -> bool {
        self.inner == other.inner
    }
}

impl Eq for A {}

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

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

Results in this warning:

warning: non-canonical implementation of `partial_cmp` on an `Ord` type
  --> 16:1
   |
16 | /  impl PartialOrd for A {
17 | |      fn partial_cmp(&self, other: &A) -> Option<Ordering> {
   | | __________________________________________________________-
18 | ||         self.inner.partial_cmp(&other.inner)
19 | ||     }
   | ||_____- help: change this to: `{ Some(self.cmp(other)) }`
20 | |  }
   | |__^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#non_canonical_partial_ord_impl
   = note: `#[warn(clippy::non_canonical_partial_ord_impl)]` on by default

But I expected no warning for this rather canonical forwarding of traits to one particular member.

Version
rustc 1.84.0 (9fc6b4312 2025-01-07)
binary: rustc
commit-hash: 9fc6b43126469e3858e2fe86cafb4f0fd5068869
commit-date: 2025-01-07
host: x86_64-unknown-linux-gnu
release: 1.84.0
LLVM version: 19.1.5

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the clippy::non_canonical_partial_ord_impl lint and the Rust reproducer in the issue. Inspect how the lint recognizes canonical PartialOrd implementations and compare that behavior with the shown forwarding implementation. Done means the reproducer no longer produces an incorrect warning while genuinely non-canonical implementations remain diagnosed.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.