]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/option_if_let_else.fixed
New upstream version 1.74.1+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / option_if_let_else.fixed
1 #![warn(clippy::option_if_let_else)]
2 #![allow(
3 unused_tuple_struct_fields,
4 clippy::redundant_closure,
5 clippy::ref_option_ref,
6 clippy::equatable_if_let,
7 clippy::let_unit_value,
8 clippy::redundant_locals
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(&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(&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 // #10335
96 fn test_result_impure_else(variable: Result<u32, &str>) {
97 variable.map_or_else(|_| {
98 println!("Err");
99 }, |binding| {
100 println!("Ok {binding}");
101 })
102 }
103
104 enum DummyEnum {
105 One(u8),
106 Two,
107 }
108
109 // should not warn since there is a complex subpat
110 // see #7991
111 fn complex_subpat() -> DummyEnum {
112 let x = Some(DummyEnum::One(1));
113 let _ = if let Some(_one @ DummyEnum::One(..)) = x { 1 } else { 2 };
114 DummyEnum::Two
115 }
116
117 fn main() {
118 let optional = Some(5);
119 let _ = optional.map_or(5, |x| x + 2);
120 let _ = bad1(None);
121 let _ = else_if_option(None);
122 unop_bad(&None, None);
123 let _ = longer_body(None);
124 test_map_or_else(None);
125 test_result_impure_else(Ok(42));
126 let _ = negative_tests(None);
127 let _ = impure_else(None);
128
129 let _ = Some(0).map_or(0, |x| loop {
130 if x == 0 {
131 break x;
132 }
133 });
134
135 // #7576
136 const fn _f(x: Option<u32>) -> u32 {
137 // Don't lint, `map_or` isn't const
138 if let Some(x) = x { x } else { 10 }
139 }
140
141 // #5822
142 let s = String::new();
143 // Don't lint, `Some` branch consumes `s`, but else branch uses `s`
144 let _ = if let Some(x) = Some(0) {
145 let s = s;
146 s.len() + x
147 } else {
148 s.len()
149 };
150
151 let s = String::new();
152 // Lint, both branches immutably borrow `s`.
153 let _ = Some(0).map_or(s.len(), |x| s.len() + x);
154
155 let s = String::new();
156 // Lint, `Some` branch consumes `s`, but else branch doesn't use `s`.
157 let _ = Some(0).map_or(1, |x| {
158 let s = s;
159 s.len() + x
160 });
161
162 let s = Some(String::new());
163 // Don't lint, `Some` branch borrows `s`, but else branch consumes `s`
164 let _ = if let Some(x) = &s {
165 x.len()
166 } else {
167 let _s = s;
168 10
169 };
170
171 let mut s = Some(String::new());
172 // Don't lint, `Some` branch mutably borrows `s`, but else branch also borrows `s`
173 let _ = if let Some(x) = &mut s {
174 x.push_str("test");
175 x.len()
176 } else {
177 let _s = &s;
178 10
179 };
180
181 async fn _f1(x: u32) -> u32 {
182 x
183 }
184
185 async fn _f2() {
186 // Don't lint. `await` can't be moved into a closure.
187 let _ = if let Some(x) = Some(0) { _f1(x).await } else { 0 };
188 }
189
190 let _ = pattern_to_vec("hello world");
191 let _ = complex_subpat();
192
193 // issue #8492
194 let _ = s.map_or(1, |string| string.len());
195 let _ = Some(10).map_or(5, |a| a + 1);
196
197 let res: Result<i32, i32> = Ok(5);
198 let _ = res.map_or(1, |a| a + 1);
199 let _ = res.map_or(1, |a| a + 1);
200 let _ = res.map_or(5, |a| a + 1);
201 }
202
203 #[allow(dead_code)]
204 fn issue9742() -> Option<&'static str> {
205 // should not lint because of guards
206 match Some("foo ") {
207 Some(name) if name.starts_with("foo") => Some(name.trim()),
208 _ => None,
209 }
210 }
211
212 mod issue10729 {
213 #![allow(clippy::unit_arg, dead_code)]
214
215 pub fn reproduce(initial: &Option<String>) {
216 // 👇 needs `.as_ref()` because initial is an `&Option<_>`
217 initial.as_ref().map_or({}, |value| do_something(value))
218 }
219
220 pub fn reproduce2(initial: &mut Option<String>) {
221 initial.as_mut().map_or({}, |value| do_something2(value))
222 }
223
224 fn do_something(_value: &str) {}
225 fn do_something2(_value: &mut str) {}
226 }