]> git.lizzy.rs Git - rust.git/blob - tests/ui/manual_map_option.fixed
Don't allow adjustments for `manual_map`
[rust.git] / tests / ui / manual_map_option.fixed
1 // edition:2018
2 // run-rustfix
3
4 #![warn(clippy::manual_map)]
5 #![allow(
6     clippy::no_effect,
7     clippy::map_identity,
8     clippy::unit_arg,
9     clippy::match_ref_pats,
10     clippy::redundant_pattern_matching,
11     dead_code
12 )]
13
14 fn main() {
15     Some(0).map(|_| 2);
16
17     Some(0).map(|x| x + 1);
18
19     Some("").map(|x| x.is_empty());
20
21     Some(0).map(|x| !x);
22
23     #[rustfmt::skip]
24     Some(0).map(std::convert::identity);
25
26     Some(&String::new()).map(|x| str::len(x));
27
28     match Some(0) {
29         Some(x) if false => Some(x + 1),
30         _ => None,
31     };
32
33     Some([0, 1]).as_ref().map(|x| x[0]);
34
35     Some(0).map(|x| x * 2);
36
37     Some(String::new()).as_ref().map(|x| x.is_empty());
38
39     Some(String::new()).as_ref().map(|x| x.len());
40
41     Some(0).map(|x| x + x);
42
43     #[warn(clippy::option_map_unit_fn)]
44     match &mut Some(String::new()) {
45         Some(x) => Some(x.push_str("")),
46         None => None,
47     };
48
49     #[allow(clippy::option_map_unit_fn)]
50     {
51         Some(String::new()).as_mut().map(|x| x.push_str(""));
52     }
53
54     Some(String::new()).as_ref().map(|x| x.len());
55
56     Some(String::new()).as_ref().map(|x| x.is_empty());
57
58     Some((0, 1, 2)).map(|(x, y, z)| x + y + z);
59
60     Some([1, 2, 3]).map(|[first, ..]| first);
61
62     Some((String::new(), "test")).as_ref().map(|(x, y)| (y, x));
63
64     match Some((String::new(), 0)) {
65         Some((ref x, y)) => Some((y, x)),
66         None => None,
67     };
68
69     match Some(Some(0)) {
70         Some(Some(_)) | Some(None) => Some(0),
71         None => None,
72     };
73
74     match Some(Some((0, 1))) {
75         Some(Some((x, 1))) => Some(x),
76         _ => None,
77     };
78
79     // #6795
80     fn f1() -> Result<(), ()> {
81         let _ = match Some(Ok(())) {
82             Some(x) => Some(x?),
83             None => None,
84         };
85         Ok(())
86     }
87
88     for &x in Some(Some(true)).iter() {
89         let _ = match x {
90             Some(x) => Some(if x { continue } else { x }),
91             None => None,
92         };
93     }
94
95     // #6797
96     let x1 = (Some(String::new()), 0);
97     let x2 = x1.0;
98     match x2 {
99         Some(x) => Some((x, x1.1)),
100         None => None,
101     };
102
103     struct S1 {
104         x: Option<String>,
105         y: u32,
106     }
107     impl S1 {
108         fn f(self) -> Option<(String, u32)> {
109             match self.x {
110                 Some(x) => Some((x, self.y)),
111                 None => None,
112             }
113         }
114     }
115
116     // #6811
117     Some(0).map(|x| vec![x]);
118
119     option_env!("").map(String::from);
120
121     // #6819
122     async fn f2(x: u32) -> u32 {
123         x
124     }
125
126     async fn f3() {
127         match Some(0) {
128             Some(x) => Some(f2(x).await),
129             None => None,
130         };
131     }
132
133     // #6847
134     if let Some(_) = Some(0) {
135         Some(0)
136     } else { Some(0).map(|x| x + 1) };
137
138     if true {
139         Some(0)
140     } else { Some(0).map(|x| x + 1) };
141
142     // #6967
143     const fn f4() {
144         match Some(0) {
145             Some(x) => Some(x + 1),
146             None => None,
147         };
148     }
149
150     // #7077
151     let s = &String::new();
152     let _: Option<&str> = match Some(s) {
153         Some(s) => Some(s),
154         None => None,
155     };
156 }