You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a type manually implements both PartialOrd and Ord, both implementations must agree i.e. behave the same way. Usually, the function bodies of ord and partial_ord are identical, save for wrapping the return expression in Some() for partial_ord. This is unnecessarily repetitive and makes the code more prone to error during refactoring, as one may forget to update one of the implementations. The best way of implementing these traits manually is to implement PartialOrd in terms of Ord, which reduces code duplication and guarantees that they function in the same way.
This lint should not trigger if the bounds on the impl for Ord are more restrictive than the bounds on PartialOrd.
Implementations of PartialOrd and Ord are guaranteed to agree. This is safer for future refactoring, since you can't forget to update one of the two impls.
Drawbacks
Will trigger on code that isn't broken which may be annoying, but following the lint's advice will always (as far as I can tell) make the code more robust.
Example
implOrdforF{fncmp(&self,other:&Self) -> Ordering{matchself.a.cmp(&other.b){// .. some complex logic}}}implPartialOrdforF{fnpartial_cmp(&self,other:&Self) -> Option<Ordering>{Some(matchself.a.cmp(&other.b){// .. some complex logic})}}
What it does
When a type manually implements both
PartialOrd
andOrd
, both implementations must agree i.e. behave the same way. Usually, the function bodies oford
andpartial_ord
are identical, save for wrapping the return expression inSome()
forpartial_ord
. This is unnecessarily repetitive and makes the code more prone to error during refactoring, as one may forget to update one of the implementations. The best way of implementing these traits manually is to implementPartialOrd
in terms ofOrd
, which reduces code duplication and guarantees that they function in the same way.This lint should not trigger if the bounds on the impl for
Ord
are more restrictive than the bounds onPartialOrd
.Real-world example: bevyengine/bevy#8529 (comment).
Lint Name
duplicate_manual_partial_ord_impl
Category
style, complexity
Advantage
PartialOrd
andOrd
are guaranteed to agree. This is safer for future refactoring, since you can't forget to update one of the two impls.Drawbacks
Will trigger on code that isn't broken which may be annoying, but following the lint's advice will always (as far as I can tell) make the code more robust.
Example
Could be written as:
The text was updated successfully, but these errors were encountered: