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..0cf7d6a3021 100644 --- a/src/formatting/visitor.rs +++ b/src/formatting/visitor.rs @@ -7,7 +7,7 @@ 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}, 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 +263,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); + 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(); + + 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()