]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/option_if_let_else.fixed
Merge commit '2ca58e7dda4a9eb142599638c59dc04d15961175' into clippyup
[rust.git] / src / tools / clippy / 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 else_if_option(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 unop_bad(string: &Option<&str>, mut num: Option<i32>) {
15     let _ = string.map_or(0, |s| s.len());
16     let _ = num.as_ref().map_or(&0, |s| s);
17     let _ = num.as_mut().map_or(&mut 0, |s| {
18         *s += 1;
19         s
20     });
21     let _ = num.as_ref().map_or(&0, |s| s);
22     let _ = num.map_or(0, |mut s| {
23         s += 1;
24         s
25     });
26     let _ = num.as_mut().map_or(&mut 0, |s| {
27         *s += 1;
28         s
29     });
30 }
31
32 fn longer_body(arg: Option<u32>) -> u32 {
33     arg.map_or(13, |x| {
34         let y = x * x;
35         y * y
36     })
37 }
38
39 fn test_map_or_else(arg: Option<u32>) {
40     let _ = arg.map_or_else(|| {
41         let mut y = 1;
42         y = (y + 2 / y) / 2;
43         y = (y + 2 / y) / 2;
44         y
45     }, |x| x * x * x * x);
46 }
47
48 fn negative_tests(arg: Option<u32>) -> u32 {
49     let _ = if let Some(13) = arg { "unlucky" } else { "lucky" };
50     for _ in 0..10 {
51         let _ = if let Some(x) = arg {
52             x
53         } else {
54             continue;
55         };
56     }
57     let _ = if let Some(x) = arg {
58         return x;
59     } else {
60         5
61     };
62     7
63 }
64
65 fn main() {
66     let optional = Some(5);
67     let _ = optional.map_or(5, |x| x + 2);
68     let _ = bad1(None);
69     let _ = else_if_option(None);
70     unop_bad(&None, None);
71     let _ = longer_body(None);
72     test_map_or_else(None);
73     let _ = negative_tests(None);
74 }