summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/src/docs/derive_ord_xor_partial_ord.txt
blob: f2107a5f69eea93e87e2c7544940bf5020407629 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
### What it does
Checks for deriving `Ord` but implementing `PartialOrd`
explicitly or vice versa.

### Why is this bad?
The implementation of these traits must agree (for
example for use with `sort`) so it’s probably a bad idea to use a
default-generated `Ord` implementation with an explicitly defined
`PartialOrd`. In particular, the following must hold for any type
implementing `Ord`:

```
k1.cmp(&k2) == k1.partial_cmp(&k2).unwrap()
```

### Example
```
#[derive(Ord, PartialEq, Eq)]
struct Foo;

impl PartialOrd for Foo {
    ...
}
```
Use instead:
```
#[derive(PartialEq, Eq)]
struct Foo;

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

impl Ord for Foo {
    ...
}
```
or, if you don't need a custom ordering:
```
#[derive(Ord, PartialOrd, PartialEq, Eq)]
struct Foo;
```