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