]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/option_if_let_else.fixed
Rollup merge of #102412 - joboet:dont_panic, r=m-ou-se
[rust.git] / src / tools / clippy / tests / ui / option_if_let_else.fixed
1 // run-rustfix
2 #![warn(clippy::option_if_let_else)]
3 #![allow(
4     unused_tuple_struct_fields,
5     clippy::redundant_closure,
6     clippy::ref_option_ref,
7     clippy::equatable_if_let,
8     clippy::let_unit_value
9 )]
10
11 fn bad1(string: Option<&str>) -> (bool, &str) {
12     string.map_or((false, "hello"), |x| (true, x))
13 }
14
15 fn else_if_option(string: Option<&str>) -> Option<(bool, &str)> {
16     if string.is_none() {
17         None
18     } else if let Some(x) = string {
19         Some((true, x))
20     } else {
21         Some((false, ""))
22     }
23 }
24
25 fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
26     let _ = string.map_or(0, |s| s.len());
27     let _ = num.as_ref().map_or(&0, |s| s);
28     let _ = num.as_mut().map_or(&mut 0, |s| {
29         *s += 1;
30         s
31     });
32     let _ = num.as_ref().map_or(&0, |s| s);
33     let _ = num.map_or(0, |mut s| {
34         s += 1;
35         s
36     });
37     let _ = num.as_mut().map_or(&mut 0, |s| {
38         *s += 1;
39         s
40     });
41 }
42
43 fn longer_body(arg: Option<u32>) -> u32 {
44     arg.map_or(13, |x| {
45         let y = x * x;
46         y * y
47     })
48 }
49
50 fn impure_else(arg: Option<i32>) {
51     let side_effect = || {
52         println!("return 1");
53         1
54     };
55     let _ = arg.map_or_else(|| side_effect(), |x| x);
56 }
57
58 fn test_map_or_else(arg: Option<u32>) {
59     let _ = arg.map_or_else(|| {
60         let mut y = 1;
61         y = (y + 2 / y) / 2;
62         y = (y + 2 / y) / 2;
63         y
64     }, |x| x * x * x * x);
65 }
66
67 fn negative_tests(arg: Option<u32>) -> u32 {
68     let _ = if let Some(13) = arg { "unlucky" } else { "lucky" };
69     for _ in 0..10 {
70         let _ = if let Some(x) = arg {
71             x
72         } else {
73             continue;
74         };
75     }
76     let _ = if let Some(x) = arg {
77         return x;
78     } else {
79         5
80     };
81     7
82 }
83
84 // #7973
85 fn pattern_to_vec(pattern: &str) -> Vec<String> {
86     pattern
87         .trim_matches('/')
88         .split('/')
89         .flat_map(|s| {
90             s.find('.').map_or_else(|| vec![s.to_string()], |idx| vec![s[..idx].to_string(), s[idx..].to_string()])
91         })
92         .collect::<Vec<_>>()
93 }
94
95 enum DummyEnum {
96     One(u8),
97     Two,
98 }
99
100 // should not warn since there is a compled complex subpat
101 // see #7991
102 fn complex_subpat() -> DummyEnum {
103     let x = Some(DummyEnum::One(1));
104     let _ = if let Some(_one @ DummyEnum::One(..)) = x { 1 } else { 2 };
105     DummyEnum::Two
106 }
107
108 fn main() {
109     let optional = Some(5);
110     let _ = optional.map_or(5, |x| x + 2);
111     let _ = bad1(None);
112     let _ = else_if_option(None);
113     unop_bad(&None, None);
114     let _ = longer_body(None);
115     test_map_or_else(None);
116     let _ = negative_tests(None);
117     let _ = impure_else(None);
118
119     let _ = Some(0).map_or(0, |x| loop {
120             if x == 0 {
121                 break x;
122             }
123         });
124
125     // #7576
126     const fn _f(x: Option<u32>) -> u32 {
127         // Don't lint, `map_or` isn't const
128         if let Some(x) = x { x } else { 10 }
129     }
130
131     // #5822
132     let s = String::new();
133     // Don't lint, `Some` branch consumes `s`, but else branch uses `s`
134     let _ = if let Some(x) = Some(0) {
135         let s = s;
136         s.len() + x
137     } else {
138         s.len()
139     };
140
141     let s = String::new();
142     // Lint, both branches immutably borrow `s`.
143     let _ = Some(0).map_or(s.len(), |x| s.len() + x);
144
145     let s = String::new();
146     // Lint, `Some` branch consumes `s`, but else branch doesn't use `s`.
147     let _ = Some(0).map_or(1, |x| {
148         let s = s;
149         s.len() + x
150     });
151
152     let s = Some(String::new());
153     // Don't lint, `Some` branch borrows `s`, but else branch consumes `s`
154     let _ = if let Some(x) = &s {
155         x.len()
156     } else {
157         let _s = s;
158         10
159     };
160
161     let mut s = Some(String::new());
162     // Don't lint, `Some` branch mutably borrows `s`, but else branch also borrows  `s`
163     let _ = if let Some(x) = &mut s {
164         x.push_str("test");
165         x.len()
166     } else {
167         let _s = &s;
168         10
169     };
170
171     async fn _f1(x: u32) -> u32 {
172         x
173     }
174
175     async fn _f2() {
176         // Don't lint. `await` can't be moved into a closure.
177         let _ = if let Some(x) = Some(0) { _f1(x).await } else { 0 };
178     }
179
180     let _ = pattern_to_vec("hello world");
181     let _ = complex_subpat();
182
183     // issue #8492
184     let _ = s.map_or(1, |string| string.len());
185     let _ = Some(10).map_or(5, |a| a + 1);
186
187     let res: Result<i32, i32> = Ok(5);
188     let _ = res.map_or(1, |a| a + 1);
189     let _ = res.map_or(1, |a| a + 1);
190     let _ = res.map_or(5, |a| a + 1);
191 }