]> git.lizzy.rs Git - rust.git/blob - tests/target/chains.rs
Add a test for #2922
[rust.git] / tests / target / chains.rs
1 // rustfmt-use_small_heuristics: Off
2 // Test chain formatting.
3
4 fn main() {
5     let a = b.c.d.1.foo(|x| x + 1);
6
7     bbbbbbbbbbbbbbbbbbb.ccccccccccccccccccccccccccccccccccccc.ddddddddddddddddddddddddddd();
8
9     bbbbbbbbbbbbbbbbbbb
10         .ccccccccccccccccccccccccccccccccccccc
11         .ddddddddddddddddddddddddddd
12         .eeeeeeee();
13
14     let f = fooooooooooooooooooooooooooooooooooooooooooooooooooo
15         .baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaar;
16
17     // Test case where first chain element isn't a path, but is shorter than
18     // the size of a tab.
19     x().y(|| match cond() {
20         true => (),
21         false => (),
22     });
23
24     loong_func().quux(move || {
25         if true {
26             1
27         } else {
28             2
29         }
30     });
31
32     some_fuuuuuuuuunction().method_call_a(aaaaa, bbbbb, |c| {
33         let x = c;
34         x
35     });
36
37     some_fuuuuuuuuunction()
38         .method_call_a(aaaaa, bbbbb, |c| {
39             let x = c;
40             x
41         }).method_call_b(aaaaa, bbbbb, |c| {
42             let x = c;
43             x
44         });
45
46     fffffffffffffffffffffffffffffffffff(a, {
47         SCRIPT_TASK_ROOT.with(|root| {
48             *root.borrow_mut() = Some(&script_task);
49         });
50     });
51
52     let suuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuum =
53         xxxxxxx.map(|x| x + 5).map(|x| x / 2).fold(0, |acc, x| acc + x);
54
55     body.fold(Body::new(), |mut body, chunk| {
56         body.extend(chunk);
57         Ok(body)
58     }).and_then(move |body| {
59         let req = Request::from_parts(parts, body);
60         f(req).map_err(|_| io::Error::new(io::ErrorKind::Other, ""))
61     });
62
63     aaaaaaaaaaaaaaaa
64         .map(|x| {
65             x += 1;
66             x
67         }).filter(some_mod::some_filter)
68 }
69
70 fn floaters() {
71     let z = Foo {
72         field1: val1,
73         field2: val2,
74     };
75
76     let x = Foo {
77         field1: val1,
78         field2: val2,
79     }.method_call()
80     .method_call();
81
82     let y = if cond {
83         val1
84     } else {
85         val2
86     }.method_call();
87
88     {
89         match x {
90             PushParam => {
91                 // params are 1-indexed
92                 stack.push(
93                     mparams[match cur.to_digit(10) {
94                                 Some(d) => d as usize - 1,
95                                 None => return Err("bad param number".to_owned()),
96                             }].clone(),
97                 );
98             }
99         }
100     }
101
102     if cond {
103         some();
104     } else {
105         none();
106     }.bar()
107     .baz();
108
109     Foo {
110         x: val,
111     }.baz(|| {
112         force();
113         multiline();
114     }).quux();
115
116     Foo {
117         y: i_am_multi_line,
118         z: ok,
119     }.baz(|| {
120         force();
121         multiline();
122     }).quux();
123
124     a + match x {
125         true => "yay!",
126         false => "boo!",
127     }.bar()
128 }
129
130 fn is_replaced_content() -> bool {
131     constellat.send(ConstellationMsg::ViewportConstrained(self.id, constraints)).unwrap();
132 }
133
134 fn issue587() {
135     a.b::<()>(c);
136
137     std::mem::transmute(dl.symbol::<()>("init").unwrap())
138 }
139
140 fn try_shorthand() {
141     let x = expr?;
142     let y = expr.kaas()?.test();
143     let loooooooooooooooooooooooooooooooooooooooooong =
144         does_this?.look?.good?.should_we_break?.after_the_first_question_mark?;
145     let yyyy = expr?.another?.another?.another?.another?.another?.another?.another?.another?.test();
146     let zzzz = expr?.another?.another?.another?.another?;
147     let aaa = x??????????????????????????????????????????????????????????????????????????;
148
149     let y = a
150         .very
151         .loooooooooooooooooooooooooooooooooooooong()
152         .chain()
153         .inside()
154         .weeeeeeeeeeeeeee()?
155         .test()
156         .0
157         .x;
158
159     parameterized(f, substs, def_id, Ns::Value, &[], |tcx| tcx.lookup_item_type(def_id).generics)?;
160     fooooooooooooooooooooooooooo()?
161         .bar()?
162         .baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz()?;
163 }
164
165 fn issue_1004() {
166     match *self {
167         ty::ImplOrTraitItem::MethodTraitItem(ref i) => write!(f, "{:?}", i),
168         ty::ImplOrTraitItem::ConstTraitItem(ref i) => write!(f, "{:?}", i),
169         ty::ImplOrTraitItem::TypeTraitItem(ref i) => write!(f, "{:?}", i),
170     }?;
171
172     ty::tls::with(|tcx| {
173         let tap = ty::Binder(TraitAndProjections(principal, projections));
174         in_binder(f, tcx, &ty::Binder(""), Some(tap))
175     })?;
176 }
177
178 fn issue1392() {
179     test_method(
180         r#"
181         if foo {
182             a();
183         }
184         else {
185             b();
186         }
187         "#.trim(),
188     );
189 }
190
191 // #2067
192 impl Settings {
193     fn save(&self) -> Result<()> {
194         let mut file = File::create(&settings_path)
195             .chain_err(|| ErrorKind::WriteError(settings_path.clone()))?;
196     }
197 }
198
199 fn issue2126() {
200     {
201         {
202             {
203                 {
204                     {
205                         let x = self
206                             .span_from(sub_span.expect("No span found for struct arant variant"));
207                         self.sspanpan_from_span(
208                             sub_span.expect("No span found for struct variant"),
209                         );
210                         let x = self.spanpan_from_span(
211                             sub_span.expect("No span found for struct variant"),
212                         )?;
213                     }
214                 }
215             }
216         }
217     }
218 }
219
220 // #2200
221 impl Foo {
222     pub fn from_ast(diagnostic: &::errors::Handler, attrs: &[ast::Attribute]) -> Attributes {
223         let other_attrs = attrs
224             .iter()
225             .filter_map(|attr| {
226                 attr.with_desugared_doc(|attr| {
227                     if attr.check_name("doc") {
228                         if let Some(mi) = attr.meta() {
229                             if let Some(value) = mi.value_str() {
230                                 doc_strings.push(DocFragment::Include(
231                                     line, attr.span, filename, contents,
232                                 ));
233                             }
234                         }
235                     }
236                 })
237             }).collect();
238     }
239 }
240
241 // #2415
242 // Avoid orphan in chain
243 fn issue2415() {
244     let base_url = (|| {
245         // stuff
246
247         Ok((|| {
248             // stuff
249             Some(value.to_string())
250         })().ok_or("")?)
251     })().unwrap_or_else(|_: Box<::std::error::Error>| String::from(""));
252 }
253
254 impl issue_2786 {
255     fn thing(&self) {
256         foo(|a| {
257             println!("a");
258             println!("b");
259         }).bar(|c| {
260             println!("a");
261             println!("b");
262         }).baz(|c| {
263             println!("a");
264             println!("b");
265         })
266     }
267 }
268
269 fn issue_2773() {
270     let bar = Some(0);
271     bar.or_else(|| {
272         // do stuff
273         None
274     }).or_else(|| {
275         // do other stuff
276         None
277     }).and_then(|val| {
278         // do this stuff
279         None
280     });
281 }