diff --git a/Configurations.md b/Configurations.md index 2d01fb3bb3b..e8a0decc848 100644 --- a/Configurations.md +++ b/Configurations.md @@ -1105,6 +1105,19 @@ macro_rules! foo { See also [`format_macro_bodies`](#format_macro_bodies). +## `surround_blocks_with_empty_lines` + +#### `false` (default): + +```rust +// todo +``` + +#### `true`: + +```rust +// todo +``` ## `format_macro_bodies` diff --git a/src/config/mod.rs b/src/config/mod.rs index 9484b2e5829..e176e8e2660 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -153,6 +153,9 @@ create_config! { format_generated_files: bool, true, false, "Format generated files"; generated_marker_line_search_limit: usize, 5, false, "Number of lines to check for a \ `@generated` marker when `format_generated_files` is enabled"; + surround_blocks_with_empty_lines: bool, false, false, + "Surround all block expressions with \ + empty lines. This option is not stable and could be removed at any time."; // Options that can change the source code beyond whitespace/blocks (somewhat linty things) merge_derives: bool, true, true, "Merge multiple `#[derive(...)]` into a single one"; @@ -683,6 +686,7 @@ version = "One" inline_attribute_width = 0 format_generated_files = true generated_marker_line_search_limit = 5 +surround_blocks_with_empty_lines = false merge_derives = true use_try_shorthand = false use_field_init_shorthand = false diff --git a/src/expr.rs b/src/expr.rs index 7808f891336..8b061669022 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1194,6 +1194,63 @@ pub(crate) fn is_simple_block( && attrs.map_or(true, |a| a.is_empty()) } +pub(crate) fn contains_curly_block(expr: &ast::Expr) -> bool { + match expr.kind { + ast::ExprKind::If(..) + | ast::ExprKind::While(..) + | ast::ExprKind::ForLoop { .. } + | ast::ExprKind::Loop(..) + | ast::ExprKind::Match(..) + | ast::ExprKind::Let(..) + | ast::ExprKind::Block(..) + | ast::ExprKind::TryBlock(..) + | ast::ExprKind::Try(..) + | ast::ExprKind::Gen(..) => true, + ast::ExprKind::Unary(_, ref expr) + | ast::ExprKind::Cast(ref expr, _) + | ast::ExprKind::Type(ref expr, _) + | ast::ExprKind::Await(ref expr, _) + | ast::ExprKind::Field(ref expr, _) + | ast::ExprKind::Become(ref expr) + | ast::ExprKind::Repeat(ref expr, _) + | ast::ExprKind::Paren(ref expr) + | ast::ExprKind::AddrOf(_, _, ref expr) + | ast::ExprKind::AssignOp(_, ref expr, ..) => contains_curly_block(expr), + ast::ExprKind::Closure(ref closure) => contains_curly_block(&closure.body), + ast::ExprKind::Binary(_, ref a, ref b) + | ast::ExprKind::Assign(ref a, ref b, _) + | ast::ExprKind::Index(ref a, ref b, _) => { + contains_curly_block(a) || contains_curly_block(b) + } + ast::ExprKind::Break(_, ref maybe_expr) + | ast::ExprKind::Ret(ref maybe_expr) + | ast::ExprKind::Yield(ref maybe_expr) + | ast::ExprKind::Yeet(ref maybe_expr) => { + maybe_expr.as_deref().map_or(false, contains_curly_block) + } + ast::ExprKind::Range(ref maybe_a, ref maybe_b, _) => maybe_a + .as_deref() + .or(maybe_b.as_deref()) + .map_or(false, contains_curly_block), + ast::ExprKind::InlineAsm(..) + | ast::ExprKind::OffsetOf(..) + | ast::ExprKind::MacCall(..) + | ast::ExprKind::Struct(..) + | ast::ExprKind::Continue(..) + | ast::ExprKind::IncludedBytes(..) + | ast::ExprKind::FormatArgs(..) + | ast::ExprKind::Path(..) + | ast::ExprKind::Array(..) + | ast::ExprKind::ConstBlock(..) + | ast::ExprKind::Call(..) + | ast::ExprKind::MethodCall(..) + | ast::ExprKind::Tup(..) + | ast::ExprKind::Lit(..) + | ast::ExprKind::Underscore + | ast::ExprKind::Err => false, + } +} + /// Checks whether a block contains at most one statement or expression, and no /// comments or attributes. pub(crate) fn is_simple_block_stmt( diff --git a/src/items.rs b/src/items.rs index 8669bdda32b..6257944b71a 100644 --- a/src/items.rs +++ b/src/items.rs @@ -489,7 +489,7 @@ impl<'a> FmtVisitor<'a> { return None; } - let res = Stmt::from_ast_node(block.stmts.first()?, true) + let res = Stmt::from_ast_node(block.stmts.first()?, true, true) .rewrite(&self.get_context(), self.shape())?; let width = self.block_indent.width() + fn_str.len() + res.len() + 5; diff --git a/src/stmt.rs b/src/stmt.rs index e3fe4ebca11..281431e6a81 100644 --- a/src/stmt.rs +++ b/src/stmt.rs @@ -3,7 +3,7 @@ use rustc_span::Span; use crate::comment::recover_comment_removed; use crate::config::Version; -use crate::expr::{format_expr, is_simple_block, ExprType}; +use crate::expr::{contains_curly_block, format_expr, is_simple_block, ExprType}; use crate::rewrite::{Rewrite, RewriteContext}; use crate::shape::Shape; use crate::source_map::LineRangeUtils; @@ -12,6 +12,7 @@ use crate::utils::semicolon_for_stmt; pub(crate) struct Stmt<'a> { inner: &'a ast::Stmt, + is_first: bool, is_last: bool, } @@ -41,15 +42,24 @@ impl<'a> Stmt<'a> { if is_simple_block(context, block, attrs) { let inner = &block.stmts[0]; // Simple blocks only contain one expr and no stmts + let is_first = true; let is_last = true; - Some(Stmt { inner, is_last }) + Some(Stmt { + inner, + is_first, + is_last, + }) } else { None } } - pub(crate) fn from_ast_node(inner: &'a ast::Stmt, is_last: bool) -> Self { - Stmt { inner, is_last } + pub(crate) fn from_ast_node(inner: &'a ast::Stmt, is_first: bool, is_last: bool) -> Self { + Stmt { + inner, + is_first, + is_last, + } } pub(crate) fn from_ast_nodes(iter: I) -> Vec @@ -58,9 +68,19 @@ impl<'a> Stmt<'a> { { let mut result = vec![]; let mut iter = iter.peekable(); - while iter.peek().is_some() { + + if let Some(inner) = iter.next() { result.push(Stmt { - inner: iter.next().unwrap(), + inner, + is_first: true, + is_last: iter.peek().is_none(), + }) + } + + while let Some(inner) = iter.next() { + result.push(Stmt { + inner, + is_first: false, is_last: iter.peek().is_none(), }) } @@ -71,6 +91,14 @@ impl<'a> Stmt<'a> { matches!(self.inner.kind, ast::StmtKind::Empty) } + pub(crate) fn is_first(&self) -> bool { + self.is_first + } + + pub(crate) fn is_last(&self) -> bool { + self.is_last + } + fn is_last_expr(&self) -> bool { if !self.is_last { return false; @@ -86,6 +114,20 @@ impl<'a> Stmt<'a> { _ => false, } } + + pub(crate) fn is_block_with_curly_braces(&self) -> bool { + match self.as_ast_node().kind { + ast::StmtKind::Local(ref local) => match local.kind { + ast::LocalKind::Decl => false, + ast::LocalKind::Init(ref expr) => contains_curly_block(expr), + ast::LocalKind::InitElse(..) => true, + }, + ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => { + contains_curly_block(expr) + } + _ => false, + } + } } impl<'a> Rewrite for Stmt<'a> { diff --git a/src/visitor.rs b/src/visitor.rs index 61e147ed8f5..3dfc5f25900 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -877,8 +877,27 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { .collect(); if items.is_empty() { + let stmt = &stmts[0]; + + let should_preced_with_empty_line = self.config.surround_blocks_with_empty_lines() + && !self.buffer.ends_with('\n') + && !stmt.is_first() + && stmt.is_block_with_curly_braces(); + + if should_preced_with_empty_line { + self.push_str("\n"); + } + self.visit_stmt(&stmts[0], include_current_empty_semi); + let should_follow_with_empty_line = self.config.surround_blocks_with_empty_lines() + && !stmt.is_last() + && stmt.is_block_with_curly_braces(); + + if should_follow_with_empty_line { + self.push_str("\n"); + } + // FIXME(calebcartwright 2021-01-03) - This exists strictly to maintain legacy // formatting where rustfmt would preserve redundant semicolons on Items in a // statement position. diff --git a/tests/source/configs/surround-blocks-with-empty-lines.rs b/tests/source/configs/surround-blocks-with-empty-lines.rs new file mode 100644 index 00000000000..03d92eeab8a --- /dev/null +++ b/tests/source/configs/surround-blocks-with-empty-lines.rs @@ -0,0 +1,1104 @@ +// rustfmt-surround_blocks_with_empty_lines: true + +fn main() { + + // control flow at start of function + if thing { + println!("Hello, world!"); + } + + let x = 42; + // two control flows in a row + if lorem { + println!("ipsum!"); + } else { + println!("dolor!"); + } + if other { + println!("start"); + } else if middle { + println!("middle!"); + } else { + println!("end"); + } + if end { + println!("end"); + } + let y = 24; + // control flow at end of function + match magi { + Homura => "Akemi", + Madoka => "Kaname", + } + +} + +fn for_loop() { + + for _ in 0..10 { + println!("Hello, world!"); + } + +} + +fn for_loop_double() { + + for _ in 0..10 { + println!("Hello, world!"); + } + for _ in 0..10 { + println!("Hello, world!"); + } + +} + +fn for_loop_triple() { + + for _ in 0..10 { + println!("Hello, world!"); + } + for _ in 0..10 { + println!("Hello, world!"); + } + for _ in 0..10 { + println!("Hello, world!"); + } + +} + +fn for_loop_quad() { + for _ in 0..10 { + println!("Hello, world!"); + } + + for _ in 0..10 { + println!("Hello, world!"); + } + for _ in 0..10 { + println!("Hello, world!"); + } + + for _ in 0..10 { + println!("Hello, world!"); + } +} + +fn let_for_loop_block() { + + let _ = for _ in 0..10 { + println!("Hello, world!"); + break thing; + }; + +} + +fn let_for_loop_double() { + + let _ = for _ in 0..10 { + println!("Hello, world!"); + break thing; + }; + let _ = for _ in 0..10 { + println!("Hello, world!"); + break thing; + }; + +} + +fn let_for_loop_triple() { + + let _ = for _ in 0..10 { + println!("Hello, world!"); + break thing; + }; + let _ = for _ in 0..10 { + println!("Hello, world!"); + break thing; + }; + let _ = for _ in 0..10 { + println!("Hello, world!"); + break thing; + }; + +} + +fn let_for_loop_quad() { + let _ = for _ in 0..10 { + println!("Hello, world!"); + break thing; + }; + + let _ = for _ in 0..10 { + println!("Hello, world!"); + break thing; + }; + let _ = for _ in 0..10 { + println!("Hello, world!"); + break thing; + }; + + let _ = for _ in 0..10 { + println!("Hello, world!"); + break thing; + }; +} + +fn while_loop() { + + while true { + println!("Hello, world!"); + } + +} + +fn while_loop_double() { + + while true { + println!("Hello, world!"); + } + while true { + println!("Hello, world!"); + } + +} + +fn while_loop_triple() { + + while true { + println!("Hello, world!"); + } + while true { + println!("Hello, world!"); + } + while true { + println!("Hello, world!"); + } + +} + +fn while_loop_quad() { + while true { + println!("Hello, world!"); + } + + while true { + println!("Hello, world!"); + } + while true { + println!("Hello, world!"); + } + + while true { + println!("Hello, world!"); + } +} + +fn let_while_loop_block() { + + let _ = while true { + println!("Hello, world!"); + break thing; + }; + +} + +fn let_while_loop_double() { + + let _ = while true { + println!("Hello, world!"); + break thing; + }; + let _ = while true { + println!("Hello, world!"); + break thing; + }; + +} + +fn let_while_loop_triple() { + + let _ = while true { + println!("Hello, world!"); + break thing; + }; + let _ = while true { + println!("Hello, world!"); + break thing; + }; + let _ = while true { + println!("Hello, world!"); + break thing; + }; + +} + +fn let_while_loop_quad() { + let _ = while true { + println!("Hello, world!"); + break thing; + }; + + let _ = while true { + println!("Hello, world!"); + break thing; + }; + let _ = while true { + println!("Hello, world!"); + break thing; + }; + + let _ = while true { + println!("Hello, world!"); + break thing; + }; +} + +fn if_block() { + + if true { + println!("Hello, world!"); + } + +} + +fn if_double() { + + if true { + println!("Hello, world!"); + } + if true { + println!("Hello, world!"); + } + +} + +fn if_triple() { + + if true { + println!("Hello, world!"); + } + if true { + println!("Hello, world!"); + } + if true { + println!("Hello, world!"); + } + +} + +fn if_quad() { + if true { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } + if true { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } +} + +fn let_if_block() { + + let _ = if true { + println!("Hello, world!"); + }; + +} + +fn let_if_double() { + + let _ = if true { + println!("Hello, world!"); + }; + let _ = if true { + println!("Hello, world!"); + }; + +} + +fn let_if_triple() { + + let _ = if true { + println!("Hello, world!"); + }; + let _ = if true { + println!("Hello, world!"); + }; + let _ = if true { + println!("Hello, world!"); + }; + +} + +fn let_if_quad() { + let _ = if true { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + }; + let _ = if true { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + }; +} + +fn if_else_block() { + + if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + +} + +fn if_else_double() { + + if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + +} + +fn if_else_triple() { + + if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + +} + +fn if_else_quad() { + if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } +} + +fn let_if_else_block() { + + let _ = if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + +} + +fn let_if_else_double() { + + let _ = if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + let _ = if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + +} + +fn let_if_else_triple() { + + let _ = if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + let _ = if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + let _ = if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + +} + +fn let_if_else_quad() { + let _ = if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + let _ = if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; +} + +fn else_if_block() { + + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } + +} + +fn else_if_double() { + + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } + +} + +fn else_if_triple() { + + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } + +} + +fn else_if_quad() { + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } +} + +fn let_if_else_if_block() { + + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + }; + +} + +fn let_if_else_if_else_double() { + + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + }; + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + }; + +} + +fn let_if_else_if_else_triple() { + + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + }; + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + }; + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + }; + +} + +fn let_if_else_if_else_quad() { + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + }; + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + }; +} + +fn if_else_if_else_block() { + + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + +} + +fn if_else_if_else_double() { + + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + +} + +fn if_else_if_else_triple() { + + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + +} + +fn if_else_if_else_quad() { + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } +} + +fn let_if_else_if_else_block() { + + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + +} + +fn let_if_else_if_else_double() { + + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + +} + +fn let_if_else_if_else_triple() { + + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + +} + +fn let_if_else_if_else_quad() { + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; +} + +fn loop_block() { + + loop { + println!("Hello, world!"); + break; + } + +} + +fn loop_double() { + + loop { + println!("Hello, world!"); + break; + } + loop { + println!("Hello, world!"); + break; + } + +} + +fn loop_triple() { + + loop { + println!("Hello, world!"); + break; + } + loop { + println!("Hello, world!"); + break; + } + loop { + println!("Hello, world!"); + break; + } + +} + +fn for_loop_quad() { + loop { + println!("Hello, world!"); + break; + } + + loop { + println!("Hello, world!"); + break; + } + loop { + println!("Hello, world!"); + break; + } + + loop { + println!("Hello, world!"); + break; + } +} + +fn let_loop_block() { + + let _ = loop { + println!("Hello, world!"); + break; + }; + +} + +fn let_loop_double() { + + let _ = loop { + println!("Hello, world!"); + break; + }; + let _ = loop { + println!("Hello, world!"); + break; + }; + +} + +fn let_loop_triple() { + + let _ = loop { + println!("Hello, world!"); + break; + }; + let _ = loop { + println!("Hello, world!"); + break; + }; + let _ = loop { + println!("Hello, world!"); + break; + }; + +} + +fn let_loop_quad() { + let _ = loop { + println!("Hello, world!"); + break; + }; + + let _ = loop { + println!("Hello, world!"); + break; + }; + let _ = loop { + println!("Hello, world!"); + break; + }; + + let _ = loop { + println!("Hello, world!"); + break; + }; +} + +fn match_block() { + + match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + } + +} + +fn match_double() { + + match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + } + match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + } + +} + +fn match_triple() { + + match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + } + match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + } + match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + } + +} + +fn match_quad() { + match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + } + + match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + } + match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + } + + match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + } +} + +fn let_match_block() { + + let _ = match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + }; + +} + +fn let_match_double() { + + let _ = match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + }; + let _ = match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + }; + +} + +fn let_match_triple() { + + let _ = match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + }; + let _ = match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + }; + let _ = match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + }; + +} + +fn let_match_quad() { + let _ = match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + }; + + let _ = match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + }; + let _ = match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + }; + + let _ = match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + }; +} + +fn let_var_with_block() { + + let _ = { + println!("Hello, world!"); + }; + +} + +fn let_var_with_double() { + + let _ = { + println!("Hello, world!"); + }; + let _ = { + println!("Hello, world!"); + }; + +} + +fn let_var_with_triple() { + + let _ = { + println!("Hello, world!"); + }; + let _ = { + println!("Hello, world!"); + }; + let _ = { + println!("Hello, world!"); + }; + +} + +fn let_var_with_quad() { + let _ = { + println!("Hello, world!"); + }; + + let _ = { + println!("Hello, world!"); + }; + let _ = { + println!("Hello, world!"); + }; + + let _ = { + println!("Hello, world!"); + }; +} diff --git a/tests/target/configs/surround-blocks-with-empty-lines.rs b/tests/target/configs/surround-blocks-with-empty-lines.rs new file mode 100644 index 00000000000..a2aea251e8e --- /dev/null +++ b/tests/target/configs/surround-blocks-with-empty-lines.rs @@ -0,0 +1,1073 @@ +// rustfmt-surround_blocks_with_empty_lines: true + +fn main() { + // control flow at start of function + if thing { + println!("Hello, world!"); + } + + let x = 42; + + // two control flows in a row + if lorem { + println!("ipsum!"); + } else { + println!("dolor!"); + } + + if other { + println!("start"); + } else if middle { + println!("middle!"); + } else { + println!("end"); + } + + if end { + println!("end"); + } + + let y = 24; + + // control flow at end of function + match magi { + Homura => "Akemi", + Madoka => "Kaname", + } +} + +fn for_loop() { + for _ in 0..10 { + println!("Hello, world!"); + } +} + +fn for_loop_double() { + for _ in 0..10 { + println!("Hello, world!"); + } + + for _ in 0..10 { + println!("Hello, world!"); + } +} + +fn for_loop_triple() { + for _ in 0..10 { + println!("Hello, world!"); + } + + for _ in 0..10 { + println!("Hello, world!"); + } + + for _ in 0..10 { + println!("Hello, world!"); + } +} + +fn for_loop_quad() { + for _ in 0..10 { + println!("Hello, world!"); + } + + for _ in 0..10 { + println!("Hello, world!"); + } + + for _ in 0..10 { + println!("Hello, world!"); + } + + for _ in 0..10 { + println!("Hello, world!"); + } +} + +fn let_for_loop_block() { + let _ = for _ in 0..10 { + println!("Hello, world!"); + break thing; + }; +} + +fn let_for_loop_double() { + let _ = for _ in 0..10 { + println!("Hello, world!"); + break thing; + }; + + let _ = for _ in 0..10 { + println!("Hello, world!"); + break thing; + }; +} + +fn let_for_loop_triple() { + let _ = for _ in 0..10 { + println!("Hello, world!"); + break thing; + }; + + let _ = for _ in 0..10 { + println!("Hello, world!"); + break thing; + }; + + let _ = for _ in 0..10 { + println!("Hello, world!"); + break thing; + }; +} + +fn let_for_loop_quad() { + let _ = for _ in 0..10 { + println!("Hello, world!"); + break thing; + }; + + let _ = for _ in 0..10 { + println!("Hello, world!"); + break thing; + }; + + let _ = for _ in 0..10 { + println!("Hello, world!"); + break thing; + }; + + let _ = for _ in 0..10 { + println!("Hello, world!"); + break thing; + }; +} + +fn while_loop() { + while true { + println!("Hello, world!"); + } +} + +fn while_loop_double() { + while true { + println!("Hello, world!"); + } + + while true { + println!("Hello, world!"); + } +} + +fn while_loop_triple() { + while true { + println!("Hello, world!"); + } + + while true { + println!("Hello, world!"); + } + + while true { + println!("Hello, world!"); + } +} + +fn while_loop_quad() { + while true { + println!("Hello, world!"); + } + + while true { + println!("Hello, world!"); + } + + while true { + println!("Hello, world!"); + } + + while true { + println!("Hello, world!"); + } +} + +fn let_while_loop_block() { + let _ = while true { + println!("Hello, world!"); + break thing; + }; +} + +fn let_while_loop_double() { + let _ = while true { + println!("Hello, world!"); + break thing; + }; + + let _ = while true { + println!("Hello, world!"); + break thing; + }; +} + +fn let_while_loop_triple() { + let _ = while true { + println!("Hello, world!"); + break thing; + }; + + let _ = while true { + println!("Hello, world!"); + break thing; + }; + + let _ = while true { + println!("Hello, world!"); + break thing; + }; +} + +fn let_while_loop_quad() { + let _ = while true { + println!("Hello, world!"); + break thing; + }; + + let _ = while true { + println!("Hello, world!"); + break thing; + }; + + let _ = while true { + println!("Hello, world!"); + break thing; + }; + + let _ = while true { + println!("Hello, world!"); + break thing; + }; +} + +fn if_block() { + if true { + println!("Hello, world!"); + } +} + +fn if_double() { + if true { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } +} + +fn if_triple() { + if true { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } +} + +fn if_quad() { + if true { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } +} + +fn let_if_block() { + let _ = if true { + println!("Hello, world!"); + }; +} + +fn let_if_double() { + let _ = if true { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + }; +} + +fn let_if_triple() { + let _ = if true { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + }; +} + +fn let_if_quad() { + let _ = if true { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + }; +} + +fn if_else_block() { + if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } +} + +fn if_else_double() { + if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } +} + +fn if_else_triple() { + if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } +} + +fn if_else_quad() { + if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } +} + +fn let_if_else_block() { + let _ = if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; +} + +fn let_if_else_double() { + let _ = if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; +} + +fn let_if_else_triple() { + let _ = if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; +} + +fn let_if_else_quad() { + let _ = if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; +} + +fn else_if_block() { + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } +} + +fn else_if_double() { + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } +} + +fn else_if_triple() { + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } +} + +fn else_if_quad() { + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } +} + +fn let_if_else_if_block() { + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + }; +} + +fn let_if_else_if_else_double() { + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + }; +} + +fn let_if_else_if_else_triple() { + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + }; +} + +fn let_if_else_if_else_quad() { + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + }; +} + +fn if_else_if_else_block() { + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } +} + +fn if_else_if_else_double() { + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } +} + +fn if_else_if_else_triple() { + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } +} + +fn if_else_if_else_quad() { + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } + + if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + } +} + +fn let_if_else_if_else_block() { + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; +} + +fn let_if_else_if_else_double() { + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; +} + +fn let_if_else_if_else_triple() { + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; +} + +fn let_if_else_if_else_quad() { + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; + + let _ = if true { + println!("Hello, world!"); + } else if false { + println!("Hello, world!"); + } else { + println!("Hello, world!"); + }; +} + +fn loop_block() { + loop { + println!("Hello, world!"); + break; + } +} + +fn loop_double() { + loop { + println!("Hello, world!"); + break; + } + + loop { + println!("Hello, world!"); + break; + } +} + +fn loop_triple() { + loop { + println!("Hello, world!"); + break; + } + + loop { + println!("Hello, world!"); + break; + } + + loop { + println!("Hello, world!"); + break; + } +} + +fn for_loop_quad() { + loop { + println!("Hello, world!"); + break; + } + + loop { + println!("Hello, world!"); + break; + } + + loop { + println!("Hello, world!"); + break; + } + + loop { + println!("Hello, world!"); + break; + } +} + +fn let_loop_block() { + let _ = loop { + println!("Hello, world!"); + break; + }; +} + +fn let_loop_double() { + let _ = loop { + println!("Hello, world!"); + break; + }; + + let _ = loop { + println!("Hello, world!"); + break; + }; +} + +fn let_loop_triple() { + let _ = loop { + println!("Hello, world!"); + break; + }; + + let _ = loop { + println!("Hello, world!"); + break; + }; + + let _ = loop { + println!("Hello, world!"); + break; + }; +} + +fn let_loop_quad() { + let _ = loop { + println!("Hello, world!"); + break; + }; + + let _ = loop { + println!("Hello, world!"); + break; + }; + + let _ = loop { + println!("Hello, world!"); + break; + }; + + let _ = loop { + println!("Hello, world!"); + break; + }; +} + +fn match_block() { + match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + } +} + +fn match_double() { + match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + } + + match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + } +} + +fn match_triple() { + match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + } + + match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + } + + match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + } +} + +fn match_quad() { + match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + } + + match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + } + + match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + } + + match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + } +} + +fn let_match_block() { + let _ = match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + }; +} + +fn let_match_double() { + let _ = match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + }; + + let _ = match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + }; +} + +fn let_match_triple() { + let _ = match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + }; + + let _ = match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + }; + + let _ = match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + }; +} + +fn let_match_quad() { + let _ = match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + }; + + let _ = match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + }; + + let _ = match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + }; + + let _ = match true { + true => println!("Hello, world!"), + false => println!("Hello, world!"), + }; +} + +fn let_var_with_block() { + let _ = { + println!("Hello, world!"); + }; +} + +fn let_var_with_double() { + let _ = { + println!("Hello, world!"); + }; + + let _ = { + println!("Hello, world!"); + }; +} + +fn let_var_with_triple() { + let _ = { + println!("Hello, world!"); + }; + + let _ = { + println!("Hello, world!"); + }; + + let _ = { + println!("Hello, world!"); + }; +} + +fn let_var_with_quad() { + let _ = { + println!("Hello, world!"); + }; + + let _ = { + println!("Hello, world!"); + }; + + let _ = { + println!("Hello, world!"); + }; + + let _ = { + println!("Hello, world!"); + }; +}