]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/src/docs/derive_ord_xor_partial_ord.txt
Auto merge of #104673 - matthiaskrgr:rollup-85f65ov, r=matthiaskrgr
[rust.git] / src / tools / clippy / src / docs / derive_ord_xor_partial_ord.txt
1 ### What it does
2 Checks for deriving `Ord` but implementing `PartialOrd`
3 explicitly or vice versa.
4
5 ### Why is this bad?
6 The implementation of these traits must agree (for
7 example for use with `sort`) so it’s probably a bad idea to use a
8 default-generated `Ord` implementation with an explicitly defined
9 `PartialOrd`. In particular, the following must hold for any type
10 implementing `Ord`:
11
12 ```
13 k1.cmp(&k2) == k1.partial_cmp(&k2).unwrap()
14 ```
15
16 ### Example
17 ```
18 #[derive(Ord, PartialEq, Eq)]
19 struct Foo;
20
21 impl PartialOrd for Foo {
22     ...
23 }
24 ```
25 Use instead:
26 ```
27 #[derive(PartialEq, Eq)]
28 struct Foo;
29
30 impl PartialOrd for Foo {
31     fn partial_cmp(&self, other: &Foo) -> Option<Ordering> {
32        Some(self.cmp(other))
33     }
34 }
35
36 impl Ord for Foo {
37     ...
38 }
39 ```
40 or, if you don't need a custom ordering:
41 ```
42 #[derive(Ord, PartialOrd, PartialEq, Eq)]
43 struct Foo;
44 ```