Skip to content

Comments between assignment (without let) lhs and rhs handling #4661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/formatting/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<String> {
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.
Expand Down
76 changes: 76 additions & 0 deletions tests/source/lhs-to-rhs-between-comments/assignment.rs
Original file line number Diff line number Diff line change
@@ -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;
}

97 changes: 97 additions & 0 deletions tests/target/lhs-to-rhs-between-comments/assignment.rs
Original file line number Diff line number Diff line change
@@ -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;
}