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