]> git.lizzy.rs Git - rust.git/blob - tests/ui/option_if_let_else.fixed
Lint for if let Some(x) = ... instead of Option::map_or
[rust.git] / tests / ui / option_if_let_else.fixed
1 // run-rustfix
2 #![warn(clippy::option_if_let_else)]
3
4 fn bad1(string: Option<&str>) -> (bool, &str) {
5     string.map_or((false, "hello"), |x| (true, x))
6 }
7
8 fn longer_body(arg: Option<u32>) -> u32 {
9     arg.map_or(13, |x| {
10         let y = x * x;
11         y * y
12     })
13 }
14
15 fn test_map_or_else(arg: Option<u32>) {
16     let _ = arg.map_or_else(|| {
17         let mut y = 1;
18         y = (y + 2 / y) / 2;
19         y = (y + 2 / y) / 2;
20         y
21     }, |x| x * x * x * x);
22 }
23
24 fn negative_tests(arg: Option<u32>) -> u32 {
25     let _ = if let Some(13) = arg { "unlucky" } else { "lucky" };
26     for _ in 0..10 {
27         let _ = if let Some(x) = arg {
28             x
29         } else {
30             continue;
31         };
32     }
33     let _ = if let Some(x) = arg {
34         return x;
35     } else {
36         5
37     };
38     7
39 }
40
41 fn main() {
42     let optional = Some(5);
43     let _ = optional.map_or(5, |x| x + 2);
44     let _ = bad1(None);
45     let _ = longer_body(None);
46     test_map_or_else(None);
47     let _ = negative_tests(None);
48 }