summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/src/docs/neg_cmp_op_on_partial_ord.txt
blob: fa55c6cfd74b3990aea2bfee110ab50e8bd69212 (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
### What it does
Checks for the usage of negated comparison operators on types which only implement
`PartialOrd` (e.g., `f64`).

### Why is this bad?
These operators make it easy to forget that the underlying types actually allow not only three
potential Orderings (Less, Equal, Greater) but also a fourth one (Uncomparable). This is
especially easy to miss if the operator based comparison result is negated.

### Example
```
let a = 1.0;
let b = f64::NAN;

let not_less_or_equal = !(a <= b);
```

Use instead:
```
use std::cmp::Ordering;

let _not_less_or_equal = match a.partial_cmp(&b) {
    None | Some(Ordering::Greater) => true,
    _ => false,
};
```