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