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