Lint that encourages people to implement PartialOrd
in terms of Ord
when applicable
#10744
Labels
A-lint
Area: New lints
PartialOrd
in terms of Ord
when applicable
#10744
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: