From 83af25f1a7c337d3f83909d2a4579d68b717250a Mon Sep 17 00:00:00 2001 From: Caleb L'Italien Date: Sun, 30 Jun 2024 17:24:26 -0400 Subject: [PATCH] Source code for example from issue #6202 Target code from example issue #6202 Corrected indentation and use of spacing for resriting let statements Modified resrite_let to account for trailing ' =' Reduced configs down to what was necessary to reproduce issue Reduced configs down to what was necessary to reproduce issue Documented reason for modifying pat_shape to account for ' =' Moved comment to match rest of codebase's style Fixed issue with accounting for trailing ' =' in let statements Removed trailing space Fixed issue 6202 --- src/expr.rs | 6 +++++- tests/source/issue-6202/issue_example.rs | 13 +++++++++++++ tests/target/issue-6202/issue_example.rs | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 tests/source/issue-6202/issue_example.rs create mode 100644 tests/target/issue-6202/issue_example.rs 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()); + } + } +}