From 2ee8b0e4c533dfa07e25a03a84b28452d68c52eb Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Fri, 18 May 2018 16:56:55 +1200 Subject: [PATCH] Remove `remove_blank_lines_at_start_or_end_of_block` cc #1974 --- Configurations.md | 39 ----------------- src/config/mod.rs | 2 - src/visitor.rs | 104 ++++++++++++++++++++++------------------------ 3 files changed, 50 insertions(+), 95 deletions(-) diff --git a/Configurations.md b/Configurations.md index 713ecbdab37..8c893f98d43 100644 --- a/Configurations.md +++ b/Configurations.md @@ -1991,45 +1991,6 @@ fn bar() { } ``` -## `remove_blank_lines_at_start_or_end_of_block` - -Remove blank lines at the start or the end of a block. - -- **Default value**: `true` -- **Possible values**: `true`, `false` -- **Stable**: No - -#### `true` - -```rust -fn foo() { - let msg = { - let mut str = String::new(); - str.push_str("hello, "); - str.push_str("world!"); - str - }; - println!("{}", msg); -} -``` - -#### `false` - -```rust -fn foo() { - - let msg = { - - let mut str = String::new(); - str.push_str("hello, "); - str.push_str("world!"); - str - - }; - println!("{}", msg); - -} -``` ## `required_version` diff --git a/src/config/mod.rs b/src/config/mod.rs index 1d76a06e3b5..e97a351a52a 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -83,8 +83,6 @@ create_config! { // Misc. remove_nested_parens: bool, true, true, "Remove nested parens."; - remove_blank_lines_at_start_or_end_of_block: bool, true, false, - "Remove blank lines at start or end of a block"; combine_control_expr: bool, true, false, "Combine control expressions with function calls."; struct_field_align_threshold: usize, 0, false, "Align struct fields if their diffs fits within \ threshold."; diff --git a/src/visitor.rs b/src/visitor.rs index ec1890035a7..a5f0c4441f6 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -128,45 +128,43 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { self.block_indent = self.block_indent.block_indent(self.config); self.push_str("{"); - if self.config.remove_blank_lines_at_start_or_end_of_block() { - if let Some(first_stmt) = b.stmts.first() { - let attr_lo = inner_attrs - .and_then(|attrs| inner_attributes(attrs).first().map(|attr| attr.span.lo())) - .or_else(|| { - // Attributes for an item in a statement position - // do not belong to the statement. (rust-lang/rust#34459) - if let ast::StmtKind::Item(ref item) = first_stmt.node { - item.attrs.first() - } else { - first_stmt.attrs().first() - }.and_then(|attr| { - // Some stmts can have embedded attributes. - // e.g. `match { #![attr] ... }` - let attr_lo = attr.span.lo(); - if attr_lo < first_stmt.span.lo() { - Some(attr_lo) - } else { - None - } - }) - }); - - let snippet = self.snippet(mk_sp( - self.last_pos, - attr_lo.unwrap_or_else(|| first_stmt.span.lo()), - )); - let len = CommentCodeSlices::new(snippet) - .nth(0) - .and_then(|(kind, _, s)| { - if kind == CodeCharKind::Normal { - s.rfind('\n') + if let Some(first_stmt) = b.stmts.first() { + let attr_lo = inner_attrs + .and_then(|attrs| inner_attributes(attrs).first().map(|attr| attr.span.lo())) + .or_else(|| { + // Attributes for an item in a statement position + // do not belong to the statement. (rust-lang/rust#34459) + if let ast::StmtKind::Item(ref item) = first_stmt.node { + item.attrs.first() + } else { + first_stmt.attrs().first() + }.and_then(|attr| { + // Some stmts can have embedded attributes. + // e.g. `match { #![attr] ... }` + let attr_lo = attr.span.lo(); + if attr_lo < first_stmt.span.lo() { + Some(attr_lo) } else { None } - }); - if let Some(len) = len { - self.last_pos = self.last_pos + BytePos::from_usize(len); - } + }) + }); + + let snippet = self.snippet(mk_sp( + self.last_pos, + attr_lo.unwrap_or_else(|| first_stmt.span.lo()), + )); + let len = CommentCodeSlices::new(snippet) + .nth(0) + .and_then(|(kind, _, s)| { + if kind == CodeCharKind::Normal { + s.rfind('\n') + } else { + None + } + }); + if let Some(len) = len { + self.last_pos = self.last_pos + BytePos::from_usize(len); } } @@ -195,24 +193,22 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { } let mut remove_len = BytePos(0); - if self.config.remove_blank_lines_at_start_or_end_of_block() { - if let Some(stmt) = b.stmts.last() { - let snippet = self.snippet(mk_sp( - stmt.span.hi(), - source!(self, b.span).hi() - brace_compensation, - )); - let len = CommentCodeSlices::new(snippet) - .last() - .and_then(|(kind, _, s)| { - if kind == CodeCharKind::Normal && s.trim().is_empty() { - Some(s.len()) - } else { - None - } - }); - if let Some(len) = len { - remove_len = BytePos::from_usize(len); - } + if let Some(stmt) = b.stmts.last() { + let snippet = self.snippet(mk_sp( + stmt.span.hi(), + source!(self, b.span).hi() - brace_compensation, + )); + let len = CommentCodeSlices::new(snippet) + .last() + .and_then(|(kind, _, s)| { + if kind == CodeCharKind::Normal && s.trim().is_empty() { + Some(s.len()) + } else { + None + } + }); + if let Some(len) = len { + remove_len = BytePos::from_usize(len); } }