]> git.lizzy.rs Git - rust.git/blob - src/tools/rustfmt/tests/source/closure.rs
Merge commit 'b7f3f7f6082679da2da9a0b3faf1b5adef3afd3b' into clippyup
[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
56 fn issue311() {
57     let func = |x| println!("{}", x);
58
59     (func)(0.0);
60 }
61
62 fn issue863() {
63     let closure = |x| match x {
64         0 => true,
65         _ => false,
66     } == true;
67 }
68
69 fn issue934() {
70     let hash: &Fn(&&Block) -> u64 = &|block| -> u64 {
71         let mut h = SpanlessHash::new(cx);
72         h.hash_block(block);
73         h.finish()
74     };
75
76     let hash: &Fn(&&Block) -> u64 = &|block| -> u64 {
77         let mut h = SpanlessHash::new(cx);
78         h.hash_block(block);
79         h.finish();
80     };
81 }
82
83 impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
84     pub fn eq_expr(&self, left: &Expr, right: &Expr) -> bool {
85         match (&left.node, &right.node) {
86             (&ExprBinary(l_op, ref ll, ref lr), &ExprBinary(r_op, ref rl, ref rr)) => {
87                 l_op.node == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr) ||
88                 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))
89             }
90         }
91     }
92 }
93
94 fn foo() {
95     lifetimes_iter___map(|lasdfasfd| {
96         let hi = if l.bounds.is_empty() {
97             l.lifetime.span.hi()
98         };
99     });
100 }
101
102 fn issue1405() {
103     open_raw_fd(fd, b'r')
104         .and_then(|file| Capture::new_raw(None, |_, err| unsafe {
105             raw::pcap_fopen_offline(file, err)
106         }));
107 }
108
109 fn issue1466() {
110     let vertex_buffer = frame.scope(|ctx| {
111         let buffer =
112             ctx.create_host_visible_buffer::<VertexBuffer<Vertex>>(&vertices);
113         ctx.create_device_local_buffer(buffer)
114     });
115 }
116
117 fn issue470() {
118     {{{
119         let explicit_arg_decls =
120             explicit_arguments.into_iter()
121             .enumerate()
122             .map(|(index, (ty, pattern))| {
123                 let lvalue = Lvalue::Arg(index as u32);
124                 block = this.pattern(block,
125                                      argument_extent,
126                                      hair::PatternRef::Hair(pattern),
127                                      &lvalue);
128                 ArgDecl { ty: ty }
129             });
130     }}}
131 }
132
133 // #1509
134 impl Foo {
135     pub fn bar(&self) {
136         Some(SomeType {
137             push_closure_out_to_100_chars: iter(otherwise_it_works_ok.into_iter().map(|f| {
138                 Ok(f)
139             })),
140         })
141     }
142 }
143
144 fn issue1329() {
145     aaaaaaaaaaaaaaaa.map(|x| {
146         x += 1;
147         x
148     })
149         .filter
150 }
151
152 fn issue325() {
153     let f = || unsafe { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx };
154 }
155
156 fn issue1697() {
157     Test.func_a(A_VERY_LONG_CONST_VARIABLE_NAME, move |arg1, arg2, arg3, arg4| arg1 + arg2 + arg3 + arg4)
158 }
159
160 fn issue1694() {
161     foooooo(|_referencefffffffff: _, _target_reference: _, _oid: _, _target_oid: _| format!("refs/pull/{}/merge", pr_id))
162 }
163
164 fn issue1713() {
165     rayon::join(
166         || recurse(left, is_less, pred, limit),
167         || recurse(right, is_less, Some(pivot), limit),
168     );
169
170     rayon::join(
171         1,
172         || recurse(left, is_less, pred, limit),
173         2,
174         || recurse(right, is_less, Some(pivot), limit),
175     );
176 }
177
178 fn issue2063() {
179     |ctx: Ctx<(String, String)>| -> io::Result<Response> {
180         Ok(Response::new().with_body(ctx.params.0))
181     }
182 }
183
184 fn issue1524() {
185     let f = |x| {{{{x}}}};
186     let f = |x| {{{x}}};
187     let f = |x| {{x}};
188     let f = |x| {x};
189     let f = |x| x;
190 }
191
192 fn issue2171() {
193     foo(|| unsafe {
194         if PERIPHERALS {
195             loop {}
196         } else {
197             PERIPHERALS = true;
198         }
199     })
200 }
201
202 fn issue2207() {
203     a.map(|_| unsafe {
204         a_very_very_very_very_very_very_very_long_function_name_or_anything_else()
205     }.to_string())
206 }
207
208 fn issue2262() {
209     result.init(&mut result.slave.borrow_mut(), &mut (result.strategy)()).map_err(|factory| Error {
210         factory,
211         slave: None,
212     })?;
213 }