]> git.lizzy.rs Git - rust.git/blob - tests/ui/option_if_let_else.fixed
Auto merge of #7726 - dswij:unseparated-literal-suffix, r=flip1995
[rust.git] / tests / ui / option_if_let_else.fixed
1 // run-rustfix
2 #![warn(clippy::option_if_let_else)]
3 #![allow(clippy::redundant_closure, clippy::ref_option_ref, clippy::equatable_if_let)]
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 if let Some(x) = string {
13         Some((true, x))
14     } else {
15         Some((false, ""))
16     }
17 }
18
19 fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
20     let _ = string.map_or(0, |s| s.len());
21     let _ = num.as_ref().map_or(&0, |s| s);
22     let _ = num.as_mut().map_or(&mut 0, |s| {
23         *s += 1;
24         s
25     });
26     let _ = num.as_ref().map_or(&0, |s| s);
27     let _ = num.map_or(0, |mut s| {
28         s += 1;
29         s
30     });
31     let _ = num.as_mut().map_or(&mut 0, |s| {
32         *s += 1;
33         s
34     });
35 }
36
37 fn longer_body(arg: Option<u32>) -> u32 {
38     arg.map_or(13, |x| {
39         let y = x * x;
40         y * y
41     })
42 }
43
44 fn impure_else(arg: Option<i32>) {
45     let side_effect = || {
46         println!("return 1");
47         1
48     };
49     let _ = arg.map_or_else(|| side_effect(), |x| x);
50 }
51
52 fn test_map_or_else(arg: Option<u32>) {
53     let _ = arg.map_or_else(|| {
54         let mut y = 1;
55         y = (y + 2 / y) / 2;
56         y = (y + 2 / y) / 2;
57         y
58     }, |x| x * x * x * x);
59 }
60
61 fn negative_tests(arg: Option<u32>) -> u32 {
62     let _ = if let Some(13) = arg { "unlucky" } else { "lucky" };
63     for _ in 0..10 {
64         let _ = if let Some(x) = arg {
65             x
66         } else {
67             continue;
68         };
69     }
70     let _ = if let Some(x) = arg {
71         return x;
72     } else {
73         5
74     };
75     7
76 }
77
78 fn main() {
79     let optional = Some(5);
80     let _ = optional.map_or(5, |x| x + 2);
81     let _ = bad1(None);
82     let _ = else_if_option(None);
83     unop_bad(&None, None);
84     let _ = longer_body(None);
85     test_map_or_else(None);
86     let _ = negative_tests(None);
87     let _ = impure_else(None);
88
89     let _ = Some(0).map_or(0, |x| loop {
90             if x == 0 {
91                 break x;
92             }
93         });
94
95     // #7576
96     const fn _f(x: Option<u32>) -> u32 {
97         // Don't lint, `map_or` isn't const
98         if let Some(x) = x { x } else { 10 }
99     }
100
101     // #5822
102     let s = String::new();
103     // Don't lint, `Some` branch consumes `s`, but else branch uses `s`
104     let _ = if let Some(x) = Some(0) {
105         let s = s;
106         s.len() + x
107     } else {
108         s.len()
109     };
110
111     let s = String::new();
112     // Lint, both branches immutably borrow `s`.
113     let _ = Some(0).map_or_else(|| s.len(), |x| s.len() + x);
114
115     let s = String::new();
116     // Lint, `Some` branch consumes `s`, but else branch doesn't use `s`.
117     let _ = Some(0).map_or(1, |x| {
118         let s = s;
119         s.len() + x
120     });
121
122     let s = Some(String::new());
123     // Don't lint, `Some` branch borrows `s`, but else branch consumes `s`
124     let _ = if let Some(x) = &s {
125         x.len()
126     } else {
127         let _s = s;
128         10
129     };
130
131     let mut s = Some(String::new());
132     // Don't lint, `Some` branch mutably borrows `s`, but else branch also borrows  `s`
133     let _ = if let Some(x) = &mut s {
134         x.push_str("test");
135         x.len()
136     } else {
137         let _s = &s;
138         10
139     };
140
141     async fn _f1(x: u32) -> u32 {
142         x
143     }
144
145     async fn _f2() {
146         // Don't lint. `await` can't be moved into a closure.
147         let _ = if let Some(x) = Some(0) { _f1(x).await } else { 0 };
148     }
149 }