From e836a2f411b24b8f2db4366133d73e911fd71cc1 Mon Sep 17 00:00:00 2001 From: WhizSid Date: Tue, 9 Mar 2021 02:52:47 +0530 Subject: [PATCH 1/2] Not moving the first line comment of blocks --- src/formatting/syntux/session.rs | 10 ++++++ src/formatting/visitor.rs | 35 ++++++++++++++++++- tests/source/issue-3255.rs | 26 ++++++++++++++ tests/target/async_fn.rs | 3 +- .../control-brace-style-always-next-line.rs | 3 +- .../control-brace-style-always-same-line.rs | 3 +- tests/target/issue-3255.rs | 27 ++++++++++++++ tests/target/match.rs | 3 +- 8 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 tests/source/issue-3255.rs create mode 100644 tests/target/issue-3255.rs diff --git a/src/formatting/syntux/session.rs b/src/formatting/syntux/session.rs index 3c21a3f2425..0099ff21dbb 100644 --- a/src/formatting/syntux/session.rs +++ b/src/formatting/syntux/session.rs @@ -1,4 +1,5 @@ use std::cell::RefCell; +use std::ops::Range; use std::path::Path; use std::rc::Rc; @@ -189,6 +190,15 @@ impl ParseSess { } } + pub(crate) fn line_bounds(&self, pos: BytePos) -> Option> { + let line = self.parse_sess.source_map().lookup_line(pos).ok(); + + match line { + Some(line_info) => Some(line_info.sf.line_bounds(line_info.line)), + None => None, + } + } + pub(crate) fn line_of_byte_pos(&self, pos: BytePos) -> usize { self.parse_sess.source_map().lookup_char_pos(pos).line } diff --git a/src/formatting/visitor.rs b/src/formatting/visitor.rs index 2a0af48c84a..c11dd48939d 100644 --- a/src/formatting/visitor.rs +++ b/src/formatting/visitor.rs @@ -7,7 +7,10 @@ use rustc_span::{symbol, BytePos, Pos, Span, DUMMY_SP}; use crate::config::{BraceStyle, Config}; use crate::formatting::{ attr::*, - comment::{contains_comment, rewrite_comment, CodeCharKind, CommentCodeSlices}, + comment::{ + comment_style, contains_comment, rewrite_comment, CodeCharKind, CommentCodeSlices, + FindUncommented, + }, items::{ format_impl, format_trait, format_trait_alias, is_mod_decl, is_use_item, rewrite_associated_impl_type, rewrite_extern_crate, rewrite_opaque_impl_type, @@ -263,6 +266,36 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { self.block_indent = self.block_indent.block_indent(self.config); self.push_str("{"); + if has_braces { + let block_line_range = self.parse_sess.lookup_line_range(b.span); + let first_line_contains_stmt = if let Some(first_stmt) = b.stmts.first() { + self.parse_sess.lookup_line_range(first_stmt.span).lo == block_line_range.lo + } else { + false + }; + + let first_line_bounds = self.parse_sess.line_bounds(self.last_pos).unwrap(); + if block_line_range.lo != block_line_range.hi { // Skipping if a single line block + let first_line_snip = self + .snippet(mk_sp(self.last_pos, first_line_bounds.end)) + .trim(); + + if !first_line_contains_stmt + && contains_comment(first_line_snip) + && comment_style(first_line_snip, self.config.normalize_comments()) + .is_line_comment() + { + if let Some(comment) = + rewrite_comment(first_line_snip, false, self.shape(), self.config) + { + self.push_str(" "); + self.push_str(&comment); + self.last_pos = self.last_pos + BytePos(first_line_snip.len() as u32); + } + } + } + } + let first_non_ws = inner_attrs .and_then(|attrs| attrs.first().map(|attr| attr.span.lo())) .or_else(|| b.stmts.first().map(|s| s.span().lo())); diff --git a/tests/source/issue-3255.rs b/tests/source/issue-3255.rs new file mode 100644 index 00000000000..027eb45b7a9 --- /dev/null +++ b/tests/source/issue-3255.rs @@ -0,0 +1,26 @@ +fn foo(){ + if true { // Sample comment + 1 + } +} + + +fn foo(){ + if true { + // Sample comment + 1 + } +} + +fn foo(){ + if true { /* Sample comment */ + 1 + } +} + +fn foo(){ + if true { /* Sample + comment */ + 1 + } +} diff --git a/tests/target/async_fn.rs b/tests/target/async_fn.rs index ac151dddb25..1bca39d5114 100644 --- a/tests/target/async_fn.rs +++ b/tests/target/async_fn.rs @@ -13,8 +13,7 @@ async unsafe fn foo() { } async unsafe fn rust() { - async move { - // comment + async move { // comment Ok(()) } } diff --git a/tests/target/control-brace-style-always-next-line.rs b/tests/target/control-brace-style-always-next-line.rs index 2c047761c5c..8c838f37923 100644 --- a/tests/target/control-brace-style-always-next-line.rs +++ b/tests/target/control-brace-style-always-next-line.rs @@ -23,8 +23,7 @@ fn main() { 'while_label: while cond - { - // while comment + { // while comment let foo = (); } diff --git a/tests/target/control-brace-style-always-same-line.rs b/tests/target/control-brace-style-always-same-line.rs index 2b842eb065f..a8d7498dfb8 100644 --- a/tests/target/control-brace-style-always-same-line.rs +++ b/tests/target/control-brace-style-always-same-line.rs @@ -18,8 +18,7 @@ fn main() { } - 'while_label: while cond { - // while comment + 'while_label: while cond { // while comment let foo = (); } diff --git a/tests/target/issue-3255.rs b/tests/target/issue-3255.rs new file mode 100644 index 00000000000..9c87137ba25 --- /dev/null +++ b/tests/target/issue-3255.rs @@ -0,0 +1,27 @@ +fn foo() { + if true { // Sample comment + 1 + } +} + +fn foo() { + if true { + // Sample comment + 1 + } +} + +fn foo() { + if true { + /* Sample comment */ + 1 + } +} + +fn foo() { + if true { + /* Sample + comment */ + 1 + } +} diff --git a/tests/target/match.rs b/tests/target/match.rs index f29f12d8a5b..2505748b5cd 100644 --- a/tests/target/match.rs +++ b/tests/target/match.rs @@ -7,8 +7,7 @@ fn foo() { // Some comment. a => foo(), b if 0 < 42 => foo(), - c => { - // Another comment. + c => { // Another comment. // Comment. an_expression; foo() From 6beaf1b76d9d4fd66f056baab9c0aaebd7f7e34b Mon Sep 17 00:00:00 2001 From: WhizSid Date: Tue, 9 Mar 2021 03:03:13 +0530 Subject: [PATCH 2/2] Removing unusable allocations when formatting empty blocks --- src/formatting/visitor.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/formatting/visitor.rs b/src/formatting/visitor.rs index c11dd48939d..0cf7d6a3021 100644 --- a/src/formatting/visitor.rs +++ b/src/formatting/visitor.rs @@ -7,10 +7,7 @@ use rustc_span::{symbol, BytePos, Pos, Span, DUMMY_SP}; use crate::config::{BraceStyle, Config}; use crate::formatting::{ attr::*, - comment::{ - comment_style, contains_comment, rewrite_comment, CodeCharKind, CommentCodeSlices, - FindUncommented, - }, + comment::{comment_style, contains_comment, rewrite_comment, CodeCharKind, CommentCodeSlices}, items::{ format_impl, format_trait, format_trait_alias, is_mod_decl, is_use_item, rewrite_associated_impl_type, rewrite_extern_crate, rewrite_opaque_impl_type, @@ -268,14 +265,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { if has_braces { let block_line_range = self.parse_sess.lookup_line_range(b.span); - let first_line_contains_stmt = if let Some(first_stmt) = b.stmts.first() { - self.parse_sess.lookup_line_range(first_stmt.span).lo == block_line_range.lo - } else { - false - }; - - let first_line_bounds = self.parse_sess.line_bounds(self.last_pos).unwrap(); if block_line_range.lo != block_line_range.hi { // Skipping if a single line block + let first_line_contains_stmt = if let Some(first_stmt) = b.stmts.first() { + self.parse_sess.lookup_line_range(first_stmt.span).lo == block_line_range.lo + } else { + false + }; + + let first_line_bounds = self.parse_sess.line_bounds(self.last_pos).unwrap(); let first_line_snip = self .snippet(mk_sp(self.last_pos, first_line_bounds.end)) .trim();