]> git.lizzy.rs Git - rust.git/blob - tests/ui/manual_map_option_2.fixed
a004ea79d3b6da4cfde0643bf3f6d73e8acfa88d
[rust.git] / tests / ui / manual_map_option_2.fixed
1 // run-rustfix
2
3 #![warn(clippy::manual_map)]
4 #![allow(clippy::toplevel_ref_arg)]
5
6 fn main() {
7     // Lint. `y` is declared within the arm, so it isn't captured by the map closure
8     let _ = Some(0).map(|x| {
9             let y = (String::new(), String::new());
10             (x, y.0)
11         });
12
13     // Don't lint. `s` is borrowed until partway through the arm, but needs to be captured by the map
14     // closure
15     let s = Some(String::new());
16     let _ = match &s {
17         Some(x) => Some((x.clone(), s)),
18         None => None,
19     };
20
21     // Don't lint. `s` is borrowed until partway through the arm, but needs to be captured by the map
22     // closure
23     let s = Some(String::new());
24     let _ = match &s {
25         Some(x) => Some({
26             let clone = x.clone();
27             let s = || s;
28             (clone, s())
29         }),
30         None => None,
31     };
32
33     // Don't lint. `s` is borrowed until partway through the arm, but needs to be captured as a mutable
34     // reference by the map closure
35     let mut s = Some(String::new());
36     let _ = match &s {
37         Some(x) => Some({
38             let clone = x.clone();
39             let ref mut s = s;
40             (clone, s)
41         }),
42         None => None,
43     };
44
45     // Lint. `s` is captured by reference, so no lifetime issues.
46     let s = Some(String::new());
47     let _ = s.as_ref().map(|x| {
48             if let Some(ref s) = s { (x.clone(), s) } else { panic!() }
49         });
50
51     unsafe fn f(x: u32) -> u32 {
52         x
53     }
54     unsafe {
55         let _ = Some(0).map(|x| f(x));
56     }
57     let _ = Some(0).map(|x| unsafe { f(x) });
58 }