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