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