diff --git a/src/formatting/expr.rs b/src/formatting/expr.rs index 30b9acb5d51..b53496affa8 100644 --- a/src/formatting/expr.rs +++ b/src/formatting/expr.rs @@ -1985,6 +1985,32 @@ pub(crate) fn rewrite_assign_rhs_with, R: Rewrite>( Some(lhs + &rhs) } +pub(crate) fn rewrite_assign_rhs_with_comments, R: Rewrite>( + context: &RewriteContext<'_>, + lhs: S, + ex: &R, + shape: Shape, + rhs_tactics: RhsTactics, + between_span: Span, + allow_extend: bool, +) -> Option { + let lhs = lhs.into(); + let contains_comment = contains_comment(context.snippet(between_span)); + let shape = if contains_comment { + shape.block_left(context.config.tab_spaces())? + } else { + shape + }; + let rhs = rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_tactics)?; + + if contains_comment { + let rhs = rhs.trim_start(); + combine_strs_with_missing_comments(context, &lhs, &rhs, between_span, shape, allow_extend) + } else { + Some(lhs + &rhs) + } +} + fn choose_rhs( context: &RewriteContext<'_>, expr: &R, diff --git a/src/formatting/items.rs b/src/formatting/items.rs index dc6a0649ed2..615526f92ff 100644 --- a/src/formatting/items.rs +++ b/src/formatting/items.rs @@ -19,7 +19,7 @@ use crate::formatting::{ }, expr::{ is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_expr, - rewrite_assign_rhs_with, RhsTactics, + rewrite_assign_rhs_with, rewrite_assign_rhs_with_comments, RhsTactics, }, lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator}, macros::{rewrite_macro, MacroPosition}, @@ -1878,14 +1878,22 @@ fn rewrite_static( }; if let Some(expr) = static_parts.expr_opt { + let comments_lo = context.snippet_provider.span_after(static_parts.span, "="); + let expr_lo = expr.span.lo(); + let comments_span = mk_sp(comments_lo, expr_lo); + let lhs = format!("{}{} =", prefix, ty_str); + // 1 = ; let remaining_width = context.budget(offset.block_indent + 1); - rewrite_assign_rhs( + rewrite_assign_rhs_with_comments( context, &lhs, &**expr, Shape::legacy(remaining_width, offset.block_only()), + RhsTactics::Default, + comments_span, + true, ) .and_then(|res| recover_comment_removed(res, static_parts.span, context)) .or_else(|| { diff --git a/tests/source/issue-4427.rs b/tests/source/issue-4427.rs new file mode 100644 index 00000000000..e14e039b98f --- /dev/null +++ b/tests/source/issue-4427.rs @@ -0,0 +1,31 @@ +const A: usize = + // Some constant + 2; + +const B: usize = +/* constant */ +3; + +const C : usize + = /* foo */5; + +const D: usize = // baz +/* Some constant */ + /* ba */ + { 3 + // foo + }; +const E: usize= /* foo */5; +const F: usize = +{ + 7 + }; +const G: usize = /* foooooooooooooooooooooooooooooooooooooooooooooooooooooooo0000000000000000xx00 */ 5; + const H: usize = /* asdfasdf */ match G > 1 { + true => 1, + false => 3, + }; + + pub static FOO_BAR: Vec = //f + { + vec![]}; diff --git a/tests/target/issue-4427.rs b/tests/target/issue-4427.rs new file mode 100644 index 00000000000..c8a37ead8cb --- /dev/null +++ b/tests/target/issue-4427.rs @@ -0,0 +1,30 @@ +const A: usize = + // Some constant + 2; + +const B: usize = + /* constant */ + 3; + +const C: usize = /* foo */ 5; + +const D: usize = // baz + /* Some constant */ + /* ba */ + { + 3 + // foo + }; +const E: usize = /* foo */ 5; +const F: usize = { 7 }; +const G: usize = + /* foooooooooooooooooooooooooooooooooooooooooooooooooooooooo0000000000000000xx00 */ + 5; +const H: usize = /* asdfasdf */ + match G > 1 { + true => 1, + false => 3, + }; + +pub static FOO_BAR: Vec = //f + { vec![] };