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