Skip to content
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

clippy::non_canonical_partial_ord_impl warns for canonical forwarding implementation #14149

Open
hrxi opened this issue Feb 5, 2025 · 0 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@hrxi
Copy link
Contributor

hrxi commented Feb 5, 2025

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
@hrxi hrxi added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

1 participant