diff --git a/src/expr.rs b/src/expr.rs index 8266f95fd70..8b63c1b5f1d 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1860,7 +1860,11 @@ fn rewrite_let( // TODO(ytmimi) comments could appear between `let` and the `pat` // 4 = "let ".len() - let pat_shape = shape.offset_left(4)?; + let mut pat_shape = shape.offset_left(4)?; + if context.config.version() == Version::Two { + // 2 to account for the length of " =" + pat_shape = pat_shape.sub_width(2)?; + } let pat_str = pat.rewrite(context, pat_shape)?; result.push_str(&pat_str); diff --git a/tests/source/issue-6202/issue_example.rs b/tests/source/issue-6202/issue_example.rs new file mode 100644 index 00000000000..1a3d24699c2 --- /dev/null +++ b/tests/source/issue-6202/issue_example.rs @@ -0,0 +1,13 @@ +// rustfmt-max_width: 120 +// rustfmt-version: Two + +impl EarlyLintPass for NeedlessContinue { + fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { + if let ExprKind::Loop(body, label, ..) | ExprKind::While(_, body, label) | ExprKind::ForLoop { body, label, .. } = + &expr.kind + && !in_external_macro(cx.sess, expr.span) + { + check_final_block_stmt(cx, body, label, expr.span.ctxt()); + } + } +} \ No newline at end of file diff --git a/tests/target/issue-6202/issue_example.rs b/tests/target/issue-6202/issue_example.rs new file mode 100644 index 00000000000..0c8408324c9 --- /dev/null +++ b/tests/target/issue-6202/issue_example.rs @@ -0,0 +1,14 @@ +// rustfmt-max_width: 120 +// rustfmt-version: Two + +impl EarlyLintPass for NeedlessContinue { + fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { + if let ExprKind::Loop(body, label, ..) + | ExprKind::While(_, body, label) + | ExprKind::ForLoop { body, label, .. } = &expr.kind + && !in_external_macro(cx.sess, expr.span) + { + check_final_block_stmt(cx, body, label, expr.span.ctxt()); + } + } +}