]> git.lizzy.rs Git - rust.git/blob - tests/ui/option_if_let_else.fixed
Auto merge of #7089 - Jarcho:multiple_impls_generic, r=Jarcho
[rust.git] / tests / ui / option_if_let_else.fixed
1 // run-rustfix
2 #![warn(clippy::option_if_let_else)]
3 #![allow(clippy::redundant_closure)]
4 #![allow(clippy::ref_option_ref)]
5
6 fn bad1(string: Option<&str>) -> (bool, &str) {
7     string.map_or((false, "hello"), |x| (true, x))
8 }
9
10 fn else_if_option(string: Option<&str>) -> Option<(bool, &str)> {
11     if string.is_none() {
12         None
13     } else if let Some(x) = string {
14         Some((true, x))
15     } else {
16         Some((false, ""))
17     }
18 }
19
20 fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
21     let _ = string.map_or(0, |s| s.len());
22     let _ = num.as_ref().map_or(&0, |s| s);
23     let _ = num.as_mut().map_or(&mut 0, |s| {
24         *s += 1;
25         s
26     });
27     let _ = num.as_ref().map_or(&0, |s| s);
28     let _ = num.map_or(0, |mut s| {
29         s += 1;
30         s
31     });
32     let _ = num.as_mut().map_or(&mut 0, |s| {
33         *s += 1;
34         s
35     });
36 }
37
38 fn longer_body(arg: Option<u32>) -> u32 {
39     arg.map_or(13, |x| {
40         let y = x * x;
41         y * y
42     })
43 }
44
45 fn impure_else(arg: Option<i32>) {
46     let side_effect = || {
47         println!("return 1");
48         1
49     };
50     let _ = arg.map_or_else(|| side_effect(), |x| x);
51 }
52
53 fn test_map_or_else(arg: Option<u32>) {
54     let _ = arg.map_or_else(|| {
55         let mut y = 1;
56         y = (y + 2 / y) / 2;
57         y = (y + 2 / y) / 2;
58         y
59     }, |x| x * x * x * x);
60 }
61
62 fn negative_tests(arg: Option<u32>) -> u32 {
63     let _ = if let Some(13) = arg { "unlucky" } else { "lucky" };
64     for _ in 0..10 {
65         let _ = if let Some(x) = arg {
66             x
67         } else {
68             continue;
69         };
70     }
71     let _ = if let Some(x) = arg {
72         return x;
73     } else {
74         5
75     };
76     7
77 }
78
79 fn main() {
80     let optional = Some(5);
81     let _ = optional.map_or(5, |x| x + 2);
82     let _ = bad1(None);
83     let _ = else_if_option(None);
84     unop_bad(&None, None);
85     let _ = longer_body(None);
86     test_map_or_else(None);
87     let _ = negative_tests(None);
88     let _ = impure_else(None);
89 }