summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/src/docs/comparison_chain.txt
blob: 43b09f31ff4aa7a6596db9d2456d6c19f63185fa (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
### What it does
Checks comparison chains written with `if` that can be
rewritten with `match` and `cmp`.

### Why is this bad?
`if` is not guaranteed to be exhaustive and conditionals can get
repetitive

### Known problems
The match statement may be slower due to the compiler
not inlining the call to cmp. See issue [#5354](https://github.com/rust-lang/rust-clippy/issues/5354)

### Example
```
fn f(x: u8, y: u8) {
    if x > y {
        a()
    } else if x < y {
        b()
    } else {
        c()
    }
}
```

Use instead:
```
use std::cmp::Ordering;
fn f(x: u8, y: u8) {
     match x.cmp(&y) {
         Ordering::Greater => a(),
         Ordering::Less => b(),
         Ordering::Equal => c()
     }
}
```