]> git.lizzy.rs Git - rust.git/blob - src/tools/rustfmt/tests/target/closure.rs
Merge commit 'd822110d3b5625b9dc80ccc442e06fc3cc851d76' into clippyup
[rust.git] / src / tools / rustfmt / tests / target / closure.rs
1 // rustfmt-normalize_comments: true
2 // Closures
3
4 fn main() {
5     let square = (|i: i32| i * i);
6
7     let commented = |// first
8                      a, // argument
9                      // second
10                      b: WithType, // argument
11                      // ignored
12                      _| {
13         (
14             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
15             bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
16         )
17     };
18
19     let block_body = move |xxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
20                            ref yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy| {
21         xxxxxxxxxxxxxxxxxxxxxxxxxxxxx + yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
22     };
23
24     let loooooooooooooong_name = |field| {
25         // format comments.
26         if field.node.attrs.len() > 0 {
27             field.node.attrs[0].span.lo()
28         } else {
29             field.span.lo()
30         }
31     };
32
33     let unblock_me = |trivial| closure();
34
35     let empty = |arg| {};
36
37     let simple = |arg| {
38         // comment formatting
39         foo(arg)
40     };
41
42     let test = || {
43         do_something();
44         do_something_else();
45     };
46
47     let arg_test =
48         |big_argument_name, test123| looooooooooooooooooong_function_naaaaaaaaaaaaaaaaame();
49
50     let arg_test =
51         |big_argument_name, test123| looooooooooooooooooong_function_naaaaaaaaaaaaaaaaame();
52
53     let simple_closure = move || -> () {};
54
55     let closure = |input: Ty| -> Option<String> { foo() };
56
57     let closure_with_return_type =
58         |aaaaaaaaaaaaaaaaaaaaaaarg1, aaaaaaaaaaaaaaaaaaaaaaarg2| -> Strong { "sup".to_owned() };
59
60     |arg1, arg2, _, _, arg3, arg4| {
61         let temp = arg4 + arg3;
62         arg2 * arg1 - temp
63     };
64
65     let block_body_with_comment = args.iter().map(|a| {
66         // Emitting only dep-info is possible only for final crate type, as
67         // as others may emit required metadata for dependent crate types
68         if a.starts_with("--emit") && is_final_crate_type && !self.workspace_mode {
69             "--emit=dep-info"
70         } else {
71             a
72         }
73     });
74
75     for<> || -> () {};
76     for<> || -> () {};
77     for<> || -> () {};
78
79     for<'a, 'b, 'c> |_: &'a (), _: &'b (), _: &'c ()| -> () {};
80 }
81
82 fn issue311() {
83     let func = |x| println!("{}", x);
84
85     (func)(0.0);
86 }
87
88 fn issue863() {
89     let closure = |x| match x {
90         0 => true,
91         _ => false,
92     } == true;
93 }
94
95 fn issue934() {
96     let hash: &Fn(&&Block) -> u64 = &|block| -> u64 {
97         let mut h = SpanlessHash::new(cx);
98         h.hash_block(block);
99         h.finish()
100     };
101
102     let hash: &Fn(&&Block) -> u64 = &|block| -> u64 {
103         let mut h = SpanlessHash::new(cx);
104         h.hash_block(block);
105         h.finish();
106     };
107 }
108
109 impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
110     pub fn eq_expr(&self, left: &Expr, right: &Expr) -> bool {
111         match (&left.node, &right.node) {
112             (&ExprBinary(l_op, ref ll, ref lr), &ExprBinary(r_op, ref rl, ref rr)) => {
113                 l_op.node == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
114                     || swap_binop(l_op.node, ll, lr).map_or(false, |(l_op, ll, lr)| {
115                         l_op == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
116                     })
117             }
118         }
119     }
120 }
121
122 fn foo() {
123     lifetimes_iter___map(|lasdfasfd| {
124         let hi = if l.bounds.is_empty() {
125             l.lifetime.span.hi()
126         };
127     });
128 }
129
130 fn issue1405() {
131     open_raw_fd(fd, b'r').and_then(|file| {
132         Capture::new_raw(None, |_, err| unsafe { raw::pcap_fopen_offline(file, err) })
133     });
134 }
135
136 fn issue1466() {
137     let vertex_buffer = frame.scope(|ctx| {
138         let buffer = ctx.create_host_visible_buffer::<VertexBuffer<Vertex>>(&vertices);
139         ctx.create_device_local_buffer(buffer)
140     });
141 }
142
143 fn issue470() {
144     {
145         {
146             {
147                 let explicit_arg_decls =
148                     explicit_arguments
149                         .into_iter()
150                         .enumerate()
151                         .map(|(index, (ty, pattern))| {
152                             let lvalue = Lvalue::Arg(index as u32);
153                             block = this.pattern(
154                                 block,
155                                 argument_extent,
156                                 hair::PatternRef::Hair(pattern),
157                                 &lvalue,
158                             );
159                             ArgDecl { ty: ty }
160                         });
161             }
162         }
163     }
164 }
165
166 // #1509
167 impl Foo {
168     pub fn bar(&self) {
169         Some(SomeType {
170             push_closure_out_to_100_chars: iter(otherwise_it_works_ok.into_iter().map(|f| Ok(f))),
171         })
172     }
173 }
174
175 fn issue1329() {
176     aaaaaaaaaaaaaaaa
177         .map(|x| {
178             x += 1;
179             x
180         })
181         .filter
182 }
183
184 fn issue325() {
185     let f =
186         || unsafe { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx };
187 }
188
189 fn issue1697() {
190     Test.func_a(
191         A_VERY_LONG_CONST_VARIABLE_NAME,
192         move |arg1, arg2, arg3, arg4| arg1 + arg2 + arg3 + arg4,
193     )
194 }
195
196 fn issue1694() {
197     foooooo(
198         |_referencefffffffff: _, _target_reference: _, _oid: _, _target_oid: _| {
199             format!("refs/pull/{}/merge", pr_id)
200         },
201     )
202 }
203
204 fn issue1713() {
205     rayon::join(
206         || recurse(left, is_less, pred, limit),
207         || recurse(right, is_less, Some(pivot), limit),
208     );
209
210     rayon::join(
211         1,
212         || recurse(left, is_less, pred, limit),
213         2,
214         || recurse(right, is_less, Some(pivot), limit),
215     );
216 }
217
218 fn issue2063() {
219     |ctx: Ctx<(String, String)>| -> io::Result<Response> {
220         Ok(Response::new().with_body(ctx.params.0))
221     }
222 }
223
224 fn issue1524() {
225     let f = |x| x;
226     let f = |x| x;
227     let f = |x| x;
228     let f = |x| x;
229     let f = |x| x;
230 }
231
232 fn issue2171() {
233     foo(|| unsafe {
234         if PERIPHERALS {
235             loop {}
236         } else {
237             PERIPHERALS = true;
238         }
239     })
240 }
241
242 fn issue2207() {
243     a.map(|_| {
244         unsafe { a_very_very_very_very_very_very_very_long_function_name_or_anything_else() }
245             .to_string()
246     })
247 }
248
249 fn issue2262() {
250     result
251         .init(&mut result.slave.borrow_mut(), &mut (result.strategy)())
252         .map_err(|factory| Error {
253             factory,
254             slave: None,
255         })?;
256 }