]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/manual_map_option.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / manual_map_option.rs
1 // run-rustfix
2
3 #![warn(clippy::manual_map)]
4 #![allow(
5 clippy::no_effect,
6 clippy::map_identity,
7 clippy::unit_arg,
8 clippy::match_ref_pats,
9 clippy::redundant_pattern_matching,
10 clippy::for_loops_over_fallibles,
11 dead_code
12 )]
13
14 fn main() {
15 match Some(0) {
16 Some(_) => Some(2),
17 None::<u32> => None,
18 };
19
20 match Some(0) {
21 Some(x) => Some(x + 1),
22 _ => None,
23 };
24
25 match Some("") {
26 Some(x) => Some(x.is_empty()),
27 None => None,
28 };
29
30 if let Some(x) = Some(0) {
31 Some(!x)
32 } else {
33 None
34 };
35
36 #[rustfmt::skip]
37 match Some(0) {
38 Some(x) => { Some(std::convert::identity(x)) }
39 None => { None }
40 };
41
42 match Some(&String::new()) {
43 Some(x) => Some(str::len(x)),
44 None => None,
45 };
46
47 match Some(0) {
48 Some(x) if false => Some(x + 1),
49 _ => None,
50 };
51
52 match &Some([0, 1]) {
53 Some(x) => Some(x[0]),
54 &None => None,
55 };
56
57 match &Some(0) {
58 &Some(x) => Some(x * 2),
59 None => None,
60 };
61
62 match Some(String::new()) {
63 Some(ref x) => Some(x.is_empty()),
64 _ => None,
65 };
66
67 match &&Some(String::new()) {
68 Some(x) => Some(x.len()),
69 _ => None,
70 };
71
72 match &&Some(0) {
73 &&Some(x) => Some(x + x),
74 &&_ => None,
75 };
76
77 #[warn(clippy::option_map_unit_fn)]
78 match &mut Some(String::new()) {
79 Some(x) => Some(x.push_str("")),
80 None => None,
81 };
82
83 #[allow(clippy::option_map_unit_fn)]
84 {
85 match &mut Some(String::new()) {
86 Some(x) => Some(x.push_str("")),
87 None => None,
88 };
89 }
90
91 match &mut Some(String::new()) {
92 Some(ref x) => Some(x.len()),
93 None => None,
94 };
95
96 match &mut &Some(String::new()) {
97 Some(x) => Some(x.is_empty()),
98 &mut _ => None,
99 };
100
101 match Some((0, 1, 2)) {
102 Some((x, y, z)) => Some(x + y + z),
103 None => None,
104 };
105
106 match Some([1, 2, 3]) {
107 Some([first, ..]) => Some(first),
108 None => None,
109 };
110
111 match &Some((String::new(), "test")) {
112 Some((x, y)) => Some((y, x)),
113 None => None,
114 };
115
116 match Some((String::new(), 0)) {
117 Some((ref x, y)) => Some((y, x)),
118 None => None,
119 };
120
121 match Some(Some(0)) {
122 Some(Some(_)) | Some(None) => Some(0),
123 None => None,
124 };
125
126 match Some(Some((0, 1))) {
127 Some(Some((x, 1))) => Some(x),
128 _ => None,
129 };
130
131 // #6795
132 fn f1() -> Result<(), ()> {
133 let _ = match Some(Ok(())) {
134 Some(x) => Some(x?),
135 None => None,
136 };
137 Ok(())
138 }
139
140 for &x in Some(Some(true)).iter() {
141 let _ = match x {
142 Some(x) => Some(if x { continue } else { x }),
143 None => None,
144 };
145 }
146
147 // #6797
148 let x1 = (Some(String::new()), 0);
149 let x2 = x1.0;
150 match x2 {
151 Some(x) => Some((x, x1.1)),
152 None => None,
153 };
154
155 struct S1 {
156 x: Option<String>,
157 y: u32,
158 }
159 impl S1 {
160 fn f(self) -> Option<(String, u32)> {
161 match self.x {
162 Some(x) => Some((x, self.y)),
163 None => None,
164 }
165 }
166 }
167
168 // #6811
169 match Some(0) {
170 Some(x) => Some(vec![x]),
171 None => None,
172 };
173
174 match option_env!("") {
175 Some(x) => Some(String::from(x)),
176 None => None,
177 };
178
179 // #6819
180 async fn f2(x: u32) -> u32 {
181 x
182 }
183
184 async fn f3() {
185 match Some(0) {
186 Some(x) => Some(f2(x).await),
187 None => None,
188 };
189 }
190
191 // #6847
192 if let Some(_) = Some(0) {
193 Some(0)
194 } else if let Some(x) = Some(0) {
195 Some(x + 1)
196 } else {
197 None
198 };
199
200 if true {
201 Some(0)
202 } else if let Some(x) = Some(0) {
203 Some(x + 1)
204 } else {
205 None
206 };
207
208 // #6967
209 const fn f4() {
210 match Some(0) {
211 Some(x) => Some(x + 1),
212 None => None,
213 };
214 }
215
216 // #7077
217 let s = &String::new();
218 #[allow(clippy::needless_match)]
219 let _: Option<&str> = match Some(s) {
220 Some(s) => Some(s),
221 None => None,
222 };
223 }