]> git.lizzy.rs Git - rust.git/blob - src/docs/comparison_chain.txt
Add iter_kv_map lint
[rust.git] / src / docs / comparison_chain.txt
1 ### What it does
2 Checks comparison chains written with `if` that can be
3 rewritten with `match` and `cmp`.
4
5 ### Why is this bad?
6 `if` is not guaranteed to be exhaustive and conditionals can get
7 repetitive
8
9 ### Known problems
10 The match statement may be slower due to the compiler
11 not inlining the call to cmp. See issue [#5354](https://github.com/rust-lang/rust-clippy/issues/5354)
12
13 ### Example
14 ```
15 fn f(x: u8, y: u8) {
16     if x > y {
17         a()
18     } else if x < y {
19         b()
20     } else {
21         c()
22     }
23 }
24 ```
25
26 Use instead:
27 ```
28 use std::cmp::Ordering;
29 fn f(x: u8, y: u8) {
30      match x.cmp(&y) {
31          Ordering::Greater => a(),
32          Ordering::Less => b(),
33          Ordering::Equal => c()
34      }
35 }
36 ```