rust-lang / rust-lang/rust-clippy
False alarm with derive_ord_xor_partial_ord
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
In my application, I have a struct wrapping a f32, but guaranteeing non-NaN values. Therefore, this struct can have an Ord implementation.
I've implemented this using:
#[derive(PartialOrd)]
struct MyWrapper {
inner: f32,
}
impl Ord for MyWrapper {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.partial_cmp(other).expect("Can't happen; this wrapper guarantees non-NaN")
}
}
This triggers a deny-by-default warning, telling me that I'm making my Ord and PartialOrd implementation not match (link). However, as you can see, this is not the case here.
This false alarm was easily fixed with #[allow(clippy::derive_ord_xor_partial_ord)], however it may be good to
- either: add this case to the "Known problems" list in the Clippy lint index
- or: improve the lint trigger detection to check whether the Ord implementation internally calls the PartialCmp implementation, and if yes, suppress the lint
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the derive_ord_xor_partial_ord lint and its lint index entry. Determine whether the false-positive case should be documented under known problems or handled by improving detection when Ord delegates to PartialOrd. Done means the selected behavior is implemented or documented and the lint no longer misrepresents this wrapper pattern.
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
- 45/100