]> git.lizzy.rs Git - rust.git/blob - tests/ui/option_if_let_else.fixed
Stop linting on macros and correctly use braces for constructs
[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 bad2(string: Option<&str>) -> Option<(bool, &str)> {
9     if string.is_none() {
10         None
11     } else { string.map_or(Some((false, "")), |x| Some((true, x))) }
12 }
13
14 fn longer_body(arg: Option<u32>) -> u32 {
15     arg.map_or(13, |x| {
16         let y = x * x;
17         y * y
18     })
19 }
20
21 fn test_map_or_else(arg: Option<u32>) {
22     let _ = arg.map_or_else(|| {
23         let mut y = 1;
24         y = (y + 2 / y) / 2;
25         y = (y + 2 / y) / 2;
26         y
27     }, |x| x * x * x * x);
28 }
29
30 fn negative_tests(arg: Option<u32>) -> u32 {
31     let _ = if let Some(13) = arg { "unlucky" } else { "lucky" };
32     for _ in 0..10 {
33         let _ = if let Some(x) = arg {
34             x
35         } else {
36             continue;
37         };
38     }
39     let _ = if let Some(x) = arg {
40         return x;
41     } else {
42         5
43     };
44     7
45 }
46
47 fn main() {
48     let optional = Some(5);
49     let _ = optional.map_or(5, |x| x + 2);
50     let _ = bad1(None);
51     let _ = bad2(None);
52     let _ = longer_body(None);
53     test_map_or_else(None);
54     let _ = negative_tests(None);
55 }