]> git.lizzy.rs Git - rust.git/blob - tests/source/closure.rs
Merge pull request #1701 from topecongiro/issue-1697
[rust.git] / 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
47 fn issue311() {
48     let func = |x| println!("{}", x);
49
50     (func)(0.0);
51 }
52
53 fn issue863() {
54     let closure = |x| match x {
55         0 => true,
56         _ => false,
57     } == true;
58 }
59
60 fn issue934() {
61     let hash: &Fn(&&Block) -> u64 = &|block| -> u64 {
62         let mut h = SpanlessHash::new(cx);
63         h.hash_block(block);
64         h.finish()
65     };
66
67     let hash: &Fn(&&Block) -> u64 = &|block| -> u64 {
68         let mut h = SpanlessHash::new(cx);
69         h.hash_block(block);
70         h.finish();
71     };
72 }
73
74 impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
75     pub fn eq_expr(&self, left: &Expr, right: &Expr) -> bool {
76         match (&left.node, &right.node) {
77             (&ExprBinary(l_op, ref ll, ref lr), &ExprBinary(r_op, ref rl, ref rr)) => {
78                 l_op.node == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr) ||
79                 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))
80             }
81         }
82     }
83 }
84
85 fn foo() {
86     lifetimes_iter___map(|lasdfasfd| {
87         let hi = if l.bounds.is_empty() {
88             l.lifetime.span.hi
89         };
90     });
91 }
92
93 fn issue1405() {
94     open_raw_fd(fd, b'r')
95         .and_then(|file| Capture::new_raw(None, |_, err| unsafe {
96             raw::pcap_fopen_offline(file, err)
97         }));
98 }
99
100 fn issue1466() {
101     let vertex_buffer = frame.scope(|ctx| {
102         let buffer =
103             ctx.create_host_visible_buffer::<VertexBuffer<Vertex>>(&vertices);
104         ctx.create_device_local_buffer(buffer)
105     });
106 }
107
108 fn issue470() {
109     {{{
110         let explicit_arg_decls =
111             explicit_arguments.into_iter()
112             .enumerate()
113             .map(|(index, (ty, pattern))| {
114                 let lvalue = Lvalue::Arg(index as u32);
115                 block = this.pattern(block,
116                                      argument_extent,
117                                      hair::PatternRef::Hair(pattern),
118                                      &lvalue);
119                 ArgDecl { ty: ty }
120             });
121     }}}
122 }
123
124 // #1509
125 impl Foo {
126     pub fn bar(&self) {
127         Some(SomeType {
128             push_closure_out_to_100_chars: iter(otherwise_it_works_ok.into_iter().map(|f| {
129                 Ok(f)
130             })),
131         })
132     }
133 }
134
135 fn issue1329() {
136     aaaaaaaaaaaaaaaa.map(|x| {
137         x += 1;
138         x
139     })
140         .filter
141 }
142
143 fn issue325() {
144     let f = || unsafe { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx };
145 }
146
147 fn issue1697() {
148     Test.func_a(A_VERY_LONG_CONST_VARIABLE_NAME, move |arg1, arg2, arg3, arg4| arg1 + arg2 + arg3 + arg4)
149 }
150
151 fn issue1694() {
152     foooooo(|_referencefffffffff: _, _target_reference: _, _oid: _, _target_oid: _| format!("refs/pull/{}/merge", pr_id))
153 }