]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/src/docs/option_if_let_else.txt
:arrow_up: rust-analyzer
[rust.git] / src / tools / clippy / src / docs / option_if_let_else.txt
1 ### What it does
2 Lints usage of `if let Some(v) = ... { y } else { x }` and
3 `match .. { Some(v) => y, None/_ => x }` which are more
4 idiomatically done with `Option::map_or` (if the else bit is a pure
5 expression) or `Option::map_or_else` (if the else bit is an impure
6 expression).
7
8 ### Why is this bad?
9 Using the dedicated functions of the `Option` type is clearer and
10 more concise than an `if let` expression.
11
12 ### Known problems
13 This lint uses a deliberately conservative metric for checking
14 if the inside of either body contains breaks or continues which will
15 cause it to not suggest a fix if either block contains a loop with
16 continues or breaks contained within the loop.
17
18 ### Example
19 ```
20 let _ = if let Some(foo) = optional {
21     foo
22 } else {
23     5
24 };
25 let _ = match optional {
26     Some(val) => val + 1,
27     None => 5
28 };
29 let _ = if let Some(foo) = optional {
30     foo
31 } else {
32     let y = do_complicated_function();
33     y*y
34 };
35 ```
36
37 should be
38
39 ```
40 let _ = optional.map_or(5, |foo| foo);
41 let _ = optional.map_or(5, |val| val + 1);
42 let _ = optional.map_or_else(||{
43     let y = do_complicated_function();
44     y*y
45 }, |foo| foo);
46 ```