diff --git a/src/formatting/items.rs b/src/formatting/items.rs index ade5e1e8b16..bd94f577ea6 100644 --- a/src/formatting/items.rs +++ b/src/formatting/items.rs @@ -349,12 +349,7 @@ impl<'a> FmtVisitor<'a> { self.last_pos = item.span.lo() + BytePos(brace_pos as u32 + 1); self.block_indent = self.block_indent.block_indent(self.config); - if item.body.is_empty() { - self.format_missing_no_indent(item.span.hi() - BytePos(1)); - self.block_indent = self.block_indent.block_unindent(self.config); - let indent_str = self.block_indent.to_string(self.config); - self.push_str(&indent_str); - } else { + if !item.body.is_empty() { let first_non_ws = item.body.first().map(|s| s.span().lo()); if let Some(opening_nls) = self.advance_to_first_block_item(first_non_ws) { self.push_str(&opening_nls); @@ -363,10 +358,11 @@ impl<'a> FmtVisitor<'a> { for item in &item.body { self.format_body_element(item); } - - self.block_indent = self.block_indent.block_unindent(self.config); - self.format_missing_with_indent(item.span.hi() - BytePos(1)); } + self.format_missing_no_indent(item.span.hi() - BytePos(1)); + self.block_indent = self.block_indent.block_unindent(self.config); + let indent_str = self.block_indent.to_string(self.config); + self.push_str(&indent_str); self.push_str("}"); } else { diff --git a/src/formatting/visitor.rs b/src/formatting/visitor.rs index dea632f703a..06917a704cb 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::{rewrite_comment, CodeCharKind, CommentCodeSlices}, + comment::{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, @@ -316,8 +316,17 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { self.block_indent = self.block_indent.block_unindent(config); } + let comment_snippet = self.snippet(span); + + let align_to_right = if unindent_comment && contains_comment(&comment_snippet) { + let first_lines = comment_snippet.splitn(2, '/').next().unwrap_or(""); + last_line_width(first_lines) > last_line_width(&comment_snippet) + } else { + false + }; + let mut iter = CommentCodeSlices::with_offset( - self.snippet(span), + comment_snippet, last_line_offset, self.config.tab_spaces(), ) @@ -380,8 +389,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { _ => self.push_str("\n"), } newline_inserted = true; - + if unindent_comment && align_to_right { + self.block_indent = self.block_indent.block_indent(self.config); + } self.push_str(&self.block_indent.to_string_with_newline(config)); + if unindent_comment && align_to_right { + self.block_indent = self.block_indent.block_unindent(self.config); + } } } prev_kind = kind; diff --git a/tests/source/issue-4120.rs b/tests/source/issue-4120.rs new file mode 100644 index 00000000000..c9ce838c51a --- /dev/null +++ b/tests/source/issue-4120.rs @@ -0,0 +1,85 @@ +fn main() { + let x = if true { + 1 + // In if + } else { + 0 + // In else + }; + + let x = if true { + 1 + /* In if */ + } else { + 0 + /* In else */ + }; + + let z = if true { + if true { + 1 + + // In if level 2 + } else { + 2 + } + } else { + 3 + }; + + let a = if true { + 1 + // In if + } else { + 0 + // In else + }; + + let a = if true { + 1 + + // In if + } else { + 0 + // In else + }; + + let b = if true { + 1 + + // In if + } else { + 0 + // In else + }; + + let c = if true { + 1 + + // In if + } else { + 0 + // In else + }; + for i in 0..2 { + println!("Something"); + // In for + } + + for i in 0..2 { + println!("Something"); + /* In for */ + } + + extern "C" { + fn first(); + + // In foreign mod + } + + extern "C" { + fn first(); + + /* In foreign mod */ + } +} diff --git a/tests/target/issue-4120.rs b/tests/target/issue-4120.rs new file mode 100644 index 00000000000..a7d461dcfdb --- /dev/null +++ b/tests/target/issue-4120.rs @@ -0,0 +1,85 @@ +fn main() { + let x = if true { + 1 + // In if + } else { + 0 + // In else + }; + + let x = if true { + 1 + /* In if */ + } else { + 0 + /* In else */ + }; + + let z = if true { + if true { + 1 + + // In if level 2 + } else { + 2 + } + } else { + 3 + }; + + let a = if true { + 1 + // In if + } else { + 0 + // In else + }; + + let a = if true { + 1 + + // In if + } else { + 0 + // In else + }; + + let b = if true { + 1 + + // In if + } else { + 0 + // In else + }; + + let c = if true { + 1 + + // In if + } else { + 0 + // In else + }; + for i in 0..2 { + println!("Something"); + // In for + } + + for i in 0..2 { + println!("Something"); + /* In for */ + } + + extern "C" { + fn first(); + + // In foreign mod + } + + extern "C" { + fn first(); + + /* In foreign mod */ + } +}