diff --git a/src/formatting/expr.rs b/src/formatting/expr.rs index 421f7ab6655..0cbd9ac3587 100644 --- a/src/formatting/expr.rs +++ b/src/formatting/expr.rs @@ -194,11 +194,11 @@ pub(crate) fn format_expr( ast::ExprKind::Path(ref qself, ref path) => { rewrite_path(context, PathContext::Expr, qself.as_ref(), path, shape) } - ast::ExprKind::Assign(ref lhs, ref rhs, _) => { - rewrite_assignment(context, lhs, rhs, None, shape) + ast::ExprKind::Assign(ref lhs, ref rhs, op_span) => { + rewrite_assignment(context, lhs, rhs, op_span, shape) } ast::ExprKind::AssignOp(ref op, ref lhs, ref rhs) => { - rewrite_assignment(context, lhs, rhs, Some(op), shape) + rewrite_assignment(context, lhs, rhs, op.span, shape) } ast::ExprKind::Continue(ref opt_label) => { let id_str = match *opt_label { @@ -1895,19 +1895,24 @@ fn rewrite_assignment( context: &RewriteContext<'_>, lhs: &ast::Expr, rhs: &ast::Expr, - op: Option<&ast::BinOp>, + op_span: Span, shape: Shape, ) -> Option { - let operator_str = match op { - Some(op) => context.snippet(op.span), - None => "=", - }; - // 1 = space between lhs and operator. + let operator_str = context.snippet(op_span); let lhs_shape = shape.sub_width(operator_str.len() + 1)?; let lhs_str = format!("{} {}", lhs.rewrite(context, lhs_shape)?, operator_str); - rewrite_assign_rhs(context, lhs_str, rhs, shape) + let comments_span = mk_sp(op_span.hi(), rhs.span.lo()); + rewrite_assign_rhs_with_comments( + context, + lhs_str, + rhs, + shape, + RhsTactics::Default, + comments_span, + true, + ) } /// Controls where to put the rhs. diff --git a/tests/source/lhs-to-rhs-between-comments/assignment.rs b/tests/source/lhs-to-rhs-between-comments/assignment.rs new file mode 100644 index 00000000000..ed53ed5f401 --- /dev/null +++ b/tests/source/lhs-to-rhs-between-comments/assignment.rs @@ -0,0 +1,76 @@ +// "=" Assignemnt +fn main () { + let var = first; + var = second; + } + fn main () { + let var = /* Block comment */ first; + var = /* Block comment */ second; + } + fn main () { + let var = // Line comment + first; + var = // Line comment + second; + } + fn main () { + let var = /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ first; + var = /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ second; + } + fn main () { + let var = /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ first; + var = /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ second; + } + fn main () { + let var = /* Block comment line 1 + * Block comment line 2 + * Block comment line 3 */ first; + var = /* Block comment line 1 + * Block comment line 2 + * Block comment line 3 */ second; + var = /* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg + * Block comment line 2 + * Block comment line 3 */ third; + var = /* Block comment line 1 + * Block comment line 2 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg + * Block comment line 3 */ forth; + } + + // BinOp Assigment + fn main () { + let var = first; + var += second; + } + fn main () { + let var = /* Block comment */ first; + var -= /* Block comment */ second; + } + fn main () { + let var = // Line comment + first; + var *= // Line comment + second; + } + fn main () { + let var = /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ first; + var /= /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ second; + } + fn main () { + let var = /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ first; + var /= /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ second; + } + fn main () { + let var = /* Block comment line 1 + * Block comment line 2 + * Block comment line 3 */ first; + var += /* Block comment line 1 + * Block comment line 2 + * Block comment line 3 */ second; + var -= /* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg + * Block comment line 2 + * Block comment line 3 */ third; + var *= /* Block comment line 1 + * Block comment line 2 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg + * Block comment line 3 */ forth; + } + \ No newline at end of file diff --git a/tests/target/lhs-to-rhs-between-comments/assignment.rs b/tests/target/lhs-to-rhs-between-comments/assignment.rs new file mode 100644 index 00000000000..de56d2ab4a1 --- /dev/null +++ b/tests/target/lhs-to-rhs-between-comments/assignment.rs @@ -0,0 +1,97 @@ +// "=" Assignemnt +fn main() { + let var = first; + var = second; +} +fn main() { + let var = /* Block comment */ first; + var = /* Block comment */ second; +} +fn main() { + let var = // Line comment + first; + var = // Line comment + second; +} +fn main() { + let var = /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ + first; + var = /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ + second; +} +fn main() { + let var = + /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ + first; + var = + /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ + second; +} +fn main() { + let var = /* Block comment line 1 + * Block comment line 2 + * Block comment line 3 */ + first; + var = /* Block comment line 1 + * Block comment line 2 + * Block comment line 3 */ + second; + var = + /* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg + * Block comment line 2 + * Block comment line 3 */ + third; + var = /* Block comment line 1 + * Block comment line 2 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg + * Block comment line 3 */ + forth; +} + +// BinOp Assigment +fn main() { + let var = first; + var += second; +} +fn main() { + let var = /* Block comment */ first; + var -= /* Block comment */ second; +} +fn main() { + let var = // Line comment + first; + var *= // Line comment + second; +} +fn main() { + let var = /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ + first; + var /= /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ + second; +} +fn main() { + let var = + /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ + first; + var /= + /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ + second; +} +fn main() { + let var = /* Block comment line 1 + * Block comment line 2 + * Block comment line 3 */ + first; + var += /* Block comment line 1 + * Block comment line 2 + * Block comment line 3 */ + second; + var -= + /* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg + * Block comment line 2 + * Block comment line 3 */ + third; + var *= /* Block comment line 1 + * Block comment line 2 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg + * Block comment line 3 */ + forth; +}