├── Cargo.toml ├── README.md ├── src ├── ast.rs ├── ft.rs ├── ir.rs ├── main.rs ├── rfmt.rs ├── tr.rs └── ts.rs └── tests ├── comments ├── attr.rs ├── block.rs ├── doc.rs ├── enum.rs ├── extern_crate.rs ├── fn.rs ├── for.rs ├── foreign.rs ├── impl.rs ├── macro.rs ├── mod.rs ├── mod_decl.rs ├── patten.rs ├── struct.rs ├── struct_expr.rs ├── struct_patten.rs ├── sugared_doc.rs ├── trait.rs ├── try_block.rs ├── union.rs └── use.rs ├── examples ├── a.rs ├── b.rs ├── c.rs ├── comment.rs ├── d.rs ├── e.rs ├── f.rs ├── g.rs └── main.rs ├── ft ├── attrs │ ├── attrs.rs │ ├── doc_with_trailing_comment.rs │ └── nl.rs ├── block.rs ├── c │ ├── d.rs │ ├── e.rs │ └── g │ │ └── mod.rs ├── comments │ ├── comments.rs │ ├── leading_comments.rs │ └── left_comments.rs ├── const.rs ├── decl.rs ├── enum.rs ├── exprs │ ├── array.rs │ ├── async.rs │ ├── block.rs │ ├── box.rs │ ├── break.rs │ ├── cast.rs │ ├── closure.rs │ ├── continue.rs │ ├── expr.rs │ ├── field.rs │ ├── fn_call.rs │ ├── for.rs │ ├── if.rs │ ├── if_let.rs │ ├── index.rs │ ├── list_op.rs │ ├── literal.rs │ ├── loop.rs │ ├── macro.rs │ ├── match.rs │ ├── method_call.rs │ ├── path.rs │ ├── range.rs │ ├── ref.rs │ ├── repeat.rs │ ├── return.rs │ ├── struct.rs │ ├── try.rs │ ├── try_block.rs │ ├── tuple.rs │ ├── type.rs │ ├── unary_op.rs │ ├── while.rs │ └── while_let.rs ├── fn.rs ├── foreign.rs ├── generics.rs ├── impl.rs ├── impl_default.rs ├── items │ ├── const.rs │ ├── enum.rs │ ├── existential.rs │ ├── extern_crate.rs │ ├── fn.rs │ ├── foreign.rs │ ├── group_items.rs │ ├── impl.rs │ ├── in_mod.rs │ ├── mod_decl.rs │ ├── mod_nest.rs │ ├── static.rs │ ├── struct.rs │ ├── trait.rs │ ├── trait_alias.rs │ ├── type_alias.rs │ ├── union.rs │ └── use.rs ├── macro.rs ├── match.rs ├── mod_nest.rs ├── pattens │ ├── enum.rs │ ├── ident.rs │ ├── literal.rs │ ├── macro.rs │ ├── paren.rs │ ├── path.rs │ ├── patten.rs │ ├── range.rs │ ├── ref.rs │ ├── slice.rs │ ├── struct.rs │ ├── tuple.rs │ └── wildcard.rs ├── static.rs ├── stmt.rs ├── struct.rs ├── trait.rs ├── ts │ └── blank_lines.rs └── types │ ├── array.rs │ ├── bare_fn.rs │ ├── impl_trait.rs │ ├── macro.rs │ ├── path.rs │ ├── ptr.rs │ ├── ref.rs │ ├── slice.rs │ ├── symbol.rs │ ├── trait_object.rs │ ├── tuple.rs │ └── type_alias.rs ├── ir ├── attrs │ ├── attr.rs │ ├── derive.rs │ ├── doc.rs │ ├── doc_comment.rs │ ├── inner.rs │ ├── meta.rs │ ├── meta_list.rs │ ├── meta_list_idents.rs │ ├── meta_list_name_value_str.rs │ ├── meta_list_paths.rs │ ├── meta_name_value_str.rs │ ├── meta_path.rs │ ├── meta_word.rs │ ├── outer.rs │ ├── sugared_doc.rs │ └── tool_attr.rs ├── comments │ └── comment.rs ├── exprs │ ├── array.rs │ ├── async.rs │ ├── block.rs │ ├── box.rs │ ├── break.rs │ ├── cast.rs │ ├── closure.rs │ ├── continue.rs │ ├── expr.rs │ ├── field.rs │ ├── fn_call.rs │ ├── for.rs │ ├── if.rs │ ├── if_let.rs │ ├── index.rs │ ├── list_op.rs │ ├── literal.rs │ ├── loop.rs │ ├── macro.rs │ ├── match.rs │ ├── method_call.rs │ ├── path.rs │ ├── range.rs │ ├── ref.rs │ ├── repeat.rs │ ├── return.rs │ ├── struct.rs │ ├── try.rs │ ├── try_block.rs │ ├── tuple.rs │ ├── type.rs │ ├── unary_op.rs │ ├── while.rs │ └── while_let.rs ├── generics │ ├── const-generics.rs │ ├── generics.rs │ └── where.rs ├── items │ ├── a.rs │ ├── asm.rs │ ├── b.rs │ ├── block.rs │ ├── const.rs │ ├── enum.rs │ ├── existential.rs │ ├── extern_crate.rs │ ├── fn.rs │ ├── foreign.rs │ ├── impl.rs │ ├── lifetime.rs │ ├── literal.rs │ ├── local.rs │ ├── mod.rs │ ├── mod_decl.rs │ ├── only_comment.rs │ ├── static.rs │ ├── stmt.rs │ ├── struct.rs │ ├── trait.rs │ ├── trait_alias.rs │ ├── union.rs │ ├── use.rs │ ├── vis.rs │ ├── wrap.rs │ └── wrap_fn_arg.rs ├── macro │ ├── def.rs │ ├── invocation.rs │ └── macro.rs ├── pattens │ ├── enum.rs │ ├── ident.rs │ ├── literal.rs │ ├── macro.rs │ ├── paren.rs │ ├── path.rs │ ├── patten.rs │ ├── range.rs │ ├── ref.rs │ ├── slice.rs │ ├── struct.rs │ ├── tuple.rs │ └── wildcard.rs └── types │ ├── array.rs │ ├── bare_fn.rs │ ├── impl_trait.rs │ ├── infer.rs │ ├── macro.rs │ ├── never.rs │ ├── path.rs │ ├── ptr.rs │ ├── ref.rs │ ├── slice.rs │ ├── trait_object.rs │ ├── tuple.rs │ └── type_alias.rs ├── issues ├── blank_line.rs ├── block_one_line.rs ├── closure.rs ├── commments │ ├── stmt_comments.rs │ ├── struct_comments.rs │ ├── trailing_commments_0.rs │ └── trailing_commments_1.rs ├── doc.rs ├── extern_crate_use_mod_decl_in_item.rs ├── fn_one_line.rs ├── if_let_one_line.rs ├── if_let_wrap.rs ├── impl_method_one_line.rs ├── macro_sep.rs ├── match.rs ├── match_wrap.rs ├── meta_item_long_str.rs ├── method_self.rs ├── method_self_value_immut.rs ├── mod_nl.rs ├── nl.rs ├── nl_after_sep.rs ├── path_type_from_expr.rs ├── return_trait.rs ├── str_litera_exceed_max_width.rs ├── str_literal.rs ├── struct_patten.rs ├── trait_method.rs ├── trait_method_one_line.rs └── use_nl.rs └── rustfmt ├── 3156.rs ├── 3167.rs ├── 3191.rs ├── 3192.rs ├── 3245.rs ├── 3251.rs ├── 3286.rs ├── 3419.rs ├── 3502.rs ├── 3551.rs ├── 3560.rs ├── 3561.rs ├── 3591.rs ├── 3599.rs ├── 3617.rs ├── 3634.rs ├── 3638.rs ├── 3701.rs ├── 3716.rs ├── 3720.rs ├── 3736.rs ├── 3741.rs ├── 3762.rs ├── 566.rs ├── 626.rs ├── 810.rs └── 915.rs /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rfmt" 3 | version = "1.38.0" 4 | authors = ["baitu "] 5 | homepage = "https://github.com/zBaitu/rfmt" 6 | repository = "https://github.com/zBaitu/rfmt" 7 | keywords = ["formatter"] 8 | readme = "README.md" 9 | license = "Apache-2.0/MIT" 10 | description = """ 11 | Another Rust source code formatter. 12 | """ 13 | edition = "2018" 14 | 15 | [[bin]] 16 | name = "rfmt" 17 | 18 | [dependencies] 19 | structopt = "0.2" 20 | rustc-ap-syntax = "542.0.0" 21 | rustc-ap-syntax_pos = "542.0.0" 22 | rustc-ap-rustc_target = "542.0.0" 23 | walkdir = "2" 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rFmt ---- Rust source code formatter 2 | 3 | Has moved to [rsfmt](https://github.com/zBaitu/rsfmt) 4 | (maybe rfmt, r is not a good name) 5 | 6 | https://github.com/zBaitu/rfmt 7 | 8 | 9 | # Overview 10 | rfmt is a Rust source code formatter. Yes, there is already an official tool [rustfmt](https://github.com/rust-lang-nursery/rustfmt) from [Rust Nursery](https://github.com/rust-lang-nursery). So why write another one? 11 | * rustfmt is great for configurable, but there are still some style that i don't like in my personal taste. 12 | * Write a code formatter for Rust can make me learn Rust more deeply, for example, the AST of Rust. 13 | * For fun: ) 14 | 15 | 16 | # Version 17 | Support for [Rust 1.36.0](https://blog.rust-lang.org/2019/07/04/Rust-1.36.0.html) 18 | 19 | 20 | # Install, Build 21 | * Install 22 | 23 | ``` 24 | cargo install rfmt 25 | ``` 26 | * Build 27 | ``` 28 | git clone git@github.com:zBaitu/rfmt.git 29 | cargo build --release 30 | ``` 31 | 32 | 33 | # Usage 34 | ``` 35 | rfmt 1.36.0 36 | baitu 37 | Another Rust source code formatter. 38 | 39 | USAGE: 40 | rfmt [FLAGS] [input] 41 | 42 | FLAGS: 43 | -a, --ast Print the rust original syntax ast debug info 44 | -c, --check Check exceed lines and trailing white space lines 45 | -d, --debug Print the rfmt ir debug info 46 | -h, --help Prints help information 47 | -o, --overwrite Overwrite the source file 48 | -p, --print Print the rfmt ir simple format 49 | -V, --version Prints version information 50 | 51 | ARGS: 52 | Input file or dir. If `input` is a dir, rfmt will do action for all files in this dir recursively. If 53 | neither `options` nor `input` is specified, rfmt will format source code from stdin. 54 | ``` 55 | 56 | 57 | # Running rfmt from your editor(Copy from rustfmt) 58 | * [Vim](http://johannh.me/blog/rustfmt-vim.html) 59 | * [Emacs](https://github.com/fbergroth/emacs-rustfmt) 60 | * [Sublime Text 3](https://packagecontrol.io/packages/BeautifyRust) 61 | * [Atom](atom.md) 62 | * Visual Studio Code using [RustyCode](https://github.com/saviorisdead/RustyCode) or [vsc-rustfmt](https://github.com/Connorcpu/vsc-rustfmt) 63 | 64 | In fact, I only use rfmt for Vim now. I do not test for other editors. It is just to replace `rustfmt` to `rfmt`. For example, Vim: 65 | ``` 66 | let g:formatdef_rfmt = '"rfmt"' 67 | let g:formatters_rust = ['rfmt'] 68 | ``` 69 | 70 | 71 | # Features 72 | Comparing to **rustfmt**, there are some main different features from **rfmt**: 73 | * Keep wrap from user input. 74 | * Different align strategy. 75 | * Group `crate`, `use`, `mod`, `attributes` and sort them. 76 | * **DO NOT** format `doc`, `comment`, `string`. You can use the **check** function to show exceed lines and trailing white space lines. 77 | * Provide check, directory recursively, ast dump, debug. 78 | * Nightly features. 79 | 80 | The following part will show such features in detail, with some existing issues from rustfmt. 81 | 82 | ### Keep wrap from user input 83 | For the issue: [rustfmt reformats bit manipiulations](https://github.com/rust-lang-nursery/rustfmt/issues/626). 84 | ``` 85 | fn main() { 86 | let (a, b, c, d) = (0, 0, 0, 0); 87 | let _ = u32::from_be(((a as u32) << 24) | 88 | ((b as u32) << 16) | 89 | ((c as u32) << 8) | 90 | (d as u32) << 0); 91 | } 92 | ``` 93 | * rustfmt 94 | ``` 95 | fn main() { 96 | let (a, b, c, d) = (0, 0, 0, 0); 97 | let _ = 98 | u32::from_be(((a as u32) << 24) | ((b as u32) << 16) | ((c as u32) << 8) | (d as u32) << 0); 99 | } 100 | ``` 101 | Of cause you can use `#[rustfmt_skip]` to avoid such code, but in my personal opinon, I really don't like to add other code just for the source formatting tool. 102 | * rfmt 103 | ``` 104 | fn main() { 105 | let (a, b, c, d) = (0, 0, 0, 0); 106 | let _ = u32::from_be(((a as u32) << 24) | 107 | ((b as u32) << 16) | 108 | ((c as u32) << 8) | 109 | (d as u32) << 0); 110 | } 111 | ``` 112 | It looks OK, isn't it? Why rfmt can keep the user wrap? Because of the [rfmt ir](https://github.com/zBaitu/rFmt/blob/master/src/ir.rs). The custom ir of Rust AST record location information of every element as far as possible. Look another example: 113 | ``` 114 | fn main() { 115 | let ref_packet = [0xde, 0xf0, 0x12, 0x34, 0x45, 0x67, 116 | 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 117 | 0x86, 0xdd]; 118 | } 119 | ``` 120 | * rustfmt 121 | ``` 122 | fn main() { 123 | let ref_packet = [ 124 | 0xde, 0xf0, 0x12, 0x34, 0x45, 0x67, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0x86, 0xdd, 125 | ]; 126 | } 127 | ``` 128 | * rfmt 129 | ``` 130 | fn main() { 131 | let ref_packet = [0xde, 0xf0, 0x12, 0x34, 0x45, 0x67, 132 | 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 133 | 0x86, 0xdd]; 134 | } 135 | ``` 136 | 137 | ### Different align strategy 138 | I prefer to put parameters on one line as much as possible. 139 | ``` 140 | fn main() { 141 | f(123456789, "abcdefg", "hijklmn", 0987654321, "opqrst", "uvwxyz", 123456789, "abcdefg", "hijklmn", 0987654321, "opqrst", "uvwxyz"); 142 | } 143 | ``` 144 | * rustfmt 145 | ``` 146 | fn main() { 147 | f( 148 | 123456789, "abcdefg", "hijklmn", 0987654321, "opqrst", "uvwxyz", 123456789, "abcdefg", 149 | "hijklmn", 0987654321, "opqrst", "uvwxyz", 150 | ); 151 | } 152 | ``` 153 | * rfmt 154 | ``` 155 | fn main() { 156 | f(123456789, "abcdefg", "hijklmn", 0987654321, "opqrst", "uvwxyz", 123456789, "abcdefg", 157 | "hijklmn", 0987654321, "opqrst", "uvwxyz"); 158 | } 159 | ``` 160 | 161 | If the left align position is beyond limit(It is **40** for now), rfmt prefer double indent align to function call align. rfmt make source code left lean, while rustfmt is right lean, I think. An exsiting issue: [rustfmt should avoid rightwards drifting big blocks of code](https://github.com/rust-lang-nursery/rustfmt/issues/439) 162 | ```` 163 | fn main() { 164 | let mut arms = variants.iter().enumerate().map(|(i, &(ident, v_span, ref summary))| { 165 | let i_expr = cx.expr_usize(v_span, i); 166 | let pat = cx.pat_lit(v_span, i_expr); 167 | 168 | let path = cx.path(v_span, vec![substr.type_ident, ident]); 169 | let thing = rand_thing(cx, v_span, path, summary, |cx, sp| rand_call(cx, sp)); 170 | cx.arm(v_span, vec![ pat ], thing) 171 | }).collect:: >(); 172 | } 173 | ```` 174 | * rustfmt 175 | ``` 176 | fn main() { 177 | let mut arms = variants 178 | .iter() 179 | .enumerate() 180 | .map(|(i, &(ident, v_span, ref summary))| { 181 | let i_expr = cx.expr_usize(v_span, i); 182 | let pat = cx.pat_lit(v_span, i_expr); 183 | 184 | let path = cx.path(v_span, vec![substr.type_ident, ident]); 185 | let thing = rand_thing(cx, v_span, path, summary, |cx, sp| rand_call(cx, sp)); 186 | cx.arm(v_span, vec![pat], thing) 187 | }) 188 | .collect::>(); 189 | } 190 | ``` 191 | * rfmt 192 | ``` 193 | fn main() { 194 | let mut arms = variants.iter().enumerate().map(|(i, &(ident, v_span, ref summary))| { 195 | let i_expr = cx.expr_usize(v_span, i); 196 | let pat = cx.pat_lit(v_span, i_expr); 197 | let path = cx.path(v_span, vec![substr.type_ident, ident]); 198 | let thing = rand_thing(cx, v_span, path, summary, |cx, sp| rand_call(cx, sp)); 199 | cx.arm(v_span, vec![pat], thing) 200 | }).collect::>(); 201 | } 202 | ``` 203 | The result from rfmt is not changed, because this source code fits rfmt's code style. 204 | 205 | ### Group `crate`, `use`, `mod`, `attributes` and sort them 206 | ``` 207 | #![feature(custom_derive)] 208 | #![deny(warnings)] 209 | #![feature(question_mark)] 210 | #![feature(iter_arith)] 211 | #![feature(rustc_private)] 212 | 213 | extern crate rst; 214 | extern crate getopts; 215 | extern crate walkdir; 216 | 217 | use std::env; 218 | use getopts::Options; 219 | 220 | #[macro_use] 221 | mod ts; 222 | 223 | mod ir; 224 | mod ft; 225 | mod tr; 226 | mod rfmt; 227 | ``` 228 | * rfmt 229 | ``` 230 | #![deny(warnings)] 231 | #![feature(custom_derive)] 232 | #![feature(iter_arith)] 233 | #![feature(question_mark)] 234 | #![feature(rustc_private)] 235 | 236 | extern crate getopts; 237 | extern crate rst; 238 | extern crate walkdir; 239 | 240 | use getopts::Options; 241 | use std::env; 242 | 243 | #[macro_use] 244 | mod ts; 245 | 246 | mod ft; 247 | mod ir; 248 | mod rfmt; 249 | mod tr; 250 | ``` 251 | rfmt only group items that appear continuously. If on item is special that it must keep its order, like the `mod ts;`, make it separate from others. 252 | 253 | ### **DO NOT** format `doc`, `comment`, `string` 254 | There are many issues about doc, comment, string, raw string from rustfmt. I think such element can leave free for user to write anything, any format they want. 255 | 256 | ### Provide check, directory recursively, ast dump 257 | If you want to check is there some line break the code style limit, rfmt provide check function. 258 | ``` 259 | // aaaaa 260 | // bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb 261 | fn main() { 262 | let a = r#"aaaaaaaaaaaaaaaaaaaaaaaaaaaa 263 | bbbbbbbbbbbbbbbbb"#; 264 | } 265 | ``` 266 | ``` 267 | rfmt -c g.rs 268 | 269 | "g.rs" 270 | trailing_ws_lines: {1, 4} 271 | 272 | ------------------------------------------------------------------------------------------------------------------------ 273 | ```` 274 | 275 | You can check or overwrite all files in a directory. 276 | ``` 277 | rfmt -c rust/src/libcore 278 | rfmt -o rust/src/libstd 279 | ``` 280 | 281 | Maybe you are interested to see the Rust AST of a source code. 282 | ``` 283 | // AST 284 | fn main() {} 285 | ``` 286 | ``` 287 | rfmt -a a.rs 288 | ``` 289 | ``` 290 | Crate { 291 | loc: Loc(7, 19, nl), 292 | attrs: [], 293 | module: Mod { 294 | loc: Loc(7, 19, nl), 295 | name: "main", 296 | items: [ 297 | Item { 298 | loc: Loc(7, 19, nl), 299 | attrs: [], 300 | vis: "", 301 | item: Fn( 302 | Fn { 303 | header: FnHeader { 304 | is_unsafe: false, 305 | is_async: false, 306 | is_const: false, 307 | abi: "\"Rust\"", 308 | }, 309 | name: "main", 310 | generics: Generics { 311 | lifetime_defs: [], 312 | type_params: [], 313 | wh: Where { 314 | clauses: [], 315 | }, 316 | }, 317 | sig: FnSig { 318 | args: [], 319 | ret: Return { 320 | nl: false, 321 | ret: None, 322 | }, 323 | }, 324 | block: Block { 325 | loc: Loc(17, 19), 326 | is_unsafe: false, 327 | stmts: [], 328 | }, 329 | }, 330 | ), 331 | }, 332 | ], 333 | }, 334 | } 335 | 336 | ------------------------------------------------------------------------------------------------------------------------ 337 | 338 | { 339 | 7: [ 340 | "// AST", 341 | ], 342 | } 343 | {} 344 | ``` 345 | 346 | 347 | # Drawbacks 348 | As rfmt is written as a personal tool(toy) for my daily develop, it lacks some common features now. 349 | * No config 350 | rustfmt provide lots of config option, but rfmt provide none. Code style is something like food, everyone has his taste. Although rustfmt has much configs now, there are still new config require open in issues. If majority part of rfmt's style suit your taste, you can clone and make some small modification, such as **LF**, **max width**, **indent**. 351 | * Only support for some kinds of comment 352 | Comment can appear anywhere in source code, is it difficult to support all kinds of comment, as comment info does not exists on AST node. On the other hand, I don't think some tricky comment is really need. The following source with comment, which comment disappeared means that it is not supported by rfmt now. 353 | ``` 354 | // aaaaa 355 | 356 | // bbbbb 357 | struct A { // ccccc-DISAPPEARED 358 | // ddddd 359 | a: bool, // eeeee 360 | b: i32, // ffff 361 | // ggggg 362 | } // hhhhh 363 | 364 | // iiiii 365 | fn f(a: bool, /* jjjjj-DISAPPEARED */ b: i32, /* kkkkk-DISAPPEARED */) -> bool { // lllll-DISAPPEARED 366 | // mmmmm 367 | const b: bool = false; // nnnnn 368 | let mut a = true; // ooooo 369 | a = false; // ppppp 370 | a!();// qqqqq 371 | a // rrrrr 372 | } // sssss 373 | // ttttt 374 | 375 | // uuuuu 376 | ``` 377 | * rfmt 378 | ``` 379 | // aaaaa 380 | 381 | // bbbbb 382 | struct A { 383 | // ddddd 384 | a: bool, // eeeee 385 | b: i32, // ffff 386 | // ggggg 387 | } // hhhhh 388 | 389 | // iiiii 390 | fn f(a: bool, b: i32) -> bool { 391 | // mmmmm 392 | const b: bool = false; // nnnnn 393 | let mut a = true; // ooooo 394 | a = false; // ppppp 395 | a!(); // qqqqq 396 | a // rrrrr 397 | } // sssss 398 | // ttttt 399 | 400 | // uuuuu 401 | ``` 402 | -------------------------------------------------------------------------------- /src/ast.rs: -------------------------------------------------------------------------------- 1 | pub use rustc_target::spec::abi::Abi; 2 | pub use syntax::ast::*; 3 | pub use syntax::parse; 4 | pub use syntax::parse::lexer::comments::*; 5 | pub use syntax::parse::token::{self, Token, TokenKind}; 6 | pub use syntax::ptr::*; 7 | pub use syntax::source_map::Spanned; 8 | pub use syntax::tokenstream::*; 9 | pub use syntax_pos::*; 10 | pub use syntax_pos::symbol::*; 11 | 12 | pub type TokenLit = token::Lit; 13 | 14 | -------------------------------------------------------------------------------- /src/ir.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::{self, Debug, Display}; 2 | 3 | pub type Pos = u32; 4 | 5 | #[derive(Clone, Copy, Default)] 6 | pub struct Loc { 7 | pub start: Pos, 8 | pub end: Pos, 9 | pub nl: bool, 10 | } 11 | 12 | impl Display for Loc { 13 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 14 | if self.nl { 15 | write!(f, "Loc({}, {}, nl)", self.start, self.end) 16 | } else { 17 | write!(f, "Loc({}, {})", self.start, self.end) 18 | } 19 | } 20 | } 21 | 22 | impl Debug for Loc { 23 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 24 | Display::fmt(self, f) 25 | } 26 | } 27 | 28 | #[derive(Debug, PartialEq)] 29 | pub enum CommentKind { 30 | Leading, 31 | Trailing, 32 | } 33 | 34 | #[derive(Debug)] 35 | pub struct Comment { 36 | pub pos: Pos, 37 | pub kind: CommentKind, 38 | pub lines: Vec, 39 | } 40 | 41 | #[derive(Debug, Default)] 42 | pub struct Chunk { 43 | pub loc: Loc, 44 | pub s: String, 45 | } 46 | 47 | impl Chunk { 48 | pub fn new(s: S) -> Chunk where S: Into { 49 | Chunk { 50 | loc: Default::default(), 51 | s: s.into(), 52 | } 53 | } 54 | } 55 | 56 | #[derive(Debug)] 57 | pub struct Crate { 58 | pub loc: Loc, 59 | pub attrs: Vec, 60 | pub module: Mod, 61 | } 62 | 63 | pub type Doc = Chunk; 64 | 65 | #[derive(Debug)] 66 | pub enum AttrKind { 67 | Doc(Doc), 68 | Attr(Attr), 69 | } 70 | 71 | #[derive(Debug)] 72 | pub struct Attr { 73 | pub loc: Loc, 74 | pub is_inner: bool, 75 | pub item: MetaItem, 76 | } 77 | 78 | #[derive(Debug)] 79 | pub struct MetaItem { 80 | pub loc: Loc, 81 | pub name: String, 82 | pub items: Option>, 83 | } 84 | 85 | #[derive(Debug)] 86 | pub struct Mod { 87 | pub loc: Loc, 88 | pub name: String, 89 | pub items: Vec, 90 | } 91 | 92 | #[derive(Debug)] 93 | pub struct Item { 94 | pub loc: Loc, 95 | pub attrs: Vec, 96 | pub vis: Vis, 97 | pub item: ItemKind, 98 | } 99 | 100 | pub type Vis = String; 101 | 102 | #[derive(Debug)] 103 | pub enum ItemKind { 104 | Mod(Mod), 105 | ModDecl(ModDecl), 106 | ExternCrate(ExternCrate), 107 | Use(Use), 108 | TypeAlias(TypeAlias), 109 | TraitAlias(TraitAlias), 110 | Existential(Existential), 111 | Const(Const), 112 | Static(Static), 113 | Struct(Struct), 114 | Union(Union), 115 | Enum(Enum), 116 | ForeignMod(ForeignMod), 117 | Fn(Fn), 118 | Trait(Trait), 119 | Impl(Impl), 120 | MacroDef(MacroDef), 121 | Macro(Macro), 122 | } 123 | 124 | #[derive(Debug)] 125 | pub struct ModDecl { 126 | pub name: String, 127 | } 128 | 129 | #[derive(Debug)] 130 | pub struct ExternCrate { 131 | pub name: String, 132 | } 133 | 134 | #[derive(Debug)] 135 | pub struct Use { 136 | pub path: String, 137 | pub trees: Option>, 138 | } 139 | 140 | #[derive(Debug)] 141 | pub struct UseTree { 142 | pub loc: Loc, 143 | pub path: String, 144 | pub trees: Option>, 145 | } 146 | 147 | #[derive(Debug)] 148 | pub struct TypeAlias { 149 | pub name: String, 150 | pub generics: Generics, 151 | pub ty: Type, 152 | } 153 | 154 | #[derive(Debug)] 155 | pub struct TraitAlias { 156 | pub name: String, 157 | pub generics: Generics, 158 | pub bounds: TypeParamBounds, 159 | } 160 | 161 | #[derive(Debug)] 162 | pub struct Existential { 163 | pub name: String, 164 | pub generics: Generics, 165 | pub bounds: TypeParamBounds, 166 | } 167 | 168 | #[derive(Debug)] 169 | pub struct Generics { 170 | pub lifetime_defs: Vec, 171 | pub type_params: Vec, 172 | pub wh: Where, 173 | } 174 | 175 | impl Generics { 176 | #[inline] 177 | pub fn is_empty(&self) -> bool { 178 | self.lifetime_defs.is_empty() && self.type_params.is_empty() 179 | } 180 | } 181 | 182 | #[derive(Debug)] 183 | pub struct LifetimeDef { 184 | pub loc: Loc, 185 | pub lifetime: Lifetime, 186 | pub bounds: Vec, 187 | } 188 | 189 | pub type Lifetime = Chunk; 190 | 191 | #[derive(Debug)] 192 | pub struct TypeParam { 193 | pub loc: Loc, 194 | pub name: String, 195 | pub bounds: TypeParamBounds, 196 | pub default: Option, 197 | } 198 | 199 | #[derive(Debug)] 200 | pub enum TypeParamBound { 201 | Lifetime(Lifetime), 202 | PolyTraitRef(PolyTraitRef), 203 | } 204 | 205 | #[derive(Debug)] 206 | pub struct TypeParamBounds(pub Vec); 207 | 208 | impl TypeParamBounds { 209 | #[inline] 210 | pub fn is_empty(&self) -> bool { 211 | self.0.is_empty() 212 | } 213 | } 214 | 215 | #[derive(Debug)] 216 | pub struct PolyTraitRef { 217 | pub loc: Loc, 218 | pub lifetime_defs: Vec, 219 | pub trait_ref: TraitRef, 220 | } 221 | 222 | impl PolyTraitRef { 223 | pub fn new_sized(loc: Loc) -> PolyTraitRef { 224 | PolyTraitRef { 225 | loc, 226 | lifetime_defs: Vec::new(), 227 | trait_ref: TraitRef::new_sized(loc), 228 | } 229 | } 230 | } 231 | 232 | pub type TraitRef = Path; 233 | 234 | #[derive(Debug)] 235 | pub struct Where { 236 | pub clauses: Vec, 237 | } 238 | 239 | impl Where { 240 | #[inline] 241 | pub fn is_empty(&self) -> bool { 242 | self.clauses.is_empty() 243 | } 244 | } 245 | 246 | #[derive(Debug)] 247 | pub struct WhereClause { 248 | pub loc: Loc, 249 | pub clause: WhereKind, 250 | } 251 | 252 | #[derive(Debug)] 253 | pub enum WhereKind { 254 | LifetimeDef(LifetimeDef), 255 | Bound(WhereBound), 256 | } 257 | 258 | #[derive(Debug)] 259 | pub struct WhereBound { 260 | pub lifetime_defs: Vec, 261 | pub ty: Type, 262 | pub bounds: TypeParamBounds, 263 | } 264 | 265 | #[derive(Debug)] 266 | pub struct Path { 267 | pub loc: Loc, 268 | pub segments: Vec, 269 | } 270 | 271 | impl Path { 272 | pub fn new_sized(loc: Loc) -> Path { 273 | Path { 274 | loc, 275 | segments: vec![PathSegment::new_sized()], 276 | } 277 | } 278 | } 279 | 280 | #[derive(Debug)] 281 | pub struct PathSegment { 282 | pub loc: Loc, 283 | pub name: String, 284 | pub param: PathParam, 285 | } 286 | 287 | impl PathSegment { 288 | pub fn new_sized() -> PathSegment { 289 | PathSegment { 290 | loc: Default::default(), 291 | name: "?Sized".to_string(), 292 | param: PathParam::Angle(Default::default()), 293 | } 294 | } 295 | 296 | #[inline] 297 | pub fn is_empty(&self) -> bool { 298 | match self.param { 299 | PathParam::Angle(ref param) => param.is_empty(), 300 | PathParam::Paren(ref param) => param.is_empty(), 301 | } 302 | } 303 | } 304 | 305 | #[derive(Debug)] 306 | pub enum PathParam { 307 | Angle(AngleParam), 308 | Paren(ParenParam), 309 | } 310 | 311 | #[derive(Debug, Default)] 312 | pub struct AngleParam { 313 | pub lifetimes: Vec, 314 | pub types: Vec, 315 | pub bindings: Vec, 316 | } 317 | 318 | impl AngleParam { 319 | #[inline] 320 | pub fn is_empty(&self) -> bool { 321 | self.lifetimes.is_empty() && self.types.is_empty() && self.bindings.is_empty() 322 | } 323 | } 324 | 325 | #[derive(Debug)] 326 | pub enum TypeBindingKind { 327 | Eq(Type), 328 | Bound(TypeParamBounds), 329 | } 330 | 331 | #[derive(Debug)] 332 | pub struct TypeBinding { 333 | pub loc: Loc, 334 | pub name: String, 335 | pub binding: TypeBindingKind, 336 | } 337 | 338 | #[derive(Debug)] 339 | pub struct ParenParam { 340 | pub loc: Loc, 341 | pub inputs: Vec, 342 | pub output: Option, 343 | } 344 | 345 | impl ParenParam { 346 | #[inline] 347 | pub fn is_empty(&self) -> bool { 348 | self.inputs.is_empty() 349 | } 350 | } 351 | 352 | #[derive(Debug)] 353 | pub struct QSelf { 354 | pub ty: Type, 355 | pub pos: usize, 356 | } 357 | 358 | #[derive(Debug)] 359 | pub struct Type { 360 | pub loc: Loc, 361 | pub ty: TypeKind, 362 | } 363 | 364 | #[derive(Debug)] 365 | pub enum TypeKind { 366 | Symbol(&'static str), 367 | Path(Box), 368 | Ptr(Box), 369 | Ref(Box), 370 | Tuple(Box), 371 | Slice(Box), 372 | Array(Box), 373 | Trait(Box), 374 | BareFn(Box), 375 | Macro(Macro), 376 | } 377 | 378 | #[derive(Debug)] 379 | pub struct PathType { 380 | pub qself: Option, 381 | pub path: Path, 382 | } 383 | 384 | #[derive(Debug)] 385 | pub struct PtrType { 386 | pub is_mut: bool, 387 | pub ty: Type, 388 | } 389 | 390 | #[derive(Debug)] 391 | pub struct RefType { 392 | pub lifetime: Option, 393 | pub is_mut: bool, 394 | pub ty: Type, 395 | } 396 | 397 | #[derive(Debug)] 398 | pub struct TupleType { 399 | pub types: Vec, 400 | } 401 | 402 | #[derive(Debug)] 403 | pub struct SliceType { 404 | pub ty: Type, 405 | } 406 | 407 | #[derive(Debug)] 408 | pub struct ArrayType { 409 | pub ty: Type, 410 | pub expr: Expr, 411 | } 412 | 413 | #[derive(Debug)] 414 | pub struct TraitType { 415 | pub is_dyn: bool, 416 | pub is_impl: bool, 417 | pub bounds: TypeParamBounds, 418 | } 419 | 420 | #[derive(Debug)] 421 | pub struct BareFnType { 422 | pub lifetime_defs: Vec, 423 | pub header: FnHeader, 424 | pub sig: FnSig, 425 | } 426 | 427 | #[derive(Debug)] 428 | pub struct Const { 429 | pub name: String, 430 | pub ty: Type, 431 | pub expr: Expr, 432 | } 433 | 434 | #[derive(Debug)] 435 | pub struct Static { 436 | pub is_mut: bool, 437 | pub name: String, 438 | pub ty: Type, 439 | pub expr: Expr, 440 | } 441 | 442 | #[derive(Debug)] 443 | pub struct Struct { 444 | pub name: String, 445 | pub generics: Generics, 446 | pub body: StructBody, 447 | } 448 | 449 | #[derive(Debug)] 450 | pub enum StructBody { 451 | Struct(Vec), 452 | Tuple(Vec), 453 | Unit, 454 | } 455 | 456 | #[derive(Debug)] 457 | pub struct StructField { 458 | pub loc: Loc, 459 | pub attrs: Vec, 460 | pub vis: Vis, 461 | pub name: String, 462 | pub ty: Type, 463 | } 464 | 465 | #[derive(Debug)] 466 | pub struct TupleField { 467 | pub loc: Loc, 468 | pub attrs: Vec, 469 | pub vis: Vis, 470 | pub ty: Type, 471 | } 472 | 473 | #[derive(Debug)] 474 | pub struct Union { 475 | pub name: String, 476 | pub generics: Generics, 477 | pub fields: Vec, 478 | } 479 | 480 | #[derive(Debug)] 481 | pub struct Enum { 482 | pub name: String, 483 | pub generics: Generics, 484 | pub body: EnumBody, 485 | } 486 | 487 | #[derive(Debug)] 488 | pub struct EnumBody { 489 | pub fields: Vec, 490 | } 491 | 492 | #[derive(Debug)] 493 | pub struct EnumField { 494 | pub loc: Loc, 495 | pub attrs: Vec, 496 | pub name: String, 497 | pub body: StructBody, 498 | pub expr: Option, 499 | } 500 | 501 | #[derive(Debug)] 502 | pub struct FnSig { 503 | pub args: Vec, 504 | pub ret: Return, 505 | } 506 | 507 | #[derive(Debug)] 508 | pub struct Arg { 509 | pub loc: Loc, 510 | pub patten: Patten, 511 | pub ty: Type, 512 | pub has_patten: bool, 513 | } 514 | 515 | #[derive(Debug)] 516 | pub struct Return { 517 | pub nl: bool, 518 | pub ret: Option, 519 | } 520 | 521 | #[derive(Debug)] 522 | pub struct ForeignMod { 523 | pub abi: String, 524 | pub items: Vec, 525 | } 526 | 527 | #[derive(Debug)] 528 | pub struct ForeignItem { 529 | pub loc: Loc, 530 | pub attrs: Vec, 531 | pub vis: Vis, 532 | pub item: ForeignKind, 533 | } 534 | 535 | #[derive(Debug)] 536 | pub enum ForeignKind { 537 | Type(ForeignType), 538 | Static(ForeignStatic), 539 | Fn(ForeignFn), 540 | Macro(Macro), 541 | } 542 | 543 | pub type ForeignType = String; 544 | 545 | #[derive(Debug)] 546 | pub struct ForeignStatic { 547 | pub is_mut: bool, 548 | pub name: String, 549 | pub ty: Type, 550 | } 551 | 552 | #[derive(Debug)] 553 | pub struct ForeignFn { 554 | pub name: String, 555 | pub generics: Generics, 556 | pub sig: FnSig, 557 | } 558 | 559 | #[derive(Debug, Default)] 560 | pub struct FnHeader { 561 | pub is_unsafe: bool, 562 | pub is_async: bool, 563 | pub is_const: bool, 564 | pub abi: String, 565 | } 566 | 567 | #[derive(Debug)] 568 | pub struct Fn { 569 | pub header: FnHeader, 570 | pub name: String, 571 | pub generics: Generics, 572 | pub sig: FnSig, 573 | pub block: Block, 574 | } 575 | 576 | #[derive(Debug)] 577 | pub struct MethodSig { 578 | pub header: FnHeader, 579 | pub name: String, 580 | pub generics: Generics, 581 | pub sig: FnSig, 582 | } 583 | 584 | #[derive(Debug)] 585 | pub struct Trait { 586 | pub is_auto: bool, 587 | pub is_unsafe: bool, 588 | pub name: String, 589 | pub generics: Generics, 590 | pub bounds: TypeParamBounds, 591 | pub items: Vec, 592 | } 593 | 594 | #[derive(Debug)] 595 | pub struct TraitItem { 596 | pub loc: Loc, 597 | pub attrs: Vec, 598 | pub item: TraitItemKind, 599 | } 600 | 601 | #[derive(Debug)] 602 | pub enum TraitItemKind { 603 | Const(ConstTraitItem), 604 | Type(TypeTraitItem), 605 | Method(MethodTraitItem), 606 | Macro(Macro), 607 | } 608 | 609 | #[derive(Debug)] 610 | pub struct ConstTraitItem { 611 | pub name: String, 612 | pub ty: Type, 613 | pub expr: Option, 614 | } 615 | 616 | #[derive(Debug)] 617 | pub struct TypeTraitItem { 618 | pub name: String, 619 | pub generics: Generics, 620 | pub bounds: TypeParamBounds, 621 | pub ty: Option, 622 | } 623 | 624 | #[derive(Debug)] 625 | pub struct MethodTraitItem { 626 | pub sig: MethodSig, 627 | pub block: Option, 628 | } 629 | 630 | #[derive(Debug)] 631 | pub struct Impl { 632 | pub is_unsafe: bool, 633 | pub is_default: bool, 634 | pub is_neg: bool, 635 | pub generics: Generics, 636 | pub trait_ref: Option, 637 | pub ty: Type, 638 | pub items: Vec, 639 | } 640 | 641 | #[derive(Debug)] 642 | pub struct ImplItem { 643 | pub loc: Loc, 644 | pub attrs: Vec, 645 | pub vis: Vis, 646 | pub is_default: bool, 647 | pub item: ImplItemKind, 648 | } 649 | 650 | #[derive(Debug)] 651 | pub enum ImplItemKind { 652 | Const(ConstImplItem), 653 | Type(TypeImplItem), 654 | Existential(ExistentialImplItem), 655 | Method(MethodImplItem), 656 | Macro(Macro), 657 | } 658 | 659 | pub type ConstImplItem = Const; 660 | 661 | #[derive(Debug)] 662 | pub struct TypeImplItem { 663 | pub name: String, 664 | pub generics: Generics, 665 | pub ty: Type, 666 | } 667 | 668 | #[derive(Debug)] 669 | pub struct ExistentialImplItem { 670 | pub name: String, 671 | pub generics: Generics, 672 | pub bounds: TypeParamBounds, 673 | } 674 | 675 | #[derive(Debug)] 676 | pub struct MethodImplItem { 677 | pub sig: MethodSig, 678 | pub block: Block, 679 | } 680 | 681 | #[derive(Debug)] 682 | pub struct Block { 683 | pub loc: Loc, 684 | pub is_unsafe: bool, 685 | pub stmts: Vec, 686 | } 687 | 688 | impl Block { 689 | #[inline] 690 | pub fn is_one_literal_expr(&self) -> bool { 691 | if self.stmts.len() != 1 { 692 | return false; 693 | } 694 | 695 | match &self.stmts[0].stmt { 696 | StmtKind::Expr(ref expr, _) => { 697 | match expr.expr { 698 | ExprKind::Literal(_) | ExprKind::Path(_) => true, 699 | _ => false, 700 | } 701 | }, 702 | _ => false, 703 | } 704 | } 705 | } 706 | 707 | #[derive(Debug)] 708 | pub struct Stmt { 709 | pub loc: Loc, 710 | pub stmt: StmtKind, 711 | } 712 | 713 | #[derive(Debug)] 714 | pub enum StmtKind { 715 | Item(Item), 716 | Let(Let), 717 | Expr(Expr, bool), 718 | Macro(MacroStmt), 719 | } 720 | 721 | #[derive(Debug)] 722 | pub struct Let { 723 | pub loc: Loc, 724 | pub attrs: Vec, 725 | pub patten: Patten, 726 | pub ty: Option, 727 | pub init: Option, 728 | } 729 | 730 | #[derive(Debug)] 731 | pub struct Patten { 732 | pub loc: Loc, 733 | pub patten: PattenKind, 734 | } 735 | 736 | #[derive(Debug)] 737 | pub enum PattenKind { 738 | Wildcard, 739 | Symbol(&'static str), 740 | Literal(Expr), 741 | Range(RangePatten), 742 | Ref(Box), 743 | Path(PathPatten), 744 | Ident(Box), 745 | Struct(StructPatten), 746 | Enum(EnumPatten), 747 | Tuple(TuplePatten), 748 | Slice(Box), 749 | Macro(Macro), 750 | } 751 | 752 | #[derive(Debug)] 753 | pub struct RangePatten { 754 | pub start: Expr, 755 | pub end: Expr, 756 | pub is_inclusive: bool, 757 | } 758 | 759 | #[derive(Debug)] 760 | pub struct RefPatten { 761 | pub is_mut: bool, 762 | pub patten: Patten, 763 | } 764 | 765 | pub type PathPatten = PathType; 766 | 767 | #[derive(Debug)] 768 | pub struct IdentPatten { 769 | pub is_ref: bool, 770 | pub is_mut: bool, 771 | pub name: String, 772 | pub patten: Option, 773 | } 774 | 775 | #[derive(Debug)] 776 | pub struct StructPatten { 777 | pub path: Path, 778 | pub fields: Vec, 779 | pub omit: bool, 780 | } 781 | 782 | #[derive(Debug)] 783 | pub struct StructFieldPatten { 784 | pub loc: Loc, 785 | pub name: String, 786 | pub patten: Patten, 787 | pub shorthand: bool, 788 | } 789 | 790 | #[derive(Debug)] 791 | pub struct EnumPatten { 792 | pub path: Path, 793 | pub pattens: Vec, 794 | } 795 | 796 | #[derive(Debug)] 797 | pub struct TuplePatten { 798 | pub pattens: Vec, 799 | } 800 | 801 | #[derive(Debug)] 802 | pub struct SlicePatten { 803 | pub pattens: Vec, 804 | } 805 | 806 | #[derive(Debug)] 807 | pub struct Expr { 808 | pub loc: Loc, 809 | pub attrs: Vec, 810 | pub expr: ExprKind, 811 | } 812 | 813 | #[derive(Debug)] 814 | pub enum ExprKind { 815 | Literal(Chunk), 816 | Path(PathExpr), 817 | Ref(Box), 818 | UnaryOp(Box), 819 | Try(Box), 820 | ListOp(Box), 821 | Repeat(Box), 822 | Array(Box>), 823 | Tuple(Box>), 824 | Index(Box), 825 | Struct(Box), 826 | Field(Box), 827 | Type(Box), 828 | Cast(Box), 829 | Range(Box), 830 | Block(Box), 831 | If(Box), 832 | While(Box), 833 | Let(Box), 834 | For(Box), 835 | Loop(Box), 836 | Break(Box), 837 | Continue(Box), 838 | Match(Box), 839 | FnCall(Box), 840 | MethodCall(Box), 841 | Closure(Box), 842 | Return(Box), 843 | Macro(Macro), 844 | } 845 | 846 | pub type PathExpr = PathType; 847 | 848 | #[derive(Debug)] 849 | pub struct RefExpr { 850 | pub is_mut: bool, 851 | pub expr: Expr, 852 | } 853 | 854 | #[derive(Debug)] 855 | pub struct UnaryOpExpr { 856 | pub op: &'static str, 857 | pub expr: Expr, 858 | } 859 | 860 | #[derive(Debug)] 861 | pub struct ListOpExpr { 862 | pub op: Chunk, 863 | pub exprs: Vec, 864 | } 865 | 866 | #[derive(Debug)] 867 | pub struct RepeatExpr { 868 | pub value: Expr, 869 | pub len: Expr, 870 | } 871 | 872 | #[derive(Debug)] 873 | pub struct IndexExpr { 874 | pub obj: Expr, 875 | pub index: Expr, 876 | } 877 | 878 | #[derive(Debug)] 879 | pub struct StructExpr { 880 | pub path: Path, 881 | pub fields: Vec, 882 | pub base: Option, 883 | } 884 | 885 | #[derive(Debug)] 886 | pub struct StructFieldExpr { 887 | pub loc: Loc, 888 | pub name: String, 889 | pub value: Expr, 890 | } 891 | 892 | #[derive(Debug)] 893 | pub struct FieldExpr { 894 | pub expr: Expr, 895 | pub field: String, 896 | } 897 | 898 | #[derive(Debug)] 899 | pub struct TypeExpr { 900 | pub expr: Expr, 901 | pub ty: Type, 902 | } 903 | 904 | #[derive(Debug)] 905 | pub struct CastExpr { 906 | pub expr: Expr, 907 | pub ty: Type, 908 | } 909 | 910 | #[derive(Debug)] 911 | pub struct RangeExpr { 912 | pub start: Option, 913 | pub end: Option, 914 | pub is_inclusive: bool, 915 | } 916 | 917 | #[derive(Debug)] 918 | pub struct BlockExpr { 919 | pub label: Option, 920 | pub block: Block, 921 | } 922 | 923 | #[derive(Debug)] 924 | pub struct IfExpr { 925 | pub expr: Expr, 926 | pub block: Block, 927 | pub br: Option, 928 | } 929 | 930 | #[derive(Debug)] 931 | pub struct WhileExpr { 932 | pub label: Option, 933 | pub expr: Expr, 934 | pub block: Block, 935 | } 936 | 937 | #[derive(Debug)] 938 | pub struct LetExpr { 939 | pub pattens: Vec, 940 | pub expr: Expr, 941 | } 942 | 943 | #[derive(Debug)] 944 | pub struct ForExpr { 945 | pub label: Option, 946 | pub patten: Patten, 947 | pub expr: Expr, 948 | pub block: Block, 949 | } 950 | 951 | #[derive(Debug)] 952 | pub struct LoopExpr { 953 | pub label: Option, 954 | pub block: Block, 955 | } 956 | 957 | #[derive(Debug)] 958 | pub struct BreakExpr { 959 | pub label: Option, 960 | pub expr: Option, 961 | } 962 | 963 | #[derive(Debug)] 964 | pub struct ContinueExpr { 965 | pub label: Option, 966 | } 967 | 968 | #[derive(Debug)] 969 | pub struct MatchExpr { 970 | pub expr: Expr, 971 | pub arms: Vec, 972 | } 973 | 974 | #[derive(Debug)] 975 | pub struct Arm { 976 | pub loc: Loc, 977 | pub attrs: Vec, 978 | pub pattens: Vec, 979 | pub guard: Option, 980 | pub body: Expr, 981 | } 982 | 983 | #[derive(Debug)] 984 | pub struct FnCallExpr { 985 | pub name: Expr, 986 | pub args: Vec, 987 | } 988 | 989 | #[derive(Debug)] 990 | pub struct MethodCallExpr { 991 | pub path: PathSegment, 992 | pub args: Vec, 993 | } 994 | 995 | #[derive(Debug)] 996 | pub struct ClosureExpr { 997 | pub is_static: bool, 998 | pub is_async: bool, 999 | pub is_move: bool, 1000 | pub sig: FnSig, 1001 | pub expr: Expr, 1002 | } 1003 | 1004 | #[derive(Debug)] 1005 | pub struct ReturnExpr { 1006 | pub ret: Option, 1007 | } 1008 | 1009 | #[derive(Debug)] 1010 | pub struct MacroDef { 1011 | pub name: String, 1012 | pub def: String, 1013 | } 1014 | 1015 | #[derive(Debug)] 1016 | pub struct MacroStmt { 1017 | pub loc: Loc, 1018 | pub attrs: Vec, 1019 | pub mac: Macro, 1020 | pub is_semi: bool, 1021 | } 1022 | 1023 | #[derive(Debug)] 1024 | pub enum MacroStyle { 1025 | Paren, 1026 | Bracket, 1027 | Brace, 1028 | } 1029 | 1030 | #[derive(Debug)] 1031 | pub struct MacroSep { 1032 | pub is_sep: bool, 1033 | pub s: &'static str, 1034 | } 1035 | 1036 | #[derive(Debug)] 1037 | pub struct Macro { 1038 | pub name: String, 1039 | pub style: MacroStyle, 1040 | pub exprs: Vec, 1041 | pub seps: Vec, 1042 | } 1043 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | use std::path::PathBuf; 2 | 3 | use structopt::StructOpt; 4 | 5 | mod ast; 6 | mod ft; 7 | mod ir; 8 | mod rfmt; 9 | mod tr; 10 | mod ts; 11 | 12 | #[derive(Debug, StructOpt)] 13 | pub struct Opt { 14 | #[structopt(long, short)] 15 | /// Print the rust original syntax ast debug info 16 | ast: bool, 17 | 18 | #[structopt(long, short)] 19 | /// Check exceed lines and trailing white space lines 20 | check: bool, 21 | 22 | #[structopt(long, short)] 23 | /// Print the rfmt ir debug info 24 | debug: bool, 25 | 26 | #[structopt(long, short)] 27 | /// Print the rfmt ir simple format 28 | print: bool, 29 | 30 | #[structopt(long, short)] 31 | /// Overwrite the source file 32 | overwrite: bool, 33 | 34 | /// Input file or dir. 35 | /// If `input` is a dir, rfmt will do action for all files in this dir recursively. 36 | /// If neither `options` nor `input` is specified, rfmt will format source code from stdin. 37 | #[structopt(parse(from_os_str))] 38 | input: Option, 39 | } 40 | 41 | fn main() { 42 | let opt = Opt::from_args(); 43 | if opt.input.is_none() { 44 | rfmt::fmt_from_stdin(opt); 45 | } else if opt.ast { 46 | rfmt::dump_ast(&opt.input.unwrap()); 47 | } else if opt.debug { 48 | rfmt::debug(&opt.input.unwrap()); 49 | } else if opt.print { 50 | rfmt::print(&opt.input.unwrap()); 51 | } else { 52 | rfmt::fmt(opt); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/rfmt.rs: -------------------------------------------------------------------------------- 1 | use std::fs; 2 | use std::fs::File; 3 | use std::io::{self, Read, Write}; 4 | use std::path::{Path, PathBuf}; 5 | 6 | use syntax::parse::{self, ParseSess, lexer::comments}; 7 | use syntax::source_map::FilePathMapping; 8 | use syntax_pos::FileName; 9 | use walkdir::WalkDir; 10 | 11 | use crate::Opt; 12 | use crate::ft; 13 | use crate::tr::{self, TrResult}; 14 | 15 | macro_rules! p { 16 | () => ({println!()}); 17 | ($arg:expr) => ({println!("{}", $arg)}); 18 | ($fmt:expr, $($arg:tt)*) => ({println!($fmt, $($arg)*)}); 19 | ($($arg:tt)+) => ({println!("{}", $($arg)+)}); 20 | } 21 | 22 | macro_rules! d { 23 | ($arg:expr) => ({println!("{:#?}", $arg)}); 24 | } 25 | 26 | const SEP: &str = r#" 27 | ------------------------------------------------------------------------------------------------------------------------ 28 | "#; 29 | 30 | pub fn dump_ast(path: &PathBuf) { 31 | let src = fs::read_to_string(path).unwrap(); 32 | 33 | syntax::with_default_globals(|| { 34 | let sess = ParseSess::new(FilePathMapping::empty()); 35 | let krate = match parse::parse_crate_from_source_str(FileName::from(path.clone()), src.clone(), &sess) { 36 | Ok(krate) => krate, 37 | Err(mut e) => { 38 | e.emit(); 39 | return; 40 | }, 41 | }; 42 | d!(krate); 43 | 44 | p!(SEP); 45 | 46 | let cmnts = comments::gather_comments(&sess, FileName::from(path.clone()), src); 47 | for cmnt in cmnts { 48 | p!("{}: {:#?} {:#?}", cmnt.pos.0, cmnt.style, cmnt.lines); 49 | } 50 | }); 51 | } 52 | 53 | pub fn fmt_from_stdin(opt: Opt) { 54 | let mut src = String::new(); 55 | io::stdin().read_to_string(&mut src).unwrap(); 56 | fmt_str(src, &PathBuf::from("stdin"), &opt); 57 | } 58 | 59 | pub fn debug(path: &PathBuf) { 60 | let src = fs::read_to_string(path).unwrap(); 61 | let result = trans(src, path); 62 | 63 | d!(result.krate); 64 | p!(SEP); 65 | d!(result.leading_cmnts); 66 | d!(result.trailing_cmnts); 67 | } 68 | 69 | pub fn print(path: &PathBuf) { 70 | let src = fs::read_to_string(path).unwrap(); 71 | let result = trans(src, path); 72 | p!(result.krate); 73 | } 74 | 75 | pub fn fmt(opt: Opt) { 76 | let path = opt.input.as_ref().unwrap(); 77 | if path.is_dir() { 78 | fmt_dir(&path, &opt); 79 | } else { 80 | fmt_file(&path, &opt); 81 | } 82 | } 83 | 84 | fn fmt_dir(path: &Path, opt: &Opt) { 85 | for entry in WalkDir::new(path) { 86 | let entry = entry.unwrap(); 87 | if entry.file_type().is_file() { 88 | let path = entry.into_path(); 89 | let ext = path.extension(); 90 | if let Some(ext) = ext { 91 | if ext == "rs" { 92 | fmt_file(&path, opt); 93 | } 94 | } 95 | } 96 | } 97 | } 98 | 99 | fn fmt_file(path: &PathBuf, opt: &Opt) { 100 | let src = fs::read_to_string(path).unwrap(); 101 | fmt_str(src, path, opt); 102 | } 103 | 104 | fn fmt_str(src: String, path: &PathBuf, opt: &Opt) { 105 | let tr_result = trans(src, path); 106 | let ft_result = ft::fmt(tr_result.krate, tr_result.leading_cmnts, tr_result.trailing_cmnts); 107 | if opt.overwrite { 108 | let mut file = File::create(path).unwrap(); 109 | file.write_all(ft_result.s.as_bytes()).unwrap(); 110 | } else if opt.check { 111 | if !ft_result.exceed_lines.is_empty() || !ft_result.trailing_ws_lines.is_empty() { 112 | p!("{:?}", path); 113 | if !ft_result.exceed_lines.is_empty() { 114 | p!("exceed_lines: {:?}", ft_result.exceed_lines); 115 | } 116 | if !ft_result.trailing_ws_lines.is_empty() { 117 | p!("trailing_ws_lines: {:?}", ft_result.trailing_ws_lines); 118 | } 119 | p!(SEP); 120 | } 121 | } else { 122 | p!(ft_result.s); 123 | } 124 | } 125 | 126 | fn trans(src: String, path: &PathBuf) -> TrResult { 127 | syntax::with_default_globals(|| { 128 | let sess = ParseSess::new(FilePathMapping::empty()); 129 | let krate = parse::parse_crate_from_source_str(FileName::from(path.to_path_buf()), src.clone(), &sess).unwrap(); 130 | let cmnts = comments::gather_comments(&sess, FileName::from(path.to_path_buf()), src.clone()); 131 | tr::trans(src, sess, krate, cmnts) 132 | }) 133 | } 134 | 135 | -------------------------------------------------------------------------------- /src/ts.rs: -------------------------------------------------------------------------------- 1 | use std::collections::BTreeSet; 2 | use std::fmt::{self, Debug}; 3 | 4 | const NL: char = '\n'; 5 | 6 | const EXCEED_WIDTH: usize = 120; 7 | const MAX_WIDTH: usize = EXCEED_WIDTH - 1; 8 | const MAX_ALIGN_COL: usize = EXCEED_WIDTH / 3; 9 | 10 | const INDENT: &'static str = " "; 11 | const WRAP_INDENT: &'static str = " "; 12 | 13 | #[macro_export] 14 | macro_rules! need_wrap { 15 | ($ts:expr, $($s:expr),+) => ({ 16 | $ts.need_wrap(&[$($s),+]) 17 | }); 18 | } 19 | 20 | #[macro_export] 21 | macro_rules! need_nl_indent { 22 | ($ts:expr, $($s:expr),+) => ({ 23 | $ts.need_nl_indent(&[$($s),+]) 24 | }); 25 | } 26 | 27 | macro_rules! raw_insert { 28 | ($sf:expr, $s:expr) => ({ 29 | $sf.s.push_str($s); 30 | 31 | $sf.col += $s.len(); 32 | if $sf.col > EXCEED_WIDTH { 33 | $sf.exceed_lines.insert($sf.line); 34 | } 35 | }); 36 | } 37 | 38 | macro_rules! minus_nf { 39 | ($a: expr, $b: expr) => ({ 40 | if $a <= $b { 41 | 0 42 | } else { 43 | $a - $b 44 | } 45 | }) 46 | } 47 | 48 | #[inline] 49 | fn list_len_info(list: &[&str]) -> (usize, usize) { 50 | let prefix_len = if list.len() > 1 { 51 | list.iter().take(list.len() - 1).map(|s| str_one_line_len(s)).sum() 52 | } else { 53 | 0 54 | }; 55 | let len = list.iter().map(|s| str_one_line_len(s)).sum(); 56 | (prefix_len, len) 57 | } 58 | 59 | #[inline] 60 | fn str_one_line_len(s: &str) -> usize { 61 | if let Some(pos) = s.find('\n') { 62 | pos 63 | } else { 64 | s.len() 65 | } 66 | } 67 | 68 | #[inline] 69 | fn fill_str(ch: char, count: usize) -> String { 70 | let mut s = String::with_capacity(count); 71 | for _ in 0..count { 72 | s.push(ch); 73 | } 74 | s 75 | } 76 | 77 | #[derive(Default)] 78 | pub struct Typesetter { 79 | line: u32, 80 | col: usize, 81 | indent: String, 82 | align_stack: Vec, 83 | 84 | s: String, 85 | exceed_lines: BTreeSet, 86 | trailing_ws_lines: BTreeSet, 87 | } 88 | 89 | pub struct TsResult { 90 | pub s: String, 91 | pub exceed_lines: BTreeSet, 92 | pub trailing_ws_lines: BTreeSet, 93 | } 94 | 95 | impl Debug for Typesetter { 96 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 97 | write!(f, "pos: ({}, {})\n", self.line, self.col)?; 98 | write!(f, "indent: \"{}\"\n", self.indent)?; 99 | write!(f, "align stack: ")?; 100 | Debug::fmt(&self.align_stack, f)?; 101 | write!(f, "\nexceed lines: ")?; 102 | Debug::fmt(&self.exceed_lines, f) 103 | } 104 | } 105 | 106 | impl Typesetter { 107 | pub fn new() -> Typesetter { 108 | Typesetter { 109 | line: 1, 110 | ..Default::default() 111 | } 112 | } 113 | 114 | pub fn result(self) -> TsResult { 115 | TsResult { 116 | s: self.s, 117 | exceed_lines: self.exceed_lines, 118 | trailing_ws_lines: self.trailing_ws_lines, 119 | } 120 | } 121 | 122 | #[inline] 123 | pub fn force_insert(&mut self, s: &str) { 124 | self.s.push_str(s); 125 | } 126 | 127 | #[inline] 128 | pub fn raw_insert(&mut self, s: &str) { 129 | raw_insert!(self, s); 130 | } 131 | 132 | #[inline] 133 | pub fn insert(&mut self, s: &str) { 134 | if need_wrap!(self, s) { 135 | self.wrap_insert(s); 136 | } else { 137 | self.raw_insert(s); 138 | } 139 | } 140 | 141 | #[inline] 142 | pub fn indent(&mut self) { 143 | self.indent.push_str(INDENT); 144 | } 145 | 146 | #[inline] 147 | pub fn outdent(&mut self) { 148 | let len = self.indent.len(); 149 | self.indent.truncate(len - INDENT.len()); 150 | } 151 | 152 | #[inline] 153 | pub fn insert_indent(&mut self) { 154 | raw_insert!(self, &self.indent); 155 | } 156 | 157 | #[inline] 158 | pub fn nl(&mut self) { 159 | if let Some(ch) = self.s.chars().last() { 160 | if ch != NL && ch.is_whitespace() { 161 | self.trailing_ws_lines.insert(self.line); 162 | } 163 | } 164 | 165 | self.s.push(NL); 166 | self.line += 1; 167 | self.col = 0; 168 | } 169 | 170 | #[inline] 171 | pub fn nl_indent(&mut self) { 172 | self.nl(); 173 | self.insert_indent(); 174 | } 175 | 176 | #[inline] 177 | pub fn can_one_line(&self, s: &str) -> bool { 178 | self.left() > s.len() 179 | } 180 | 181 | #[inline] 182 | pub fn need_wrap(&self, list: &[&str]) -> bool { 183 | let (prefix_len, len) = list_len_info(list); 184 | self.need_wrap_len(prefix_len, len) 185 | } 186 | 187 | #[inline] 188 | pub fn need_nl_indent(&self, list: &[&str]) -> bool { 189 | let (prefix_len, len) = list_len_info(list); 190 | self.need_nl_indent_len(prefix_len, len) 191 | } 192 | 193 | #[inline] 194 | pub fn wrap(&mut self) { 195 | self.nl(); 196 | 197 | if self.should_align() { 198 | self.insert_align(); 199 | } else { 200 | self.insert_indent(); 201 | self.insert_wrap_indent(); 202 | } 203 | } 204 | 205 | #[inline] 206 | pub fn insert_mark_align(&mut self, s: &str) { 207 | self.raw_insert(s); 208 | self.mark_align(); 209 | } 210 | 211 | #[inline] 212 | pub fn insert_unmark_align(&mut self, s: &str) { 213 | self.raw_insert(s); 214 | self.unmark_align(); 215 | } 216 | 217 | #[inline] 218 | fn wrap_insert(&mut self, s: &str) { 219 | self.wrap(); 220 | self.raw_insert(s); 221 | } 222 | 223 | #[inline] 224 | fn need_wrap_len(&self, prefix_len: usize, len: usize) -> bool { 225 | (minus_nf!(self.left(), prefix_len) <= 0) || (len > self.left() && len <= self.nl_left()) 226 | } 227 | 228 | #[inline] 229 | fn nl_left(&self) -> usize { 230 | if self.should_align() { 231 | self.nl_align_left() 232 | } else { 233 | self.nl_wrap_left() 234 | } 235 | } 236 | 237 | #[inline] 238 | fn should_align(&self) -> bool { 239 | match self.align_stack.last() { 240 | Some(col) if *col <= MAX_ALIGN_COL => true, 241 | _ => false, 242 | } 243 | } 244 | 245 | #[inline] 246 | fn nl_align_left(&self) -> usize { 247 | minus_nf!(MAX_WIDTH, *self.align_stack.last().unwrap()) 248 | } 249 | 250 | #[inline] 251 | fn nl_wrap_left(&self) -> usize { 252 | minus_nf!(MAX_WIDTH, self.indent.len() + WRAP_INDENT.len()) 253 | } 254 | 255 | #[inline] 256 | fn need_nl_indent_len(&self, prefix_len: usize, len: usize) -> bool { 257 | (minus_nf!(self.left(), prefix_len) <= 0) || (len > self.left() && len <= self.nl_indent_left()) 258 | } 259 | 260 | #[inline] 261 | fn left(&self) -> usize { 262 | minus_nf!(MAX_WIDTH, self.col) 263 | } 264 | 265 | #[inline] 266 | fn nl_indent_left(&self) -> usize { 267 | minus_nf!(MAX_WIDTH, self.indent.len()) 268 | } 269 | 270 | #[inline] 271 | fn insert_wrap_indent(&mut self) { 272 | self.raw_insert(WRAP_INDENT); 273 | } 274 | 275 | #[inline] 276 | fn insert_align(&mut self) { 277 | let blank = fill_str(' ', *self.align_stack.last().unwrap()); 278 | self.raw_insert(&blank); 279 | } 280 | 281 | #[inline] 282 | fn mark_align(&mut self) { 283 | self.align_stack.push(self.col); 284 | } 285 | 286 | #[inline] 287 | fn unmark_align(&mut self) { 288 | self.align_stack.pop(); 289 | } 290 | } 291 | -------------------------------------------------------------------------------- /tests/comments/attr.rs: -------------------------------------------------------------------------------- 1 | 2 | // aa 3 | 4 | // bb 5 | #![allow(unused_variables)] // cc 6 | #![crate_type = "lib"] // dd 7 | // ee 8 | 9 | -------------------------------------------------------------------------------- /tests/comments/block.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | 'label: { 3 | let a = 10; 4 | // leading 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /tests/comments/doc.rs: -------------------------------------------------------------------------------- 1 | // aa 2 | 3 | // bb 4 | #![doc(html_favicon_url = "https://example.com/favicon.ico")] // cc 5 | // dd 6 | 7 | // aa 8 | 9 | // bb 10 | //!aaaaaaaaaaaaaa 11 | //!bbbbbbbbbbbbbb 12 | // dd 13 | -------------------------------------------------------------------------------- /tests/comments/enum.rs: -------------------------------------------------------------------------------- 1 | // 2 | // aa 3 | enum A<'a> { // enum-trailing 4 | Dog = 10, // bb 5 | Cat, // cc 6 | // enum-leading 7 | } // dd 8 | // hh 9 | // 10 | -------------------------------------------------------------------------------- /tests/comments/extern_crate.rs: -------------------------------------------------------------------------------- 1 | 2 | // 3 | // aa 4 | // 5 | // bb 6 | extern crate aa as _; // cc 7 | extern crate bb; // dd 8 | extern crate cc as dd; // ee 9 | // ff 10 | // 11 | // gg 12 | -------------------------------------------------------------------------------- /tests/comments/fn.rs: -------------------------------------------------------------------------------- 1 | // 2 | // aa 3 | pub fn fmt(krate: Crate, leading_cmnts: HashMap>, 4 | trailing_cmnts: HashMap) -> rfmt::Result { // fn-trailing 5 | // aa 6 | Formatter::new(leading_cmnts, trailing_cmnts).fmt_crate(krate) // bb 7 | // fn-leading 8 | } // dd 9 | // ee 10 | // 11 | -------------------------------------------------------------------------------- /tests/comments/for.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | 'label: for a in b { 3 | let c = 10; 4 | // leading 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /tests/comments/foreign.rs: -------------------------------------------------------------------------------- 1 | // 2 | // aa 3 | extern "C" { // foreign-trailing 4 | static a: bool; // aa 5 | pub fn f(a: bool) -> i32; // bb 6 | // foreign-leading 7 | } // cc 8 | // dd 9 | // 10 | -------------------------------------------------------------------------------- /tests/comments/impl.rs: -------------------------------------------------------------------------------- 1 | // 2 | // aa 3 | impl A for B { // impl-trailing 4 | default const a: bool = true; // aa 5 | type E = T; // bb 6 | existential type Item: Debug; 7 | fn f(&self) {} 8 | a!(true); 9 | // impl-leading 10 | } // dd 11 | 12 | -------------------------------------------------------------------------------- /tests/comments/macro.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | a!{ a, 3 | b, 4 | // leading 5 | }; 6 | } 7 | -------------------------------------------------------------------------------- /tests/comments/mod.rs: -------------------------------------------------------------------------------- 1 | // 2 | // aa 3 | // 4 | // bb 5 | mod aa { // block-trailing 6 | // cc 7 | const A: i32 = 0; // dd 8 | // block-leading 9 | } // ff 10 | -------------------------------------------------------------------------------- /tests/comments/mod_decl.rs: -------------------------------------------------------------------------------- 1 | 2 | // 3 | // aa 4 | // 5 | // bb 6 | mod a; // cc 7 | 8 | mod aa {} // dd 9 | 10 | pub mod b; // ee 11 | 12 | // ff 13 | mod c {} // gg 14 | // hh 15 | 16 | -------------------------------------------------------------------------------- /tests/comments/patten.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | Some(..) => a, 4 | // leading 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /tests/comments/struct.rs: -------------------------------------------------------------------------------- 1 | // aaaaa 2 | 3 | // bbbbb 4 | struct A { // ccccc-struct-trailing 5 | // ddddd 6 | a: bool, // eeeee 7 | b: i32, // ffff 8 | // ggggg-struct-heading 9 | } // hhhhh 10 | -------------------------------------------------------------------------------- /tests/comments/struct_expr.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | Point { // block-trailing 3 | x: 10.0, // aa 4 | y: 20.0, // bb 5 | // block-leading 6 | }; 7 | } 8 | -------------------------------------------------------------------------------- /tests/comments/struct_patten.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | A { x: bool, y: B { y1, y2, }, ref mut z, .. } => true, 4 | A { 5 | xxxxxxxxxxxxxxxxxxxx: bool, // aa 6 | // bb 7 | yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy: B { y1, y2, }, 8 | ref mut zzzzzzzzzzzzzzzzzzzzz, 9 | // cc 10 | .. 11 | } => true, 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/comments/sugared_doc.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT 2 | // file at the top-level directory of this distribution and at 3 | // http://rust-lang.org/COPYRIGHT. 4 | // 5 | // Licensed under the Apache License, Version 2.0 or the MIT license 7 | // , at your 8 | // option. This file may not be copied, modified, or distributed 9 | // except according to those terms. 10 | 11 | //! # The Rust Standard Library 12 | #![test] 13 | 14 | /// # The Rust Standard Library 15 | /// 16 | /// 17 | 18 | fn f() {} 19 | -------------------------------------------------------------------------------- /tests/comments/trait.rs: -------------------------------------------------------------------------------- 1 | // 2 | // aa 3 | pub trait Trait { // trait-trailing 4 | const a: bool = true; // aa 5 | type E: Iterator + 'static = bool; // bb 6 | unsafe fn f(); 7 | const fn f(&self); 8 | fn f(&self); 9 | fn f(&'a self); 10 | fn f(self: Iterator); 11 | a!(true); 12 | // trait-leading 13 | } 14 | -------------------------------------------------------------------------------- /tests/comments/try_block.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | try { 3 | a: bool, 4 | // leading 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /tests/comments/union.rs: -------------------------------------------------------------------------------- 1 | // 2 | // aa 3 | pub union Point { // union-trailing 4 | pub x: i32, // bb 5 | y: i32, 6 | // union-leading 7 | } 8 | 9 | -------------------------------------------------------------------------------- /tests/comments/use.rs: -------------------------------------------------------------------------------- 1 | 2 | // aa 3 | // 4 | // bb 5 | use *; // cc 6 | use ::*; // dd 7 | use ::f; 8 | use a::b::{c, d, e::f, g::h::i}; // ee 9 | use a::b::{self, c, d::e}; // ff 10 | use a::b::{self as ab, c as abc}; // gg 11 | use a::b::*; // hh 12 | use a::b::{ self as ab, c, d::{*, e::f}, }; 13 | use p::q::r as x; 14 | 15 | // 16 | // jj 17 | // 18 | // kk 19 | use crate::aa as x; // ll 20 | // mm 21 | // nn 22 | 23 | -------------------------------------------------------------------------------- /tests/examples/a.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let (a, b, c, d) = (0, 0, 0, 0); 3 | let _ = u32::from_be(((a as u32) << 24) | 4 | ((b as u32) << 16) | 5 | ((c as u32) << 8) | 6 | (d as u32) << 0); 7 | } 8 | -------------------------------------------------------------------------------- /tests/examples/b.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let ref_packet = [0xde, 0xf0, 0x12, 0x34, 0x45, 0x67, 3 | 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 4 | 0x86, 0xdd]; 5 | } 6 | -------------------------------------------------------------------------------- /tests/examples/c.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | f(123456789, "abcdefg", "hijklmn", 0987654321, "opqrst", "uvwxyz", 123456789, "abcdefg", "hijklmn", 0987654321, 3 | "opqrst", "uvwxyz"); 4 | } 5 | -------------------------------------------------------------------------------- /tests/examples/comment.rs: -------------------------------------------------------------------------------- 1 | // aaaaa 2 | 3 | // bbbbb 4 | struct A { // ccccc-DISAPPEARED 5 | // ddddd 6 | a: bool, // eeeee 7 | b: i32, // ffff 8 | // ggggg 9 | } // hhhhh 10 | 11 | // iiiii 12 | fn f(a: bool, /* jjjjj-DISAPPEARED */ b: i32, /* kkkkk-DISAPPEARED */) -> bool { // lllll-DISAPPEARED 13 | // mmmmm 14 | const b: bool = false; // nnnnn 15 | let mut a = true; // ooooo 16 | a = false; // ppppp 17 | a!();// qqqqq 18 | a // rrrrr 19 | } // sssss 20 | // ttttt 21 | 22 | // uuuuu 23 | -------------------------------------------------------------------------------- /tests/examples/d.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff(123456789, "abcdefg", "hijklmn", 3 | 0987654321, "opqrst", "uvwxyz", 123456789, "abcdefg", "hijklmn", 0987654321, "opqrst", "uvwxyz"); 4 | fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff( 5 | 123456789, "abcdefg", "hijklmn", 0987654321, "opqrst", "uvwxyz", 123456789, "abcdefg", "hijklmn", 0987654321, "opqrst", "uvwxyz"); 6 | } 7 | -------------------------------------------------------------------------------- /tests/examples/e.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let mut arms = variants.iter().enumerate().map(|(i, &(ident, v_span, ref summary))| { 3 | let i_expr = cx.expr_usize(v_span, i); 4 | let pat = cx.pat_lit(v_span, i_expr); 5 | 6 | let path = cx.path(v_span, vec![substr.type_ident, ident]); 7 | let thing = rand_thing(cx, v_span, path, summary, |cx, sp| rand_call(cx, sp)); 8 | cx.arm(v_span, vec![ pat ], thing) 9 | }).collect:: >(); 10 | } 11 | -------------------------------------------------------------------------------- /tests/examples/f.rs: -------------------------------------------------------------------------------- 1 | #![feature(custom_derive)] 2 | #![deny(warnings)] 3 | #![feature(question_mark)] 4 | #![feature(iter_arith)] 5 | #![feature(rustc_private)] 6 | 7 | extern crate rst; 8 | extern crate getopts; 9 | extern crate walkdir; 10 | 11 | use std::env; 12 | use getopts::Options; 13 | 14 | #[macro_use] 15 | mod ts; 16 | 17 | mod ir; 18 | mod ft; 19 | mod tr; 20 | mod rfmt; 21 | -------------------------------------------------------------------------------- /tests/examples/g.rs: -------------------------------------------------------------------------------- 1 | // aaaaa 2 | // bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb 3 | fn main() { 4 | let a = r#"aaaaaaaaaaaaaaaaaaaaaaaaaaaa 5 | bbbbbbbbbbbbbbbbb"#; 6 | } 7 | -------------------------------------------------------------------------------- /tests/examples/main.rs: -------------------------------------------------------------------------------- 1 | // AST 2 | fn main() {} 3 | -------------------------------------------------------------------------------- /tests/ft/attrs/attrs.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT 2 | // file at the top-level directory of this distribution and at 3 | // http://rust-lang.org/COPYRIGHT. 4 | // 5 | // Licensed under the Apache License, Version 2.0 or the MIT license 7 | // , at your 8 | // option. This file may not be copied, modified, or distributed 9 | // except according to those terms. 10 | 11 | //! The Rust parser and macro expander. 12 | //! 13 | //! # Note 14 | //! 15 | //! This API is completely unstable and subject to change. 16 | 17 | #![crate_name = "syntax"] // abcdefg 18 | #![unstable(feature = "rustc_private", issue = "27812")] /* hijklmn */ 19 | #![crate_type = "rlib"] // aaaaa 20 | #![crate_type = "dylib"] // bbbbb 21 | /* abcdefg 22 | * hijklmn 23 | */ 24 | #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", html_favicon_url = "https://doc.rust-lang.org/favicon.ico", html_root_url = "https://doc.rust-lang.org/nightly/", 25 | test(attr(deny(warnings))))] 26 | #![cfg_attr(not(stage0), 27 | deny(warnings))] 28 | 29 | #![feature(filling_drop)] 30 | #![feature(rustc_private)] 31 | #![feature(staged_api)] 32 | #![feature(str_escape)] 33 | #![feature(associated_consts)] 34 | #![feature(unicode)] 35 | #![feature(str_char)] // aaaaa 36 | #![feature(libc)] // ccccc 37 | 38 | 39 | 40 | 41 | 42 | // aaaaa 43 | // bbbbb 44 | -------------------------------------------------------------------------------- /tests/ft/attrs/doc_with_trailing_comment.rs: -------------------------------------------------------------------------------- 1 | // aaaaa 2 | /*! AAAAA !*/ /* bbbbb */ 3 | /*! BBBBB !*/ /* ccccc */ 4 | // ddddd 5 | -------------------------------------------------------------------------------- /tests/ft/attrs/nl.rs: -------------------------------------------------------------------------------- 1 | #![allow( 2 | unused_variables)] 3 | #![allow( a)] 4 | -------------------------------------------------------------------------------- /tests/ft/block.rs: -------------------------------------------------------------------------------- 1 | fn f() -> bool { 2 | let a; // aaaaa 3 | a = true; // 1111111111111111 4 | if true {} else {} // 222222222222222 5 | a!(); // bbbbbbbbbbbbb 6 | true; // ddddddddd 7 | unsafe loop { 8 | a = 10; 9 | } 10 | 11 | } // eeeeeeeeeee 12 | -------------------------------------------------------------------------------- /tests/ft/c/d.rs: -------------------------------------------------------------------------------- 1 | // leading c/d 2 | type d = bool; // trailing c/d 3 | -------------------------------------------------------------------------------- /tests/ft/c/e.rs: -------------------------------------------------------------------------------- 1 | // leading c/e 2 | type e = bool; // trailing c.e 3 | -------------------------------------------------------------------------------- /tests/ft/c/g/mod.rs: -------------------------------------------------------------------------------- 1 | #![test] 2 | // leading c/g 3 | type g = bool; // trailing c/g 4 | const a: &'static str = "gggggggggggggggggg"; 5 | -------------------------------------------------------------------------------- /tests/ft/comments/comments.rs: -------------------------------------------------------------------------------- 1 | #![crate_name = "syntax"] // abcd 2 | #![unstable(feature = "rustc_private", issue = "27812")] // aaaaaaaa 3 | #![crate_type = "rlib"] // bbbbbbbbbb 4 | #![crate_type = "dylib"]// cccccccccc 5 | 6 | // gggggggggggggggggg 7 | /* 8 | * abcdefg 9 | * hijklmn 10 | */ 11 | #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", 12 | html_favicon_url = "https://doc.rust-lang.org/favicon.ico", html_root_url = "https://doc.rust-lang.org/nightly/", test(attr(deny(warnings))))] 13 | // ddddddddddddddd 14 | #![cfg_attr(not(stage0), 15 | deny(warnings))] 16 | 17 | // aaaaaaaaaaaaa 18 | const a: bool = true; // bbbbbbbbbbbbbbbbb 19 | // cccccccccccccccc 20 | // ddddddddddddddddd 21 | 22 | const b: &'static str = br#"aaaaaaaaaaa"#; // eeeeeeeeeee 23 | pub const c: i32 = -12_345;// fffffffffffffffffff 24 | 25 | // aaaaaaaaaaaaa 26 | pub struct Point { 27 | // ddddddddddd 28 | pub x: i32, // Point aaa 29 | y: i32, // Point bbb 30 | // ccc 31 | pub xxx: i32, // Point ddddddd 32 | } 33 | 34 | // ffffffffffffff 35 | type a /* aaaaaaaaaaaa */ = bool; 36 | -------------------------------------------------------------------------------- /tests/ft/comments/leading_comments.rs: -------------------------------------------------------------------------------- 1 | // aaaaa 2 | 3 | mod a {} 4 | -------------------------------------------------------------------------------- /tests/ft/comments/left_comments.rs: -------------------------------------------------------------------------------- 1 | // aaaaa 2 | 3 | mod a {} 4 | // bbbbb 5 | // ccccc 6 | // 7 | -------------------------------------------------------------------------------- /tests/ft/const.rs: -------------------------------------------------------------------------------- 1 | const a: bool = true; 2 | const b: &'static str = br#"aaaaaaaaaaa"#; 3 | pub const c: i32 = -12_345; 4 | -------------------------------------------------------------------------------- /tests/ft/decl.rs: -------------------------------------------------------------------------------- 1 | fn f() -> bool { 2 | let a; 3 | let mut b: bool; 4 | let c = false; 5 | a = true; 6 | 7 | type c = i32; 8 | let f!(A); 9 | } 10 | -------------------------------------------------------------------------------- /tests/ft/enum.rs: -------------------------------------------------------------------------------- 1 | enum A { 2 | Dog = 10, // a 3 | Cat, // b 4 | } // c 5 | 6 | struct BA; 7 | struct BB; 8 | 9 | enum B { 10 | A(BA, 11 | BB), 12 | B(BB), 13 | } 14 | 15 | enum C { 16 | // ccccccccccccc 17 | CA { 18 | a: bool, // aaaaaaaaaaaa 19 | }, 20 | // ddddddddddddddd 21 | CB { 22 | b: i32, // bbbbbbbbbbbbb 23 | } 24 | } 25 | 26 | enum E {} 27 | -------------------------------------------------------------------------------- /tests/ft/exprs/array.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | [true, 1, "a"]; 3 | } 4 | -------------------------------------------------------------------------------- /tests/ft/exprs/async.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | let a = async || {}; 3 | } 4 | -------------------------------------------------------------------------------- /tests/ft/exprs/block.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | 'label: {} 3 | } 4 | -------------------------------------------------------------------------------- /tests/ft/exprs/box.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | box a; 3 | } 4 | -------------------------------------------------------------------------------- /tests/ft/exprs/break.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | 'label: for a in b { 3 | break 'label; 4 | } 5 | 6 | let a = for b in c { 7 | break 0; 8 | }; 9 | } 10 | -------------------------------------------------------------------------------- /tests/ft/exprs/cast.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | a as bool; 3 | } 4 | -------------------------------------------------------------------------------- /tests/ft/exprs/closure.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | static move |a: bool| -> bool { true }; 3 | let add = |x, y| {x + y; y+x}; 4 | } 5 | -------------------------------------------------------------------------------- /tests/ft/exprs/continue.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | 'label: for a in b { 3 | continue 'label; 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /tests/ft/exprs/expr.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | true; 3 | 4 | a::b; 5 | <::i32 as Vec>::MAX; 6 | 7 | &a; 8 | &mut b; 9 | 10 | !a; 11 | 12 | f()?; 13 | 14 | a + b; 15 | a = bool; 16 | a += 1; 17 | a + b + c; 18 | 19 | [true; 10]; 20 | 21 | [true, 1, "a"]; 22 | 23 | (true, 1, "a"); 24 | 25 | a = b[1]; 26 | 27 | NothingInMe {}; 28 | Point { x: 10.0, y: 20.0 }; 29 | TuplePoint { 0: 10.0, 1: 20.0 }; 30 | Point3d { y: 0, z: 10, ..base }; 31 | 32 | a.a; 33 | a.0; 34 | 35 | a: bool; 36 | 37 | a as bool; 38 | 39 | 1..2; 40 | 1..; 41 | ..2; 42 | 1..=2; 43 | ..=2; 44 | 45 | {} 46 | 'label: {} 47 | 'label: { 48 | true; 49 | } 50 | 51 | if true {} else if false {} else {} 52 | if true { 53 | a; 54 | } else if false { 55 | b; 56 | } else { 57 | c; 58 | } 59 | 60 | if let Some(ref a) = a { 61 | true; 62 | } 63 | if let a | b | c = d {} else {} 64 | 65 | 'label: while true {} 66 | 67 | while let a | b | c = d {} 68 | 69 | 'label: for a in b {} 70 | 71 | 'label: loop {} 72 | 73 | 'label: for a in b { 74 | break 'label; 75 | } 76 | 77 | let a = for b in c { 78 | break 0; 79 | }; 80 | 81 | 'label: for a in b { 82 | continue 'label; 83 | } 84 | 85 | ff(0, bool); 86 | Position(0, 0, 0); 87 | 88 | a.f::(0, bool); 89 | ::a.b.c(0, 1); 90 | 91 | static move |a: bool| -> bool { true; false; }; 92 | let add = |x, y| x + y; 93 | 94 | return bool; 95 | return; 96 | 97 | a!(); 98 | a!() 99 | } 100 | -------------------------------------------------------------------------------- /tests/ft/exprs/field.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | a.a; 3 | a.0; 4 | } 5 | -------------------------------------------------------------------------------- /tests/ft/exprs/fn_call.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | ff(0, bool); 3 | Position(0, 0, 0); 4 | } 5 | -------------------------------------------------------------------------------- /tests/ft/exprs/for.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | 'label: for a in b {} 3 | } 4 | -------------------------------------------------------------------------------- /tests/ft/exprs/if.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | if true {} else if false {} else {} 3 | } 4 | -------------------------------------------------------------------------------- /tests/ft/exprs/if_let.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | if let a | b | c = d {} else {} 3 | } 4 | -------------------------------------------------------------------------------- /tests/ft/exprs/index.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | a = b[1]; 3 | } 4 | -------------------------------------------------------------------------------- /tests/ft/exprs/list_op.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | a + b; 3 | a = bool; 4 | a += 1; 5 | a + b + c; 6 | } 7 | -------------------------------------------------------------------------------- /tests/ft/exprs/literal.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | true 3 | } 4 | -------------------------------------------------------------------------------- /tests/ft/exprs/loop.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | 'label: loop {} 3 | } 4 | -------------------------------------------------------------------------------- /tests/ft/exprs/macro.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | a!() 3 | } 4 | -------------------------------------------------------------------------------- /tests/ft/exprs/match.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | aa if y => true, 4 | bb | cc => false, 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /tests/ft/exprs/method_call.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | a.f::(0, bool); 3 | a.b.c(0); 4 | } 5 | -------------------------------------------------------------------------------- /tests/ft/exprs/path.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | a::b; 3 | <::i32 as Vec>::MAX; 4 | } 5 | -------------------------------------------------------------------------------- /tests/ft/exprs/range.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | 1..2; 3 | 1..; 4 | ..2; 5 | 1..=2; 6 | ..=2; 7 | } 8 | -------------------------------------------------------------------------------- /tests/ft/exprs/ref.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | &a; 3 | &mut b; 4 | } 5 | -------------------------------------------------------------------------------- /tests/ft/exprs/repeat.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | [true; 10]; 3 | } 4 | -------------------------------------------------------------------------------- /tests/ft/exprs/return.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | return bool; 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/ft/exprs/struct.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | NothingInMe {}; 3 | Point { x: 10.0, y: 20.0 }; 4 | TuplePoint { 0: 10.0, 1: 20.0, }; 5 | Point3d { y: 0, z: 10, ..base, }; 6 | A { a, b }; 7 | } 8 | -------------------------------------------------------------------------------- /tests/ft/exprs/try.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | a? 3 | } 4 | -------------------------------------------------------------------------------- /tests/ft/exprs/try_block.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | try {} 3 | } 4 | -------------------------------------------------------------------------------- /tests/ft/exprs/tuple.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | (true, 1, "a"); 3 | } 4 | -------------------------------------------------------------------------------- /tests/ft/exprs/type.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | a: bool; 3 | } 4 | -------------------------------------------------------------------------------- /tests/ft/exprs/unary_op.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | !a; 3 | } 4 | -------------------------------------------------------------------------------- /tests/ft/exprs/while.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | 'label: while true {} 3 | } 4 | -------------------------------------------------------------------------------- /tests/ft/exprs/while_let.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | while let a | b | c = d {} 3 | } 4 | -------------------------------------------------------------------------------- /tests/ft/fn.rs: -------------------------------------------------------------------------------- 1 | pub fn f() {} 2 | pub fn f() -> !{} 3 | 4 | pub unsafe extern "C" fn fff((a, b): (bool, i32)) -> Result where T: Iterator {} 5 | 6 | mod a { 7 | pub unsafe extern "C" fn fff((a, b): (bool, i32)) -> Result where T: Iterator {} 8 | } 9 | 10 | fn f(a: bool, 11 | b: i32, 12 | c: String) { 13 | let a = true; 14 | } 15 | 16 | fn ffff(){} 17 | -------------------------------------------------------------------------------- /tests/ft/foreign.rs: -------------------------------------------------------------------------------- 1 | extern {} 2 | 3 | extern { 4 | static mut a: bool; // a 5 | pub fn f(a: bool) -> i32 where T: bool; // b 6 | pub fn ff(a: bool, ...) -> i32; // c 7 | // end 8 | } // aaaaaaaaaaa 9 | 10 | extern "Rust" { 11 | static a: bool; 12 | } 13 | -------------------------------------------------------------------------------- /tests/ft/generics.rs: -------------------------------------------------------------------------------- 1 | type a = Option< T >; 2 | 3 | type a<'a, 'b, 4 | 'c: 'a + 'b, 5 | T: 'a + for<'b> ::iter::Iterator, U, V, R = Result> where 'b: 'a, U: Eq, V: Fn(bool, i32) -> String = Option<'a, T, U = Result>; 6 | 7 | type a where T: Fn(bool, str, isize, usize, i32, u32) -> u32 = Option<'a, T, U = Result,>; 8 | type a u32> = Option<'a, T, U = Result,>; 9 | type a<'a, 'b> where 'a: 'b = Option; 10 | //type a where for<'a: 'b> = Option; 11 | type a where for<'a: 'b> T: Iterator + 'a = Option; 12 | -------------------------------------------------------------------------------- /tests/ft/impl.rs: -------------------------------------------------------------------------------- 1 | impl A{} 2 | impl !A for B{} 3 | pub unsafe impl A for B{} 4 | impl A for B 5 | where T: 'static + Iterator + Option { 6 | const a: bool = true; // a 7 | type E = T; // b 8 | fn f(&self) { 9 | println!("hello world"); // c 10 | } // d 11 | a!(ABCD); // e 12 | } 13 | -------------------------------------------------------------------------------- /tests/ft/impl_default.rs: -------------------------------------------------------------------------------- 1 | pub unsafe impl !Shape for .. {} 2 | pub unsafe impl Shape for .. {} 3 | -------------------------------------------------------------------------------- /tests/ft/items/const.rs: -------------------------------------------------------------------------------- 1 | const a: bool = true; 2 | const b: &'static str = r#"aaaaaaaaaaa"#; 3 | pub const c: i32 = -12_345; 4 | -------------------------------------------------------------------------------- /tests/ft/items/enum.rs: -------------------------------------------------------------------------------- 1 | enum A { 2 | Dog = 10, 3 | Cat, 4 | } 5 | 6 | struct BA; 7 | 8 | struct BB; 9 | 10 | enum B { 11 | A(BA), 12 | B(BB), 13 | } 14 | 15 | enum C { 16 | CA { 17 | a: bool, 18 | }, 19 | CB { 20 | b: i32, 21 | }, 22 | } 23 | -------------------------------------------------------------------------------- /tests/ft/items/existential.rs: -------------------------------------------------------------------------------- 1 | existential type Foo: Bar + Boo; 2 | -------------------------------------------------------------------------------- /tests/ft/items/extern_crate.rs: -------------------------------------------------------------------------------- 1 | extern crate aa as _; 2 | 3 | extern crate cc as dd; 4 | extern crate bb; 5 | -------------------------------------------------------------------------------- /tests/ft/items/fn.rs: -------------------------------------------------------------------------------- 1 | async unsafe fn f() {} 2 | 3 | const fn f() {} 4 | 5 | extern "C" fn f() {} 6 | 7 | pub fn fmt(krate: Crate, leading_cmnts: HashMap>, 8 | trailing_cmnts: HashMap) -> rfmt::Result where T: bool { 9 | Formatter::new(leading_cmnts, trailing_cmnts).fmt_crate(krate) 10 | } 11 | 12 | fn f(a: _) {} 13 | -------------------------------------------------------------------------------- /tests/ft/items/foreign.rs: -------------------------------------------------------------------------------- 1 | extern { type bool; } 2 | 3 | //extern { a!(true); } 4 | 5 | extern "C" { 6 | static a: bool; 7 | pub fn f(a: bool) -> i32 where T: bool; 8 | } 9 | 10 | extern "Rust" { 11 | static mut a: bool; 12 | } 13 | -------------------------------------------------------------------------------- /tests/ft/items/group_items.rs: -------------------------------------------------------------------------------- 1 | extern crate aa as _; 2 | 3 | extern crate cc as dd; 4 | extern crate bb; 5 | 6 | use *; 7 | use ::*; 8 | use ::f; 9 | use a::b::{c}; 10 | use a::b::c as d; 11 | use a::b::{c, d, e::f, g::h::i}; 12 | use a::b::{self, c, d::e}; 13 | use a::b::{self as ab, c as abc}; 14 | use a::b::*; 15 | use a::b::{ 16 | self as ab, c, 17 | d::{*, e::f}, 18 | }; 19 | use p::q::r as x; 20 | 21 | use crate::aa as x; 22 | 23 | mod a; 24 | pub mod c; 25 | mod b; 26 | 27 | mod aa {} 28 | 29 | pub mod b; 30 | 31 | mod c {} 32 | 33 | -------------------------------------------------------------------------------- /tests/ft/items/impl.rs: -------------------------------------------------------------------------------- 1 | default impl A {} 2 | 3 | unsafe impl !A {} 4 | 5 | impl A for B {} 6 | 7 | impl A for B { 8 | default const a: bool = true; 9 | type E where T: bool = T; 10 | existential type Item: Debug; 11 | fn f(&self) {} 12 | fn f(&'a mut self) {} 13 | fn f(self: bool) {} 14 | a!(true); 15 | } 16 | 17 | unsafe impl !A {} 18 | -------------------------------------------------------------------------------- /tests/ft/items/in_mod.rs: -------------------------------------------------------------------------------- 1 | mod a { 2 | extern crate aa as _; 3 | 4 | 5 | 6 | extern crate cc as dd; 7 | extern crate bb; 8 | 9 | 10 | use *; 11 | use ::*; 12 | use ::f; 13 | use a::b::{c}; 14 | use a::b::c as d; 15 | use a::b::{c, d, e::f, g::h::i}; 16 | use a::b::{self, c, d::e}; 17 | use a::b::{self as ab, c as abc}; 18 | use a::b::*; 19 | use a::b::{ 20 | self as ab, c, 21 | d::{*, e::f}, 22 | }; 23 | use p::q::r as x; 24 | 25 | 26 | use crate::aa as x; 27 | 28 | 29 | mod d; 30 | 31 | mod a; 32 | pub mod c; 33 | mod b; 34 | 35 | 36 | } 37 | -------------------------------------------------------------------------------- /tests/ft/items/mod_decl.rs: -------------------------------------------------------------------------------- 1 | mod a; 2 | pub mod c; 3 | mod b; 4 | 5 | mod aa {} 6 | 7 | pub mod b; 8 | 9 | mod c {} 10 | 11 | -------------------------------------------------------------------------------- /tests/ft/items/mod_nest.rs: -------------------------------------------------------------------------------- 1 | // a 2 | #[test] 3 | pub mod a; // a 4 | 5 | pub mod b; // b 6 | 7 | mod c { 8 | /// bbbbbbbbbbbbbbbbbbbbbb 9 | // abc 10 | #[test] 11 | mod e; 12 | mod d; 13 | mod g; // g 14 | 15 | type a = bool; // ca 16 | // zzzzzzzzzzzzzzzz 17 | } // c 18 | 19 | mod e { 20 | // eeeeeeeeee 21 | } 22 | 23 | mod f {} // aaaaa 24 | 25 | // bbbbb 26 | // ccccc 27 | -------------------------------------------------------------------------------- /tests/ft/items/static.rs: -------------------------------------------------------------------------------- 1 | static a: bool = true; 2 | pub static mut b: i32 = 0; 3 | -------------------------------------------------------------------------------- /tests/ft/items/struct.rs: -------------------------------------------------------------------------------- 1 | struct A; 2 | 3 | struct B {} 4 | 5 | struct Tuple(bool, i32); 6 | 7 | pub struct Point where T : bool { 8 | #[test] 9 | pub x: i32, // a 10 | y: i32, 11 | } 12 | 13 | -------------------------------------------------------------------------------- /tests/ft/items/trait.rs: -------------------------------------------------------------------------------- 1 | auto trait IsCool {} 2 | 3 | unsafe trait Trait: A + B + 'static + Sized where T: bool {} 4 | 5 | pub trait Trait { 6 | const a: bool = true; 7 | type E: Iterator + 'static where T: bool = bool; 8 | unsafe fn f(); 9 | const fn f(&self); 10 | fn ff(&self); 11 | fn f(&'a self); 12 | fn f(self: Iterator); 13 | fn f(&self) where T: bool; 14 | a!(true); 15 | } 16 | -------------------------------------------------------------------------------- /tests/ft/items/trait_alias.rs: -------------------------------------------------------------------------------- 1 | trait A = B; 2 | trait A = B + C; 3 | trait B = Result + Iterator + 'static + Sized; 4 | trait C = for < 'a, 'b: 'a > Foo< & 'a Bar>; 5 | -------------------------------------------------------------------------------- /tests/ft/items/type_alias.rs: -------------------------------------------------------------------------------- 1 | type a = bool; 2 | type a = result::Result; 3 | type a = <::i32 as Vec>::MAX; 4 | type a = [bool]; 5 | type a = [[bool]]; 6 | type a = [bool; 8]; 7 | type a = *const bool; 8 | type a = *mut bool; 9 | type a = &bool; 10 | type a = &'a mut bool; 11 | type a = &[bool]; 12 | type a = &'a bool; 13 | type a = (); 14 | type a = (bool); 15 | type a = (bool, usize); 16 | type a = _; 17 | type a where T: Iterator = Option; 18 | type a = dyn Result + Iterator + 'static + Sized; 19 | type a = impl Result + Iterator + 'static + Sized; 20 | type a = for<'a, 'b: 'a> Foo<&'a Bar>; 21 | type a = unsafe extern "C" fn(bool) -> usize; 22 | //type a = a!("a"); 23 | -------------------------------------------------------------------------------- /tests/ft/items/union.rs: -------------------------------------------------------------------------------- 1 | union A {} 2 | 3 | pub union Point { 4 | pub x: i32, 5 | y: i32, 6 | } 7 | 8 | -------------------------------------------------------------------------------- /tests/ft/items/use.rs: -------------------------------------------------------------------------------- 1 | use *; 2 | use ::*; 3 | use ::f; 4 | use a::b::{c}; 5 | use a::b::c as d; 6 | use a::b::{c, d, e::f, g::h::i}; 7 | use a::b::{self, c, d::e}; 8 | use a::b::{self as ab, c as abc}; 9 | use a::b::*; 10 | use a::b::{ 11 | self as ab, c, 12 | d::{*, e::f}, 13 | }; 14 | use p::q::r as x; 15 | 16 | use crate::aa as x; 17 | 18 | -------------------------------------------------------------------------------- /tests/ft/macro.rs: -------------------------------------------------------------------------------- 1 | macro_rules! a { () => (); } 2 | 3 | macro_rules! m { 4 | (1) => {}; 5 | } 6 | 7 | macro_rules! ambiguity { 8 | ($($i:ident)* $j:ident) => {}; 9 | } 10 | 11 | macro_rules! d { 12 | ($arg:expr) => ({println!("{:#?}", $arg)}); 13 | } 14 | 15 | macro_rules! p { 16 | () => ({println!()}); 17 | ($arg:expr) => ({println!("{}", $arg)}); 18 | ($fmt:expr, $($arg:tt)*) => ({println!($fmt, $($arg)*)}); 19 | ($($arg:tt)+) => ({println!("{}", $($arg)+)}); 20 | } 21 | 22 | 23 | a!(); 24 | a![]; 25 | a!{} 26 | println!("{}"); 27 | println!(a, b); 28 | maybe_wrap!( self, " ", "", expr, fmt_expr); 29 | -------------------------------------------------------------------------------- /tests/ft/match.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | _ => true, // aaa 4 | true | false if a > 0 => true, // bbb 5 | 1...3 => true, 6 | a @ 1...5 => true, 7 | 8 | a => true, 9 | 10 | ref mut a => true, 11 | 12 | &mut a => true, 13 | 14 | 15 | Some(a, ref b) => true, 16 | Some(..) => true, 17 | Some(_) => true, 18 | rst::Abcdefg => false, 19 | 20 | ::CONST => true, 21 | 22 | A {} => false, 23 | B { x: bool, // zzzzzzzzzzzzzzz 24 | y : B { y1, // yyyyyyyyyyyyyyyy 25 | y2 }, ref mut z, .. // xxxxxxxxx 26 | } => true, 27 | 28 | [a, b, .., d, e] => true, 29 | [.., a] => true, 30 | [a, ..] => true, 31 | [..] => true, 32 | 33 | () => false, 34 | (a, b) => true, 35 | 36 | box a => true, 37 | 38 | 39 | 1 => true, 40 | 41 | 42 | 43 | a!() => true, 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /tests/ft/mod_nest.rs: -------------------------------------------------------------------------------- 1 | // a 2 | #[test] 3 | pub mod a; // a 4 | 5 | pub mod b; // b 6 | 7 | mod c { 8 | /// bbbbbbbbbbbbbbbbbbbbbb 9 | // abc 10 | #[test] 11 | mod e; 12 | mod d; 13 | mod g; // g 14 | 15 | type a = bool; // ca 16 | // zzzzzzzzzzzzzzzz 17 | } // c 18 | 19 | mod e { 20 | // eeeeeeeeee 21 | } 22 | 23 | mod f {} // aaaaa 24 | 25 | // bbbbb 26 | // ccccc 27 | -------------------------------------------------------------------------------- /tests/ft/pattens/enum.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | Some(b) => true, 4 | Some(..) => true, 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /tests/ft/pattens/ident.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | ref mut a @ 1...5 => true, 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /tests/ft/pattens/literal.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | 1 => true 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /tests/ft/pattens/macro.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | a!() => true 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /tests/ft/pattens/paren.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | &(0..=5) => (), 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /tests/ft/pattens/path.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | ::CONST => true 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /tests/ft/pattens/patten.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | _ => true, 4 | 5 | 1..=3 => true, 6 | 1..2 => true, 7 | 8 | &mut a => true, 9 | 10 | ::CONST => true, 11 | a::CONST => true, 12 | 13 | a => true, 14 | ref mut a => true, 15 | a @ 1...5 => true, 16 | 17 | A { x: bool, y : B { y1, y2 }, ref mut z, .. } => true, 18 | 19 | Some => true, 20 | Some(b) => true, 21 | Some(x, y) => true, 22 | Some(.., a) => true, 23 | Some(a, .., b) => true, 24 | Some(a, ..) => true, 25 | 26 | (a) => true, 27 | (a, b) => true, 28 | (a, .., b) => true, 29 | (.., b) => true, 30 | (a, ..) => true, 31 | 32 | [a, b, c] => true, 33 | [a, i.., e] => true, 34 | [a, b, .., d, e] => true, 35 | [..] => true, 36 | 37 | //a!() => true 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tests/ft/pattens/range.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | 0..1 => true, 4 | 0...1 => true, 5 | 0..=1 => true, 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /tests/ft/pattens/ref.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | &mut 1 => true 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /tests/ft/pattens/slice.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | [a, b, .., _, e] => true, 4 | [..] => true, 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /tests/ft/pattens/struct.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | A { x: bool, y: B { y1, y2 }, ref mut z, .. } => true 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /tests/ft/pattens/tuple.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | (0, .., 1) => true, 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /tests/ft/pattens/wildcard.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | _ => {} 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /tests/ft/static.rs: -------------------------------------------------------------------------------- 1 | static a: bool = true; 2 | pub static mut b: i32 = 0; 3 | -------------------------------------------------------------------------------- /tests/ft/stmt.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | const a: bool = true; 3 | let b = a; 4 | //b = a; 5 | /* 6 | a!(0); 7 | a![1]; 8 | a! {2}; 9 | a! {2} 10 | */ 11 | //a!(0) 12 | true 13 | } 14 | -------------------------------------------------------------------------------- /tests/ft/struct.rs: -------------------------------------------------------------------------------- 1 | pub struct Point { 2 | pub x: i32, // a 3 | 4 | // b 5 | y: i32, // c 6 | } // aaaaaaaaaaa 7 | struct A(bool, pub i32, str, 8 | String, 9 | [[bool]]); 10 | struct A; 11 | struct B{} 12 | struct B(); 13 | -------------------------------------------------------------------------------- /tests/ft/trait.rs: -------------------------------------------------------------------------------- 1 | pub unsafe trait Trait: A + B + 'static + Sized { } // aaaaa 2 | 3 | pub trait Trait { 4 | // aaaaaa 5 | const a: bool; // bbbbb 6 | type E: Iterator + 'static + Option = B; 7 | type A = Result; 8 | unsafe fn f(); 9 | const fn f(self, a: bool); 10 | fn f(&self); 11 | fn f(&'a self); 12 | fn f(self: Iterator); // cccccc 13 | } // aaaaaaaaaaaa 14 | 15 | // a 16 | // b 17 | -------------------------------------------------------------------------------- /tests/ft/ts/blank_lines.rs: -------------------------------------------------------------------------------- 1 | 2 | const a: bool = true; 3 | 4 | const a: bool = true; 5 | 6 | 7 | const a: bool = true; 8 | 9 | 10 | 11 | const a: bool = true; 12 | 13 | 14 | // aaaaaaa 15 | 16 | 17 | 18 | const a: bool = true; 19 | -------------------------------------------------------------------------------- /tests/ft/types/array.rs: -------------------------------------------------------------------------------- 1 | type a = [bool; 8]; 2 | -------------------------------------------------------------------------------- /tests/ft/types/bare_fn.rs: -------------------------------------------------------------------------------- 1 | type a = for<'a, 'b: 'a> unsafe extern "C" fn(T, bool, ...) -> usize; 2 | -------------------------------------------------------------------------------- /tests/ft/types/impl_trait.rs: -------------------------------------------------------------------------------- 1 | type a = impl Trait; 2 | type b = impl Trait + Send; 3 | type c = impl Trait + Send + Sync; 4 | type d = impl Trait + 'static; 5 | type e = impl Trait + Send + 'static; 6 | type g = impl 'static + Trait; 7 | type h = impl (Trait); 8 | -------------------------------------------------------------------------------- /tests/ft/types/macro.rs: -------------------------------------------------------------------------------- 1 | type a = a!("a"); 2 | -------------------------------------------------------------------------------- /tests/ft/types/path.rs: -------------------------------------------------------------------------------- 1 | type a = bool; 2 | type b = str; 3 | type c = result::Result; 4 | type d = ::result::Result; 5 | type e = <::i32 as Vec>::MAX; 6 | -------------------------------------------------------------------------------- /tests/ft/types/ptr.rs: -------------------------------------------------------------------------------- 1 | type a = *const bool; 2 | type b = *mut bool; 3 | -------------------------------------------------------------------------------- /tests/ft/types/ref.rs: -------------------------------------------------------------------------------- 1 | type a = &bool; 2 | type b = &'a mut bool; 3 | type c = &'a bool; 4 | -------------------------------------------------------------------------------- /tests/ft/types/slice.rs: -------------------------------------------------------------------------------- 1 | type a = [bool]; 2 | type b = [[bool]]; 3 | -------------------------------------------------------------------------------- /tests/ft/types/symbol.rs: -------------------------------------------------------------------------------- 1 | type a = _; 2 | type a = !; 3 | -------------------------------------------------------------------------------- /tests/ft/types/trait_object.rs: -------------------------------------------------------------------------------- 1 | type a = Trait; 2 | type b = dyn Trait + Send; 3 | type c = dyn Trait + Send + Sync; 4 | type d = dyn Trait + 'static; 5 | type e = dyn Trait + Send + 'static; 6 | type g = dyn 'static + Trait; 7 | type h = dyn (Trait); 8 | -------------------------------------------------------------------------------- /tests/ft/types/tuple.rs: -------------------------------------------------------------------------------- 1 | type a = (); 2 | type b = (bool); 3 | type c = (bool, usize); 4 | -------------------------------------------------------------------------------- /tests/ft/types/type_alias.rs: -------------------------------------------------------------------------------- 1 | type a = bool; 2 | type a = result::Result; 3 | type a = <::i32>::MAX; 4 | type a = <::i32 as a::b::Vec>::c::d::MAX; 5 | 6 | type a = *const bool; 7 | type a = *mut bool; 8 | type a = &bool; 9 | type a = &'a mut bool; 10 | 11 | type a = [bool]; 12 | type a = [[bool]]; 13 | type a = [bool; 8]; 14 | type a = [usize; 15 | 8]; 16 | 17 | type a = &[bool]; 18 | type a = &'a bool; 19 | 20 | type a = (); 21 | type a = (bool); 22 | type a = (bool, usize); 23 | type a = (bool, usize, isize, String, Vec, str); 24 | 25 | //type a = for<'a> unsafe extern "C" fn(bool) -> usize; 26 | type a = for<'a> extern "Rust" fn(bool) -> usize; 27 | type a = for<'a> unsafe extern "C" fn(bool) -> usize; 28 | type a = for<'a> fn(bool) -> usize; 29 | 30 | type a = Result + Iterator + 'static + Sized; 31 | 32 | type a = for<'a, 'b: 'a> Foo<&'a Bar>; 33 | 34 | type a = a!("a"); 35 | 36 | type a = _; // a 37 | 38 | 39 | // b 40 | -------------------------------------------------------------------------------- /tests/ir/attrs/attr.rs: -------------------------------------------------------------------------------- 1 | #![allow(unused_variables)] 2 | #![crate_type = "lib"] 3 | 4 | fn main() { 5 | // General metadata applied to the enclosing module or crate. 6 | #![crate_type = "lib"] 7 | 8 | // A function marked as a unit test 9 | #[test] 10 | fn test_foo() { 11 | /* ... */ 12 | } 13 | 14 | // A conditionally-compiled module 15 | #[cfg(target_os = "linux")] 16 | mod bar { 17 | /* ... */ 18 | } 19 | 20 | // A lint attribute used to suppress a warning/error 21 | #[allow(non_camel_case_types)] 22 | type int8_t = i8; 23 | 24 | // Inner attribute applies to the entire function. 25 | fn some_unused_variables() { 26 | #![allow(unused_variables)] 27 | 28 | let x = (); 29 | let y = (); 30 | let z = (); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/ir/attrs/derive.rs: -------------------------------------------------------------------------------- 1 | #[derive(Display, Clone, Copy, Debug, EnumString, PartialEq)] 2 | pub enum A {} 3 | -------------------------------------------------------------------------------- /tests/ir/attrs/doc.rs: -------------------------------------------------------------------------------- 1 | #![doc(html_favicon_url = "https://example.com/favicon.ico")] 2 | //!aaaaaaaaaaaaaa 3 | //!bbbbbbbbbbbbbb 4 | /*!cccccccccc 5 | * ddddddddd 6 | !*/ 7 | //! 8 | 9 | /// aaaaaaaaaaa 10 | /// bbbbbbbbbb 11 | /** cccccccccc **/ 12 | const a: bool = true; 13 | 14 | // abcde 15 | /* abcde */ const b: bool = true; 16 | /* aaaaaaa 17 | * 18 | * aaaaaa 19 | */ 20 | 21 | #[doc = " This is a doc comment."] 22 | const c: bool = true; 23 | -------------------------------------------------------------------------------- /tests/ir/attrs/doc_comment.rs: -------------------------------------------------------------------------------- 1 | #![allow(unused_variables)] 2 | 3 | fn main() { 4 | //! A doc comment that applies to the implicit anonymous module of this crate 5 | 6 | pub mod outer_module { 7 | //! - Inner line doc 8 | //!! - Still an inner line doc (but with a bang at the beginning) 9 | 10 | /*! - Inner block doc */ 11 | /*!! - Still an inner block doc (but with a bang at the beginning) */ 12 | 13 | // - Only a comment 14 | /// - Outer line doc (exactly 3 slashes) 15 | //// - Only a comment 16 | 17 | /* - Only a comment */ 18 | /** - Outer block doc (exactly) 2 asterisks */ 19 | /*** - Only a comment */ 20 | 21 | pub mod inner_module {} 22 | 23 | pub mod nested_comments { 24 | /* In Rust /* we can /* nest comments */ */ */ 25 | 26 | // All three types of block comments can contain or be nested inside 27 | // any other type: 28 | 29 | /* /* */ /** */ /*! */ */ 30 | /*! /* */ /** */ /*! */ */ 31 | /** /* */ /** */ /*! */ */ 32 | pub mod dummy_item {} 33 | } 34 | 35 | pub mod degenerate_cases { 36 | // empty inner line doc 37 | //! 38 | 39 | // empty inner block doc 40 | /*!*/ 41 | 42 | // empty line comment 43 | // 44 | 45 | // empty outer line doc 46 | /// 47 | 48 | // empty block comment 49 | /**/ 50 | 51 | pub mod dummy_item {} 52 | 53 | // empty 2-asterisk block isn't a doc block, it is a block comment 54 | /***/ 55 | } 56 | 57 | /* The next one isn't allowed because outer doc comments 58 | require an item that will receive the doc */ 59 | 60 | /// Where is my item? 61 | mod boo {} 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /tests/ir/attrs/inner.rs: -------------------------------------------------------------------------------- 1 | #![inline] 2 | type a = bool; 3 | -------------------------------------------------------------------------------- /tests/ir/attrs/meta.rs: -------------------------------------------------------------------------------- 1 | #![macro_use(foo, bar)] 2 | #![allow(unused, clippy::inline_always)] 3 | #![link(name = "CoreFoundation", kind = "framework")] 4 | #![doc = "example"] 5 | #![clippy::inline_always] 6 | #![no_std] 7 | 8 | #![cfg(all(unix, 9 | target_pointer_width = "32", 10 | /* aaa */ b = "a"))] 11 | -------------------------------------------------------------------------------- /tests/ir/attrs/meta_list.rs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | #![cfg(all(unix, 6 | target_pointer_width = "32", 7 | /* aaa */ b = "a"))] 8 | -------------------------------------------------------------------------------- /tests/ir/attrs/meta_list_idents.rs: -------------------------------------------------------------------------------- 1 | #![macro_use(foo, bar)] 2 | -------------------------------------------------------------------------------- /tests/ir/attrs/meta_list_name_value_str.rs: -------------------------------------------------------------------------------- 1 | #![link(name = "CoreFoundation", kind = "framework")] 2 | -------------------------------------------------------------------------------- /tests/ir/attrs/meta_list_paths.rs: -------------------------------------------------------------------------------- 1 | #![allow(unused, clippy::inline_always)] 2 | -------------------------------------------------------------------------------- /tests/ir/attrs/meta_name_value_str.rs: -------------------------------------------------------------------------------- 1 | #![doc = "example"] 2 | -------------------------------------------------------------------------------- /tests/ir/attrs/meta_path.rs: -------------------------------------------------------------------------------- 1 | #![clippy::inline_always] 2 | -------------------------------------------------------------------------------- /tests/ir/attrs/meta_word.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | -------------------------------------------------------------------------------- /tests/ir/attrs/outer.rs: -------------------------------------------------------------------------------- 1 | #[inline] 2 | type a = bool; 3 | -------------------------------------------------------------------------------- /tests/ir/attrs/sugared_doc.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT 2 | // file at the top-level directory of this distribution and at 3 | // http://rust-lang.org/COPYRIGHT. 4 | // 5 | // Licensed under the Apache License, Version 2.0 or the MIT license 7 | // , at your 8 | // option. This file may not be copied, modified, or distributed 9 | // except according to those terms. 10 | 11 | //! # The Rust Standard Library 12 | #![test] 13 | 14 | /// # The Rust Standard Library 15 | /// 16 | /// 17 | 18 | fn f() {} 19 | -------------------------------------------------------------------------------- /tests/ir/attrs/tool_attr.rs: -------------------------------------------------------------------------------- 1 | #![allow(unused_variables)] 2 | 3 | fn main() { 4 | // Tells the rustfmt tool to not format the following element. 5 | #[rustfmt::skip] 6 | struct S {} 7 | 8 | // Controls the "cyclomatic complexity" threshold for the clippy tool. 9 | #[clippy::cyclomatic_complexity = "100"] 10 | pub fn f() {} 11 | } 12 | -------------------------------------------------------------------------------- /tests/ir/comments/comment.rs: -------------------------------------------------------------------------------- 1 | /// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 2 | /// bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb 3 | 4 | // aaaaa 5 | mod a { 6 | /* ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc */ 7 | struct a { 8 | a: i32, 9 | // dddddddddddddddd 10 | c: /*aaa*/ i32, 11 | b: bool, // eeeeeeeeeeeeeeeeeeeeee 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/ir/exprs/array.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | [true, 1, "a"]; 3 | } 4 | -------------------------------------------------------------------------------- /tests/ir/exprs/async.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | let a = async || {}; 3 | } 4 | -------------------------------------------------------------------------------- /tests/ir/exprs/block.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | 'label: {} 3 | } 4 | -------------------------------------------------------------------------------- /tests/ir/exprs/box.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | box a; 3 | } 4 | -------------------------------------------------------------------------------- /tests/ir/exprs/break.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | 'label: for a in b { 3 | break 'label; 4 | } 5 | 6 | let a = for b in c { 7 | break 0; 8 | }; 9 | } 10 | -------------------------------------------------------------------------------- /tests/ir/exprs/cast.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | a as bool; 3 | } 4 | -------------------------------------------------------------------------------- /tests/ir/exprs/closure.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | static move |a: bool| -> bool { true }; 3 | let add = |x, y| x + y; 4 | trees.sort_by(|a, b| { 5 | if a.path.starts_with("self") { 6 | Ordering::Less 7 | } else if b.path.starts_with("self") { 8 | Ordering::Greater 9 | } else { 10 | a.path.cmp(&b.path) 11 | } 12 | }); 13 | } 14 | -------------------------------------------------------------------------------- /tests/ir/exprs/continue.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | 'label: for a in b { 3 | continue 'label; 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /tests/ir/exprs/expr.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | //box a 3 | 4 | //a <- b 5 | 6 | //[0, true, "a"] 7 | //(0, bool) 8 | 9 | //ff(0, bool) 10 | 11 | //a.f::(0, bool) 12 | 13 | //a + b 14 | 15 | // !a 16 | 17 | //true 18 | 19 | //a as bool 20 | //a: bool 21 | 22 | //{} 23 | //if true {} else if false {} else {} 24 | //if let a = b {} else {} 25 | 26 | /* 27 | 'label: 28 | while true { 29 | } 30 | */ 31 | 32 | //while let a = b {} 33 | 34 | /* 35 | 'label: 36 | for a in b {} 37 | */ 38 | 39 | //loop {} 40 | 41 | /* 42 | match a { 43 | aa if y => true, 44 | bb | cc => false, 45 | } 46 | */ 47 | 48 | //move |a| -> bool {a} 49 | 50 | //a = bool 51 | //a += 1 52 | 53 | //a.b; 54 | //a.0 55 | 56 | //a[0] 57 | 58 | //1..2 59 | //..2 60 | //1.. 61 | //1...2 62 | 63 | //::a::b 64 | // as SomeTrait>::SomeType 65 | 66 | //&a 67 | 68 | // break 'label; 69 | // continue 'label; 70 | 71 | //return; 72 | //return true; 73 | 74 | //a!() 75 | 76 | //A { a: true, ..Default::default() } 77 | //A::A(B) 78 | 79 | //[0; 8] 80 | //(0) 81 | 82 | f()? 83 | } 84 | -------------------------------------------------------------------------------- /tests/ir/exprs/field.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | a.a; 3 | a.0; 4 | } 5 | -------------------------------------------------------------------------------- /tests/ir/exprs/fn_call.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | ff(0, bool); 3 | Position(0, 0, 0); 4 | } 5 | -------------------------------------------------------------------------------- /tests/ir/exprs/for.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | 'label: for a in b {} 3 | } 4 | -------------------------------------------------------------------------------- /tests/ir/exprs/if.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | if true {} else if false {} else {} 3 | let span_forward = if is_inner { 2 } else { 1 }; 4 | } 5 | -------------------------------------------------------------------------------- /tests/ir/exprs/if_let.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | if let a | b | c = d {} else {} 3 | } 4 | -------------------------------------------------------------------------------- /tests/ir/exprs/index.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | a = b[1]; 3 | } 4 | -------------------------------------------------------------------------------- /tests/ir/exprs/list_op.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | a + b; 3 | a = bool; 4 | a += 1; 5 | a + b + c; 6 | } 7 | -------------------------------------------------------------------------------- /tests/ir/exprs/literal.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | true 3 | } 4 | -------------------------------------------------------------------------------- /tests/ir/exprs/loop.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | 'label: loop {} 3 | } 4 | -------------------------------------------------------------------------------- /tests/ir/exprs/macro.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | a!() 3 | } 4 | -------------------------------------------------------------------------------- /tests/ir/exprs/match.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | aa if y => true, 4 | bb | cc => false, 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /tests/ir/exprs/method_call.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | a.f::(0, bool); 3 | a.b.c(0); 4 | } 5 | -------------------------------------------------------------------------------- /tests/ir/exprs/path.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | a::b; 3 | <::i32 as Vec>::MAX; 4 | } 5 | -------------------------------------------------------------------------------- /tests/ir/exprs/range.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | 1..2; 3 | 1..; 4 | ..2; 5 | 1..=2; 6 | ..=2; 7 | } 8 | -------------------------------------------------------------------------------- /tests/ir/exprs/ref.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | &a; 3 | &mut b; 4 | } 5 | -------------------------------------------------------------------------------- /tests/ir/exprs/repeat.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | [true; 10]; 3 | } 4 | -------------------------------------------------------------------------------- /tests/ir/exprs/return.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | return bool; 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/ir/exprs/struct.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | NothingInMe {}; 3 | Point { x: 10.0, y: 20.0 }; 4 | TuplePoint { 0: 10.0, 1: 20.0 }; 5 | Point3d { y: 0, z: 10, ..base }; 6 | } 7 | -------------------------------------------------------------------------------- /tests/ir/exprs/try.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | a? 3 | } 4 | -------------------------------------------------------------------------------- /tests/ir/exprs/try_block.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | try {} 3 | } 4 | -------------------------------------------------------------------------------- /tests/ir/exprs/tuple.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | (true, 1, "a"); 3 | } 4 | -------------------------------------------------------------------------------- /tests/ir/exprs/type.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | a: bool; 3 | } 4 | -------------------------------------------------------------------------------- /tests/ir/exprs/unary_op.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | !a; 3 | } 4 | -------------------------------------------------------------------------------- /tests/ir/exprs/while.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | 'label: while true {} 3 | } 4 | -------------------------------------------------------------------------------- /tests/ir/exprs/while_let.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | while let a | b | c = d {} 3 | } 4 | -------------------------------------------------------------------------------- /tests/ir/generics/const-generics.rs: -------------------------------------------------------------------------------- 1 | impl Index for StaticVec { 2 | } 3 | -------------------------------------------------------------------------------- /tests/ir/generics/generics.rs: -------------------------------------------------------------------------------- 1 | type a<> = bool; 2 | type a<'a> = bool; 3 | type a = bool; 4 | type a<'a, 'b: 'a, 'c: 'a + 'b> = bool; 5 | type a<'a, T: 'a> = bool; 6 | type a = bool; 7 | type b = bool; 8 | 9 | type a ::iter::Iterator + Sized> = bool; 10 | //type a ()> = bool; 11 | type a = bool; 12 | -------------------------------------------------------------------------------- /tests/ir/generics/where.rs: -------------------------------------------------------------------------------- 1 | //type a<'a, T> where T: 'a = bool; 2 | //type a<'a, 'b, 'c> where 'b: 'a, 'c: 'a + 'b = bool; 3 | //type a where T: for<'a> ::iter::Iterator + Sized = bool; 4 | //type a where T: Fn(A, B) -> () = bool; 5 | //type a where T: Fn() -> () = bool; 6 | //type a<'a, 'b, T, U> where for<'a> T: Iterator + 'a, U: Option, 'a: 'b = bool; 7 | -------------------------------------------------------------------------------- /tests/ir/items/a.rs: -------------------------------------------------------------------------------- 1 | extern crate b; 2 | 3 | mod b; 4 | -------------------------------------------------------------------------------- /tests/ir/items/asm.rs: -------------------------------------------------------------------------------- 1 | fn foo() { 2 | unsafe { 3 | asm!("NOP"); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /tests/ir/items/b.rs: -------------------------------------------------------------------------------- 1 | mod a; 2 | -------------------------------------------------------------------------------- /tests/ir/items/block.rs: -------------------------------------------------------------------------------- 1 | fn f() -> bool { 2 | //let mut a: bool = true; 3 | a = false; 4 | a 5 | //fn ff() {} 6 | //true 7 | /* 8 | let a; 9 | a = true; 10 | if true {} else {} 11 | a!(); 12 | fn ff() {} 13 | unsafe {} 14 | true 15 | */ 16 | } 17 | -------------------------------------------------------------------------------- /tests/ir/items/const.rs: -------------------------------------------------------------------------------- 1 | const a: bool = true; 2 | const b: &'static str = r#"aaaaaaaaaaa"#; 3 | pub const c: i32 = -12_345; 4 | -------------------------------------------------------------------------------- /tests/ir/items/enum.rs: -------------------------------------------------------------------------------- 1 | enum A { 2 | Dog = 10, 3 | Cat, 4 | } 5 | 6 | struct BA; 7 | 8 | struct BB; 9 | 10 | enum B { 11 | A(BA), 12 | B(BB), 13 | } 14 | 15 | enum C { 16 | CA { 17 | a: bool, 18 | }, 19 | CB { 20 | b: i32, 21 | }, 22 | } 23 | -------------------------------------------------------------------------------- /tests/ir/items/existential.rs: -------------------------------------------------------------------------------- 1 | existential type Foo: Bar + Boo; 2 | -------------------------------------------------------------------------------- /tests/ir/items/extern_crate.rs: -------------------------------------------------------------------------------- 1 | extern crate aa as _; 2 | extern crate bb; 3 | extern crate cc as dd; 4 | -------------------------------------------------------------------------------- /tests/ir/items/fn.rs: -------------------------------------------------------------------------------- 1 | async unsafe fn f() {} 2 | 3 | const fn f() {} 4 | 5 | extern "C" fn f() {}; 6 | 7 | pub fn fmt(krate: Crate, leading_cmnts: HashMap>, 8 | trailing_cmnts: HashMap) -> rfmt::Result { 9 | Formatter::new(leading_cmnts, trailing_cmnts).fmt_crate(krate) 10 | } 11 | -------------------------------------------------------------------------------- /tests/ir/items/foreign.rs: -------------------------------------------------------------------------------- 1 | extern { type bool; } 2 | 3 | extern { a!(true); } 4 | 5 | extern "C" { 6 | static a: bool; 7 | pub fn f(a: bool) -> i32; 8 | } 9 | 10 | extern "Rust" { 11 | static mut a: bool; 12 | } 13 | -------------------------------------------------------------------------------- /tests/ir/items/impl.rs: -------------------------------------------------------------------------------- 1 | default impl A {} 2 | 3 | //unsafe impl !A {} 4 | 5 | impl A for B {} 6 | 7 | impl A for B { 8 | default const a: bool = true; 9 | type E = T; 10 | existential type Item: Debug; 11 | fn f(&self) {} 12 | a!(true); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /tests/ir/items/lifetime.rs: -------------------------------------------------------------------------------- 1 | struct A where 'a: 'b + 'c {} 2 | -------------------------------------------------------------------------------- /tests/ir/items/literal.rs: -------------------------------------------------------------------------------- 1 | //const a: bool = true; 2 | //const a: &'static str = r#"a"#; 3 | //const a: &[u8] = b"a"; 4 | //const a: &[u8] = br#"a"#; 5 | //const a: u8 = b'a'; 6 | //const a: char = 'a'; 7 | //const a: i8 = -10; 8 | //const a: u64 = 100_100; 9 | //const a: f32 = 0.01f32; 10 | //const a: f64 = 1e12; 11 | const a: bool = true; 12 | -------------------------------------------------------------------------------- /tests/ir/items/local.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | #[a] 3 | #[b] 4 | let mut a: bool = true; 5 | } 6 | -------------------------------------------------------------------------------- /tests/ir/items/mod.rs: -------------------------------------------------------------------------------- 1 | mod a; 2 | mod b; 3 | 4 | mod aa { 5 | const A: i32 = 0; 6 | } 7 | 8 | mod bb {} 9 | -------------------------------------------------------------------------------- /tests/ir/items/mod_decl.rs: -------------------------------------------------------------------------------- 1 | mod a; 2 | 3 | mod aa {} 4 | 5 | pub mod b; 6 | 7 | mod c {} 8 | 9 | -------------------------------------------------------------------------------- /tests/ir/items/only_comment.rs: -------------------------------------------------------------------------------- 1 | type a = bool; 2 | // aaaaa 3 | -------------------------------------------------------------------------------- /tests/ir/items/static.rs: -------------------------------------------------------------------------------- 1 | static a: bool = true; 2 | pub static mut b: i32 = 0; 3 | -------------------------------------------------------------------------------- /tests/ir/items/stmt.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | const a: bool = true; 3 | let b = a; 4 | b = a; 5 | a!(0); 6 | a![1]; 7 | a! {2}; 8 | a! {2} 9 | //a!(0) 10 | true 11 | } 12 | -------------------------------------------------------------------------------- /tests/ir/items/struct.rs: -------------------------------------------------------------------------------- 1 | struct A; 2 | 3 | struct B {} 4 | 5 | struct Tuple(bool, i32); 6 | 7 | pub struct Point { 8 | pub x: i32, 9 | y: i32, 10 | } 11 | 12 | -------------------------------------------------------------------------------- /tests/ir/items/trait.rs: -------------------------------------------------------------------------------- 1 | auto trait IsCool {} 2 | 3 | unsafe trait Trait: A + B + 'static + Sized {} 4 | 5 | pub trait Trait { 6 | const a: bool = true; 7 | type E: Iterator + 'static = bool; 8 | unsafe fn f(); 9 | const fn f(&self); 10 | fn f(&self); 11 | fn f(&'a self); 12 | fn f(self: Iterator); 13 | a!(true); 14 | } 15 | -------------------------------------------------------------------------------- /tests/ir/items/trait_alias.rs: -------------------------------------------------------------------------------- 1 | trait A = B; 2 | //trait A = B + C; 3 | //trait B = Result + Iterator + 'static + Sized; 4 | //trait C = for < 'a, 'b: 'a > Foo< & 'a Bar>; 5 | -------------------------------------------------------------------------------- /tests/ir/items/union.rs: -------------------------------------------------------------------------------- 1 | union A {} 2 | 3 | pub union Point { 4 | pub x: i32, 5 | y: i32, 6 | } 7 | 8 | -------------------------------------------------------------------------------- /tests/ir/items/use.rs: -------------------------------------------------------------------------------- 1 | use *; 2 | use ::*; 3 | use ::f; 4 | use a::b::{c, d, e::f, g::h::i}; 5 | use a::b::{self, c, d::e}; 6 | use a::b::{self as ab, c as abc}; 7 | use a::b::*; 8 | use a::b::{ 9 | self as ab, c, 10 | d::{*, e::f}, 11 | }; 12 | use p::q::r as x; 13 | 14 | use crate::aa as x; 15 | 16 | -------------------------------------------------------------------------------- /tests/ir/items/vis.rs: -------------------------------------------------------------------------------- 1 | pub mod outer_mod { 2 | pub mod inner_mod { 3 | pub(in crate::outer_mod) mod aa; 4 | pub(in outer_mod) mod bb; 5 | 6 | pub(crate) mod cc; 7 | pub(in crate) mod ccc; 8 | 9 | pub(super) mod dd; 10 | pub(in super) mod ddd; 11 | 12 | pub(self) mod ee; 13 | pub mod ff; 14 | crate mod gg; 15 | 16 | mod hh; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tests/ir/items/wrap.rs: -------------------------------------------------------------------------------- 1 | struct a { 2 | a: i8, 3 | b: u8, 4 | 5 | c: i16, 6 | } 7 | -------------------------------------------------------------------------------- /tests/ir/items/wrap_fn_arg.rs: -------------------------------------------------------------------------------- 1 | fn ff(a: i8, b: u8, 2 | c: i16) { 3 | } 4 | 5 | fn f() { 6 | ff(0 + 1 + 2, /* bbbbbbbbbbbbbb */1, 7 | // aaaaaaaaa 8 | 2); 9 | } 10 | -------------------------------------------------------------------------------- /tests/ir/macro/def.rs: -------------------------------------------------------------------------------- 1 | macro_rules! a { () => (); } 2 | 3 | macro_rules! m { 4 | (1) => {}; 5 | } 6 | 7 | macro_rules! ambiguity { 8 | ($($i:ident)* $j:ident) => {}; 9 | } 10 | 11 | macro_rules! d { 12 | ($arg:expr) => ({println!("{:#?}", $arg)}); 13 | } 14 | 15 | macro_rules! p { 16 | () => ({println!()}); 17 | ($arg:expr) => ({println!("{}", $arg)}); 18 | ($fmt:expr, $($arg:tt)*) => ({println!($fmt, $($arg)*)}); 19 | ($($arg:tt)+) => ({println!("{}", $($arg)+)}); 20 | } 21 | -------------------------------------------------------------------------------- /tests/ir/macro/invocation.rs: -------------------------------------------------------------------------------- 1 | a!(); 2 | a![]; 3 | a! {} 4 | println!("{}"); 5 | println!(a, b); 6 | maybe_wrap ! ( self, " ", "", expr, fmt_expr) 7 | -------------------------------------------------------------------------------- /tests/ir/macro/macro.rs: -------------------------------------------------------------------------------- 1 | #[test] 2 | macro_rules! a { 3 | () => () 4 | } 5 | 6 | // macro_rules! fmt_src { 7 | // ($src:tt, $($value:tt)*) => (format!(src!($src), $($value)*)); 8 | // } 9 | // 10 | 11 | // macro_rules! fmt_src { 12 | // ($src:tt, $($value:tt)*) => ("a") 13 | // } 14 | // 15 | 16 | // macro_rules! src { 17 | // (HEADER) => (r#" 18 | // pub const HEADER: Header = Header {{ 19 | // name: b"{}", 20 | // major: {}, 21 | // minor: {}, 22 | // revision: {}, 23 | // }}; 24 | // "#); 25 | // } 26 | // 27 | 28 | // fn f() -> bool { 29 | // a!{}; 30 | // } 31 | // 32 | 33 | fn f() { 34 | thread_local!(static THREAD_RNG_KEY: Rc> = { 35 | let r = match StdRng::new() { 36 | Ok(r) => r, 37 | Err(e) => panic!("could not initialize thread_rng: {}", e) 38 | }; 39 | let rng = reseeding::ReseedingRng::new(r, 40 | THREAD_RNG_RESEED_THRESHOLD, 41 | ThreadRngReseeder); 42 | Rc::new(RefCell::new(rng)) 43 | }); 44 | 45 | let a = Ok(); 46 | match a { 47 | Ok(_) => unreachable!(), 48 | Err(_) => (), 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /tests/ir/pattens/enum.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | Some(b) => true, 4 | Some(..) => true, 5 | Some(a, .., b) => true, 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /tests/ir/pattens/ident.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | ref mut a @ 1...5 => true, 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /tests/ir/pattens/literal.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | 1 => true 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /tests/ir/pattens/macro.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | a!() => true 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /tests/ir/pattens/paren.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | &(0..=5) => (), 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /tests/ir/pattens/path.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | ::CONST => true 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /tests/ir/pattens/patten.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | //_ => true 4 | 5 | //a => true 6 | 7 | //ref mut a => true 8 | 9 | //a @ 1...5 => true 10 | 11 | //Some(b) => true 12 | //Some(..) => true 13 | 14 | ::CONST => true 15 | //a::CONST => true 16 | 17 | //A { x: bool, y : B { y1, y2 }, ref mut z, .. } => true 18 | 19 | //(a, b) => true 20 | 21 | //box a => true 22 | 23 | //&mut a => true 24 | 25 | //1 => true 26 | 27 | //1...3 => true 28 | 29 | //[a, b, .., d, e] => true 30 | //[..] => true 31 | 32 | //a!() => true 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/ir/pattens/range.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | 0..1 => true, 4 | 0...1 => true, 5 | 0..=1 => true, 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /tests/ir/pattens/ref.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | &mut 1 => true 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /tests/ir/pattens/slice.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | [a, b, .., _, e] => true, 4 | [..] => true, 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /tests/ir/pattens/struct.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | A { x: bool, y: B { y1, y2 }, ref mut z, .. } => true, 4 | A { xxxxxxxxxxxxxxxxxxxx: bool, yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy: B { y1, y2, }, ref mut zzzzzzzzzzzzzzzzzzzzz, .. } => true, 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /tests/ir/pattens/tuple.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | (0, .., 1) => true, 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /tests/ir/pattens/wildcard.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | _ => {} 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /tests/ir/types/array.rs: -------------------------------------------------------------------------------- 1 | type a = [bool; 8]; 2 | -------------------------------------------------------------------------------- /tests/ir/types/bare_fn.rs: -------------------------------------------------------------------------------- 1 | type a = for<'a, 'b: 'a> unsafe extern "C" fn(T, bool, ...) -> usize; 2 | -------------------------------------------------------------------------------- /tests/ir/types/impl_trait.rs: -------------------------------------------------------------------------------- 1 | type a = impl Trait; 2 | type b = impl Trait + Send; 3 | type c = impl Trait + Send + Sync; 4 | type d = impl Trait + 'static; 5 | type e = impl Trait + Send + 'static; 6 | type g = impl 'static + Trait; 7 | type h = impl (Trait); 8 | -------------------------------------------------------------------------------- /tests/ir/types/infer.rs: -------------------------------------------------------------------------------- 1 | type a = _; 2 | -------------------------------------------------------------------------------- /tests/ir/types/macro.rs: -------------------------------------------------------------------------------- 1 | type a = a!("a"); 2 | -------------------------------------------------------------------------------- /tests/ir/types/never.rs: -------------------------------------------------------------------------------- 1 | type a = !; 2 | -------------------------------------------------------------------------------- /tests/ir/types/path.rs: -------------------------------------------------------------------------------- 1 | type a = bool; 2 | type b = str; 3 | type c = result::Result; 4 | type d = <::i32 as Vec>::MAX; 5 | -------------------------------------------------------------------------------- /tests/ir/types/ptr.rs: -------------------------------------------------------------------------------- 1 | type a = *const bool; 2 | type b = *mut bool; 3 | -------------------------------------------------------------------------------- /tests/ir/types/ref.rs: -------------------------------------------------------------------------------- 1 | type a = &bool; 2 | type b = &'a mut bool; 3 | type c = &'a bool; 4 | -------------------------------------------------------------------------------- /tests/ir/types/slice.rs: -------------------------------------------------------------------------------- 1 | type a = [bool]; 2 | type b = [[bool]]; 3 | -------------------------------------------------------------------------------- /tests/ir/types/trait_object.rs: -------------------------------------------------------------------------------- 1 | type a = Trait; 2 | type b = dyn Trait + Send; 3 | type c = dyn Trait + Send + Sync; 4 | type d = dyn Trait + 'static; 5 | type e = dyn Trait + Send + 'static; 6 | type g = dyn 'static + Trait; 7 | type h = dyn Trait; 8 | -------------------------------------------------------------------------------- /tests/ir/types/tuple.rs: -------------------------------------------------------------------------------- 1 | type a = (); 2 | type b = (bool); 3 | type c = (bool, usize); 4 | -------------------------------------------------------------------------------- /tests/ir/types/type_alias.rs: -------------------------------------------------------------------------------- 1 | //type a = bool; 2 | //type a = result::Result; 3 | //type a = <::i32 as Vec>::MAX; 4 | //type a = [bool]; 5 | //type a = [[bool]]; 6 | //type a = [bool; 8]; 7 | //type a = *const bool; 8 | //type a = *mut bool; 9 | //type a = &bool; 10 | //type a = &'a mut bool; 11 | //type a = &[bool]; 12 | //type a = &'a bool; 13 | //type a = (); 14 | //type a = (bool); 15 | //type a = (bool, usize); 16 | //type a = _; 17 | //type a = unsafe extern "C" fn(bool) -> usize; 18 | //type a = dyn Result + Iterator + 'static + Sized; 19 | //type a = impl Result + Iterator + 'static + Sized; 20 | //type a = for<'a, 'b: 'a> Foo<&'a Bar>; 21 | //type a = a!("a"); 22 | //type a where T: Iterator = Option; 23 | -------------------------------------------------------------------------------- /tests/issues/blank_line.rs: -------------------------------------------------------------------------------- 1 | extern crate a; 2 | extern crate b; 3 | 4 | extern crate c; 5 | use b; 6 | use c; 7 | 8 | 9 | 10 | use a; 11 | 12 | 13 | fn a() { 14 | } 15 | fn b() { 16 | } 17 | 18 | struct A { 19 | } 20 | struct B { 21 | } 22 | 23 | pub trait Trait { 24 | const a: bool = true; 25 | type E: Iterator + 'static = bool; 26 | unsafe fn f(); 27 | const fn f(&self); 28 | fn f(&self) {} 29 | fn f(&'a self); 30 | fn f(self: Iterator) {} 31 | fn f(self: Iterator) { true } 32 | fn f(self: Iterator) { true } 33 | fn f(self: Iterator) { true } 34 | a!(true); 35 | } 36 | 37 | impl A { 38 | fn a() {} 39 | fn b() {} 40 | } 41 | -------------------------------------------------------------------------------- /tests/issues/block_one_line.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | let span_forward = if is_inner { 2 } else { 1 }; 3 | let span_forward = if is_inner { Ok(()) } else { Err(()) }; 4 | let span_forward = if is_inner { OK } else { ERR }; 5 | let span_forward = if is_inner { A } else if true { B } else { ERR }; 6 | } 7 | -------------------------------------------------------------------------------- /tests/issues/closure.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | trees.sort_by(|a, b| { 3 | if a.path.starts_with("self") { 4 | Ordering::Less 5 | } else if b.path.starts_with("self") { 6 | Ordering::Greater 7 | } else { 8 | a.path.cmp(&b.path) 9 | } 10 | }); 11 | } 12 | -------------------------------------------------------------------------------- /tests/issues/commments/stmt_comments.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | type a = bool; // item 3 | let b = 10; // let 4 | b = 20; // expr 5 | a!(); // macro 6 | true // expr no simi 7 | } 8 | -------------------------------------------------------------------------------- /tests/issues/commments/struct_comments.rs: -------------------------------------------------------------------------------- 1 | // aaaaa 2 | 3 | // bbbbb 4 | struct A { // ccccc-struct-trailing 5 | // ddddd 6 | a: bool, // eeeee 7 | b: i32, // ffff 8 | // ggggg-struct-heading 9 | } // hhhhh 10 | -------------------------------------------------------------------------------- /tests/issues/commments/trailing_commments_0.rs: -------------------------------------------------------------------------------- 1 | mod a;// fffffffffffffffffff 2 | -------------------------------------------------------------------------------- /tests/issues/commments/trailing_commments_1.rs: -------------------------------------------------------------------------------- 1 | pub const c: i32 = -12_345; // fffffffffffffffffff 2 | -------------------------------------------------------------------------------- /tests/issues/doc.rs: -------------------------------------------------------------------------------- 1 | /*! 2 | This crate provides convenience methods for encoding and decoding numbers 3 | in either big-endian or little-endian order. 4 | 5 | The organization of the crate is pretty simple. A trait, `ByteOrder`, specifies 6 | byte conversion methods for each type of number in Rust (sans numbers that have 7 | a platform dependent size like `usize` and `isize`). Two types, `BigEndian` 8 | and `LittleEndian` implement these methods. Finally, `ReadBytesExt` and 9 | `WriteBytesExt` provide convenience methods available to all types that 10 | implement `Read` and `Write`. 11 | 12 | # Examples 13 | 14 | Read unsigned 16 bit big-endian integers from a `Read` type: 15 | 16 | ```rust 17 | use std::io::Cursor; 18 | use byteorder::{BigEndian, ReadBytesExt}; 19 | 20 | let mut rdr = Cursor::new(vec![2, 5, 3, 0]); 21 | // Note that we use type parameters to indicate which kind of byte order 22 | // we want! 23 | assert_eq!(517, rdr.read_u16::().unwrap()); 24 | assert_eq!(768, rdr.read_u16::().unwrap()); 25 | ``` 26 | 27 | Write unsigned 16 bit little-endian integers to a `Write` type: 28 | 29 | ```rust 30 | use byteorder::{LittleEndian, WriteBytesExt}; 31 | 32 | let mut wtr = vec![]; 33 | wtr.write_u16::(517).unwrap(); 34 | wtr.write_u16::(768).unwrap(); 35 | assert_eq!(wtr, vec![5, 2, 0, 3]); 36 | ``` 37 | */ 38 | -------------------------------------------------------------------------------- /tests/issues/extern_crate_use_mod_decl_in_item.rs: -------------------------------------------------------------------------------- 1 | fn test_to_json() { 2 | extern crate a; 3 | use super::ToJson; 4 | mod b; 5 | 6 | let array2 = Array(vec!(I64(1), I64(2))); 7 | let array3 = Array(vec!(I64(1), I64(2), I64(3))); 8 | let object = { 9 | let mut tree_map = BTreeMap::new(); 10 | tree_map.insert("a".to_string(), U64(1)); 11 | tree_map.insert("b".to_string(), U64(2)); 12 | Object(tree_map) 13 | }; 14 | } 15 | -------------------------------------------------------------------------------- /tests/issues/fn_one_line.rs: -------------------------------------------------------------------------------- 1 | fn f() -> bool { false } 2 | -------------------------------------------------------------------------------- /tests/issues/if_let_one_line.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | if let ExprKind::Literal(_) = expr { true } else { false } 3 | } 4 | -------------------------------------------------------------------------------- /tests/issues/if_let_wrap.rs: -------------------------------------------------------------------------------- 1 | impl A { 2 | fn build() -> Client { 3 | let compressor = if let Some(compression) = self.compression { 4 | Some(compression::build(compression)) 5 | } else { 6 | None 7 | }; 8 | 9 | if let Some(compression) = self.compression { 10 | Some(compression::build(compression)) 11 | } else { 12 | None 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/issues/impl_method_one_line.rs: -------------------------------------------------------------------------------- 1 | impl A for B { 2 | fn is_response(&self) -> bool { false } 3 | } 4 | -------------------------------------------------------------------------------- /tests/issues/macro_sep.rs: -------------------------------------------------------------------------------- 1 | #[test] 2 | fn test_from() { 3 | from_and_cause!(io::Error::new(io::ErrorKind::Other, "other") => Io(..)); 4 | } 5 | -------------------------------------------------------------------------------- /tests/issues/match.rs: -------------------------------------------------------------------------------- 1 | fn is_async(asyncness: ast::IsAsync) -> bool { 2 | match asyncness { 3 | ast::IsAsync::Async{..} => true, 4 | ast::IsAsync::Async{} => true, 5 | _ => false, 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /tests/issues/match_wrap.rs: -------------------------------------------------------------------------------- 1 | impl A { 2 | fn f() { 3 | match a { 4 | ast::ExprKind::Block(ref block, ref label) => ExprKind::Block(Box::new(self.trans_block_expr(block, label))), 5 | ast::TyKind::Rptr(ref lifetime, ref mut_type) => { 6 | TypeKind::Ref(Box::new(self.trans_ref_type(lifetime, mut_type))) 7 | } 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tests/issues/meta_item_long_str.rs: -------------------------------------------------------------------------------- 1 | #![unstable(feature = "set_stdio", 2 | reason = "this function may disappear completely or be replaced \ 3 | with a more general mechanism", 4 | issue = "0")] 5 | -------------------------------------------------------------------------------- /tests/issues/method_self.rs: -------------------------------------------------------------------------------- 1 | impl A { 2 | fn f(mut self) {} 3 | } 4 | -------------------------------------------------------------------------------- /tests/issues/method_self_value_immut.rs: -------------------------------------------------------------------------------- 1 | impl A { 2 | pub fn result(self) -> rfmt::Result { 3 | rfmt::Result { 4 | s: self.s, 5 | exceed_lines: self.exceed_lines, 6 | trailing_ws_lines: self.trailing_ws_lines, 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tests/issues/mod_nl.rs: -------------------------------------------------------------------------------- 1 | mod a { 2 | #![test] 3 | type a = bool; 4 | } 5 | 6 | -------------------------------------------------------------------------------- /tests/issues/nl.rs: -------------------------------------------------------------------------------- 1 | #![test] 2 | 3 | 4 | #![allow( 5 | unused_variables)] 6 | #![crate_type = "lib"] 7 | 8 | #![allow( 9 | aaa( 10 | bbb))] 11 | 12 | impl A { 13 | pub fn a(&self) { 14 | self.a.get(b).put(c) 15 | .map(|c| f(c)) 16 | .map(|c| f(c)); 17 | 18 | if aa && bb 19 | && cc 20 | && dd {} 21 | 22 | aa + bb + cc - ee 23 | + cc 24 | - dd; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tests/issues/nl_after_sep.rs: -------------------------------------------------------------------------------- 1 | 2 | impl fmt::Display for DeserializeError { 3 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { 4 | match *self { 5 | DeserializeError::IoError(ref ioerr) => 6 | write!(fmt, "IoError: {}", ioerr), 7 | DeserializeError::InvalidEncoding(ref ib) => 8 | write!(fmt, "InvalidEncoding: {}", ib), 9 | DeserializeError::SizeLimit => 10 | write!(fmt, "SizeLimit"), 11 | DeserializeError::SyntaxError => 12 | write!(fmt, "SyntaxError"), 13 | DeserializeError::EndOfStreamError => 14 | write!(fmt, "EndOfStreamError"), 15 | DeserializeError::UnknownFieldError => 16 | write!(fmt, "UnknownFieldError"), 17 | DeserializeError::MissingFieldError => 18 | write!(fmt, "MissingFieldError"), 19 | } 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /tests/issues/path_type_from_expr.rs: -------------------------------------------------------------------------------- 1 | pub trait Method: Encodable + Decodable { 2 | fn se(&self) -> AmqpResult> { 3 | let mut encoder = Encoder::::new(); 4 | Ok(encoder.data) 5 | } 6 | } 7 | 8 | -------------------------------------------------------------------------------- /tests/issues/return_trait.rs: -------------------------------------------------------------------------------- 1 | fn returns_summarizable() -> impl Summary { 2 | Tweet { 3 | username: String::from("horse_ebooks"), 4 | content: String::from("of course, as you probably already know, people"), 5 | reply: false, 6 | retweet: false, 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /tests/issues/str_litera_exceed_max_width.rs: -------------------------------------------------------------------------------- 1 | mod a { 2 | #[test] 3 | fn test_combined() { 4 | let args = vec!("prog".to_string(), 5 | "free1".to_string(), 6 | "-s".to_string(), 7 | "20".to_string(), 8 | "free2".to_string(), 9 | "--flag".to_string(), 10 | "--long=30".to_string(), 11 | "-f".to_string(), 12 | "-m".to_string(), 13 | "40".to_string(), 14 | "-m".to_string(), 15 | "50".to_string(), 16 | "-n".to_string(), 17 | "-A B".to_string(), 18 | "-n".to_string(), 19 | "-60 70".to_string()); 20 | match Options::new().optopt("s", "something", "something", "SOMETHING").optflag("", "flag", 21 | "a flag").reqopt("", "long", "hi", "LONG").optflag("f", "", "another flag") 22 | .optmulti("m", "", "mmmmmm", "YUM").optmulti("n", "", "nothing", "NOTHING").optopt( 23 | "", "notpresent", "nothing to see here", "NOPE").parse(&args) {} 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tests/issues/str_literal.rs: -------------------------------------------------------------------------------- 1 | const HEADER_STRUCT: &'static str = r#" 2 | #[derive(Debug)] 3 | pub struct Header { 4 | name: &'static [u8; 4], 5 | major: u8, 6 | minor: u8, 7 | revision: u8, 8 | } 9 | "#; 10 | 11 | const HEADER_STRUCT: &'static str = br#" 12 | #[derive(Debug)] 13 | pub struct Header { 14 | name: &'static [u8; 4], 15 | major: u8, 16 | minor: u8, 17 | revision: u8, 18 | } 19 | "#; 20 | -------------------------------------------------------------------------------- /tests/issues/struct_patten.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | match a { 3 | ast::VisibilityKind::Restricted { ref path, .. } => {} 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /tests/issues/trait_method.rs: -------------------------------------------------------------------------------- 1 | pub trait Message { 2 | fn f() -> bool where T: Sized; 3 | fn new() 4 | -> Self where Self: Sized; 5 | } 6 | -------------------------------------------------------------------------------- /tests/issues/trait_method_one_line.rs: -------------------------------------------------------------------------------- 1 | pub trait Request: Message { 2 | fn is_response(&self) -> bool { false } 3 | } 4 | -------------------------------------------------------------------------------- /tests/issues/use_nl.rs: -------------------------------------------------------------------------------- 1 | use std::{ 2 | fmt::{self, Debug, Display, Formatter}, 3 | fmt::{self, Debug, 4 | Display, Formatter}, 5 | io 6 | }; 7 | 8 | use std::{ fmt::{self, Debug, Display, Formatter}, fmt::{self, Debug, Display, Formatter}, io }; 9 | 10 | use std::{ fmt::{self, Debug, Display, Formatter}, 11 | fmt::{self, 12 | Debug, Display, Formatter}, io }; 13 | -------------------------------------------------------------------------------- /tests/rustfmt/3156.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | let mut a = Vec::new(); 3 | a.push(match 0 { 4 | 0 => {let a=0;""} 5 | _ => r#" 6 | --------------------loong line----------------------------------------------------------------------. 7 | "#, 8 | }); 9 | } 10 | -------------------------------------------------------------------------------- /tests/rustfmt/3167.rs: -------------------------------------------------------------------------------- 1 | // http://tools.ietf.org/html/rfc6265#section-5.1.4 2 | pub fn path_match(request_path: &str, cookie_path: &str) -> bool { 3 | // A request-path path-matches a given cookie-path if at least one of 4 | // the following conditions holds: 5 | // The cookie-path and the request-path are identical. 6 | request_path == cookie_path || 7 | 8 | (request_path.starts_with(cookie_path) && ( 9 | // The cookie-path is a prefix of the request-path, and the last 10 | // character of the cookie-path is %x2F ("/"). 11 | cookie_path.ends_with("/") || 12 | // The cookie-path is a prefix of the request-path, and the first 13 | // character of the request-path that is not included in the cookie- 14 | // path is a %x2F ("/") character. 15 | request_path[cookie_path.len()..].starts_with("/") 16 | )) 17 | } 18 | -------------------------------------------------------------------------------- /tests/rustfmt/3191.rs: -------------------------------------------------------------------------------- 1 | fn foo() { 2 | unprivileged_content.start_all::(true); 3 | } 4 | -------------------------------------------------------------------------------- /tests/rustfmt/3192.rs: -------------------------------------------------------------------------------- 1 | fn foo(input: Option) -> usize { 2 | match input { 3 | Some(x) => return x, 4 | None => { 5 | return 0; 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /tests/rustfmt/3245.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let x = 1; 3 | ;let y = 3; 4 | } 5 | -------------------------------------------------------------------------------- /tests/rustfmt/3251.rs: -------------------------------------------------------------------------------- 1 | fn foo() { 2 | let coords: Vec<_> = 3 | [(self.y - 1, self.x), 4 | (self.y, self.x - 1), 5 | (self.y, self.x + 1), 6 | (self.y + 1, self.x)].iter() 7 | .filter_map({}) 8 | .filter({}) 9 | .map({}) 10 | .collect(); 11 | } 12 | -------------------------------------------------------------------------------- /tests/rustfmt/3286.rs: -------------------------------------------------------------------------------- 1 | use { 2 | fidl_fuchsia_hardware_ethernet_ext::EthernetQueueFlags, futures::TryStreamExt, std::fs::File, 3 | structopt::StructOpt, 4 | }; 5 | -------------------------------------------------------------------------------- /tests/rustfmt/3419.rs: -------------------------------------------------------------------------------- 1 | lazy_static! { 2 | pub static ref BLOCKING_POOL: tokio_threadpool::ThreadPool = { 3 | tokio_threadpool::Builder::new().pool_size(1).build() 4 | }; 5 | 6 | static ref FOO: Foo = unsafe { 7 | very_long_function_name().another_function_with_really_long_name() 8 | }; 9 | } 10 | -------------------------------------------------------------------------------- /tests/rustfmt/3502.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let f = future::poll_fn( 3 | move || match tokio_threadpool::blocking(|| f.poll()).unwrap() { 4 | Async::Ready(v) => v, 5 | Async::NotReady => Ok(Async::NotReady), 6 | }, 7 | ); 8 | } 9 | -------------------------------------------------------------------------------- /tests/rustfmt/3551.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let dnlml = Array1::from_shape_fn(ln_hyperparams.len(), |i| { 3 | 0.5 * (self 4 | .covfunc 5 | .deriv_covariance(ln_hyperparams, train_inputs, i) 6 | * &W) 7 | .scalar_sum() 8 | }); 9 | } 10 | -------------------------------------------------------------------------------- /tests/rustfmt/3560.rs: -------------------------------------------------------------------------------- 1 | struct Foo { 2 | something: Option, 3 | bar: u32, 4 | } 5 | 6 | fn main() { 7 | let a = Foo { 8 | something: Some(1), 9 | bar: 2, 10 | }; 11 | if let Foo { something: Some(something), .. } = a { 12 | println!("{}", something); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/rustfmt/3561.rs: -------------------------------------------------------------------------------- 1 | fn main() {;7 2 | } 3 | -------------------------------------------------------------------------------- /tests/rustfmt/3591.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | /* Common case: The double precision result is fine. */ 3 | if (ui & 0x1fffffff) != 0x10000000 /* not a halfway case */ 4 | || e == 0x7ff /* NaN */ 5 | || (result - xy == z as f64 && result - z as f64 == xy) /* exact */ 6 | || fegetround() != FE_TONEAREST 7 | /* not round-to-nearest */ 8 | {} 9 | } 10 | -------------------------------------------------------------------------------- /tests/rustfmt/3599.rs: -------------------------------------------------------------------------------- 1 | type ProposeTransactionsFuture: Future< 2 | Item = ProposeTransactionsResponse, 3 | Error = Error, 4 | > + Send 5 | + 'static; 6 | -------------------------------------------------------------------------------- /tests/rustfmt/3617.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod tests { 3 | #[test] 4 | fn handles_mid_demangling() { 5 | assert_eq!( 6 | crate::demangle_line(" lea rax, [rip + _ZN55_$LT$$RF$$u27$a$u20$T$u20$as$u20$core..fmt..Display$GT$3fmt17h510ed05e72307174E]"), 7 | " lea rax, [rip + <&\'a T as core::fmt::Display>::fmt]"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tests/rustfmt/3634.rs: -------------------------------------------------------------------------------- 1 | fn test() { 2 | let aaaaaaaaaaaaaaaa = ffffffffffffffffffffffff 3 | .iter() 4 | .filter_map(|_| { 5 | if bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb 6 | == ccccccccccccccccccccccc 7 | .dddddddddddddddddddddddd() 8 | .eeeeeeeeeeeeeeeeeeeeee() 9 | { 10 | (); 11 | } 12 | }) 13 | .collect(); 14 | } 15 | -------------------------------------------------------------------------------- /tests/rustfmt/3638.rs: -------------------------------------------------------------------------------- 1 | macro_rules! abait { ($x:expr) => { Ok(()) } } 2 | 3 | mod a { 4 | fn foo() -> Result<(), ()> { 5 | unsafe { 6 | ( 7 | abait!(proxy.load_account(ahc_client_end, TEST_ACCOUNT_ID.clone().as_mut().into()))?, 8 | (), 9 | ) 10 | }; 11 | Ok(()) 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/rustfmt/3701.rs: -------------------------------------------------------------------------------- 1 | fn build_sorted_static_get_entry_names( 2 | mut entries: Vec<(u8, &'static str)>, 3 | ) -> impl 4 | ( 5 | Fn( 6 | AlphabeticalTraversal, 7 | Box>, 8 | ) -> BoxFuture<'static, Result, Status>> 9 | ) 10 | + Send 11 | + Sync 12 | + 'static 13 | {} 14 | -------------------------------------------------------------------------------- /tests/rustfmt/3716.rs: -------------------------------------------------------------------------------- 1 | struct MyStruct { 2 | field1: String, 3 | field2: usize, 4 | field3: MyType, 5 | // This group ends here, and I want this comment to be attached to its end 6 | 7 | field4: f64, 8 | field5: f64, 9 | field6: f64, 10 | } 11 | -------------------------------------------------------------------------------- /tests/rustfmt/3720.rs: -------------------------------------------------------------------------------- 1 | use c; // use c; 2 | use b; // use b; 3 | use a; // use a; 4 | -------------------------------------------------------------------------------- /tests/rustfmt/3736.rs: -------------------------------------------------------------------------------- 1 | impl Index for StaticVec { 2 | type Output = T; 3 | ///Asserts that `index` is less than the current length of the StaticVec, 4 | ///and if so returns the value at that position as a constant reference. 5 | #[inline(always)] 6 | fn index(&self, index: usize) -> &Self::Output { 7 | assert!(index < self.length); 8 | unsafe { self.data.get_unchecked(index).get_ref() } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tests/rustfmt/3741.rs: -------------------------------------------------------------------------------- 1 | pub enum PublishedFileVisibility { 2 | Public = sys::ERemoteStoragePublishedFileVisibility_k_ERemoteStoragePublishedFileVisibilityPublic as i32, 3 | FriendsOnly = sys::ERemoteStoragePublishedFileVisibility_k_ERemoteStoragePublishedFileVisibilityFriendsOnly as i32, 4 | Private = sys::ERemoteStoragePublishedFileVisibility_k_ERemoteStoragePublishedFileVisibilityPrivate as i32, 5 | } 6 | -------------------------------------------------------------------------------- /tests/rustfmt/3762.rs: -------------------------------------------------------------------------------- 1 | extern crate toml; 2 | #[macro_use] 3 | extern crate serde_derive; 4 | 5 | use toml::Value; 6 | 7 | macro_rules! float_inf_tests { 8 | ($ty:ty) => {{ 9 | #[derive(Serialize, Deserialize)] 10 | struct S { 11 | sf1: $ty, 12 | sf2: $ty, 13 | sf3: $ty, 14 | sf4: $ty, 15 | sf5: $ty, 16 | sf6: $ty, 17 | sf7: $ty, 18 | sf8: $ty, 19 | } 20 | let inf: S = toml::from_str( 21 | r" 22 | # infinity 23 | sf1 = inf # positive infinity 24 | sf2 = +inf # positive infinity 25 | sf3 = -inf # negative infinity 26 | 27 | # not a number 28 | sf4 = nan # actual sNaN/qNaN encoding is implementation specific 29 | sf5 = +nan # same as `nan` 30 | sf6 = -nan # valid, actual encoding is implementation specific 31 | 32 | # zero 33 | sf7 = +0.0 34 | sf8 = -0.0 35 | ", 36 | ) 37 | .expect("Parse infinities."); 38 | 39 | assert!(inf.sf1.is_infinite()); 40 | assert!(inf.sf1.is_sign_positive()); 41 | assert!(inf.sf2.is_infinite()); 42 | assert!(inf.sf2.is_sign_positive()); 43 | assert!(inf.sf3.is_infinite()); 44 | assert!(inf.sf3.is_sign_negative()); 45 | 46 | assert!(inf.sf4.is_nan()); 47 | assert!(inf.sf4.is_sign_positive()); 48 | assert!(inf.sf5.is_nan()); 49 | assert!(inf.sf5.is_sign_positive()); 50 | assert!(inf.sf6.is_nan()); 51 | assert!(inf.sf6.is_sign_negative()); 52 | 53 | assert_eq!(inf.sf7, 0.0); 54 | assert!(inf.sf7.is_sign_positive()); 55 | assert_eq!(inf.sf8, 0.0); 56 | assert!(inf.sf8.is_sign_negative()); 57 | 58 | let s = toml::to_string(&inf).unwrap(); 59 | assert_eq!( 60 | s, 61 | "\ 62 | sf1 = inf 63 | sf2 = inf 64 | sf3 = -inf 65 | sf4 = nan 66 | sf5 = nan 67 | sf6 = -nan 68 | sf7 = 0.0 69 | sf8 = -0.0 70 | " 71 | ); 72 | 73 | toml::from_str::(&s).expect("roundtrip"); 74 | }}; 75 | } 76 | 77 | #[test] 78 | fn float_inf() { 79 | float_inf_tests!(f32); 80 | float_inf_tests!(f64); 81 | } 82 | -------------------------------------------------------------------------------- /tests/rustfmt/566.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | let program = glium::Program::from_source(&display, &include_str!("./shaders/vertex.glsl"), 3 | &include_str!("./shaders/fragment.glsl"), None) 4 | .unwrap(); 5 | } 6 | -------------------------------------------------------------------------------- /tests/rustfmt/626.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let (a, b, c, d) = (0, 0, 0, 0); 3 | let _ = u32::from_be(((a as u32) << 24) | 4 | ((b as u32) << 16) | 5 | ((c as u32) << 8) | 6 | (d as u32) << 0); 7 | } 8 | -------------------------------------------------------------------------------- /tests/rustfmt/810.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | let mut board: [[char; 3]; 3] = [ 3 | ['O', 'O', 'O'], 4 | ['O', 'O', 'O'], 5 | ['O', 'O', 'O'] 6 | ]; 7 | } 8 | -------------------------------------------------------------------------------- /tests/rustfmt/915.rs: -------------------------------------------------------------------------------- 1 | fn f() { 2 | gfx_pipeline!(pipe { 3 | vbuf: gfx::VertexBuffer = (), 4 | out: gfx::RenderTarget = "Target0", 5 | }); 6 | } 7 | --------------------------------------------------------------------------------