Skip to content

clippy::non-canonical-partial-ord-impl warns if self.cmp(other).into() instead of Some(self.cmp(other)) is used #13640

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

Closed
Holzhaus opened this issue Oct 31, 2024 · 0 comments · Fixed by #14573
Assignees
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

@Holzhaus
Copy link

Summary

The lint causes a warning if self.cmp(other).into() is used inside the PartialEq::partial_cmp() implementation instead of using Some(self.cmp(other)).

Lint Name

clippy::non-canonical-partial-ord-impl

Reproducer

I tried this code:

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

I saw this happen:

error: non-canonical implementation of `partial_cmp` on an `Ord` type
   --> src/taggedfile.rs:287:1
    |
287 | /  impl PartialOrd for TaggedFile {
288 | |      fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
    | | _____________________________________________________________-
289 | ||         self.cmp(other).into()
290 | ||     }
    | ||_____- help: change this to: `{ Some(self.cmp(other)) }`
291 | |  }
    | |__^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#non_canonical_partial_ord_impl
    = note: `-D clippy::non-canonical-partial-ord-impl` implied by `-D warnings`
    = help: to override `-D warnings` add `#[allow(clippy::non_canonical_partial_ord_impl)]`

I expected to see this happen:

No warningg, because self.cmp(other).into() is equivalent to Some(self.cmp(other)) in this context.

Version

rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: x86_64-unknown-linux-gnu
release: 1.82.0
LLVM version: 19.1.1

Additional Labels

No response

@Holzhaus Holzhaus 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 Oct 31, 2024
@samueltardieu samueltardieu self-assigned this Apr 9, 2025
github-merge-queue bot pushed a commit that referenced this issue Apr 9, 2025
`non_canonical_partial_ord_impl` will now recognize two forms of
canonical implementations: `Some(self.cmp(other))` and
`self.cmp(other).into()`.

changelog: [`non_canonical_partial_ord_impl`]: recognize
`self.cmp(other).into()` as a canonical implementation of
`PartialOrd::partial_cmp()`.

Fixes #13640
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

Successfully merging a pull request may close this issue.

2 participants