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