Skip to content

Commit

Permalink
Source code for example from issue #6202
Browse files Browse the repository at this point in the history
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
  • Loading branch information
CalebLItalien committed Jul 1, 2024
1 parent 6093d48 commit 9d0428c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
13 changes: 13 additions & 0 deletions tests/source/issue-6202/issue_example.rs
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
14 changes: 14 additions & 0 deletions tests/target/issue-6202/issue_example.rs
Original file line number Diff line number Diff line change
@@ -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());
}
}
}

0 comments on commit 9d0428c

Please sign in to comment.