Skip to content

Commit

Permalink
Auto merge of #12777 - roife:merge-fixes-needless-late-init, r=Alexendoo
Browse files Browse the repository at this point in the history
fix: merge multiple suggestions into a single multi-span suggestion in `needless_late_init`

See rust-lang/rust-analyzer#17163 (comment).

Currently, the fix for `needless_late_init` would modify multiple parts in the file. However, these modifications are exported as separate suggestions instead of a unified 'multi-part suggestion'.

Consequently, rust-analyzer is unable to perform the fix correctly when applying suggestions automatically, as only one suggestion is processed at a time. This PR addresses this issue by merge all modifications into a single multi-part suggestion.

changelog: [`needless_late_init`]: merge multiple fixes into a single multi-span fix.
  • Loading branch information
bors committed May 8, 2024
2 parents befb659 + 362ef42 commit 30b3b73
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 142 deletions.
69 changes: 23 additions & 46 deletions clippy_lints/src/needless_late_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,85 +273,62 @@ fn check<'tcx>(
msg_span,
"unneeded late initialization",
|diag| {
diag.tool_only_span_suggestion(
local_stmt.span,
"remove the local",
"",
Applicability::MachineApplicable,
);

diag.span_suggestion(
assign.lhs_span,
format!("declare `{binding_name}` here"),
let_snippet,
diag.multipart_suggestion(
format!("move the declaration `{binding_name}` here"),
vec![(local_stmt.span, String::new()), (assign.lhs_span, let_snippet)],
Applicability::MachineApplicable,
);
},
);
},
ExprKind::If(cond, then_expr, Some(else_expr)) if !contains_let(cond) => {
let (applicability, suggestions) = assignment_suggestions(cx, binding_id, [then_expr, else_expr])?;
let (applicability, mut suggestions) = assignment_suggestions(cx, binding_id, [then_expr, else_expr])?;

span_lint_and_then(
cx,
NEEDLESS_LATE_INIT,
local_stmt.span,
"unneeded late initialization",
|diag| {
diag.tool_only_span_suggestion(local_stmt.span, "remove the local", String::new(), applicability);

diag.span_suggestion_verbose(
usage.stmt.span.shrink_to_lo(),
format!("declare `{binding_name}` here"),
format!("{let_snippet} = "),
applicability,
);

diag.multipart_suggestion("remove the assignments from the branches", suggestions, applicability);
suggestions.push((local_stmt.span, String::new()));
suggestions.push((usage.stmt.span.shrink_to_lo(), format!("{let_snippet} = ")));

if usage.needs_semi {
diag.span_suggestion(
usage.stmt.span.shrink_to_hi(),
"add a semicolon after the `if` expression",
";",
applicability,
);
suggestions.push((usage.stmt.span.shrink_to_hi(), ";".to_owned()));
}

diag.multipart_suggestion(
format!(
"move the declaration `{binding_name}` here and remove the assignments from the branches"
),
suggestions,
applicability,
);
},
);
},
ExprKind::Match(_, arms, MatchSource::Normal) => {
let (applicability, suggestions) = assignment_suggestions(cx, binding_id, arms.iter().map(|arm| arm.body))?;
let (applicability, mut suggestions) =
assignment_suggestions(cx, binding_id, arms.iter().map(|arm| arm.body))?;

span_lint_and_then(
cx,
NEEDLESS_LATE_INIT,
local_stmt.span,
"unneeded late initialization",
|diag| {
diag.tool_only_span_suggestion(local_stmt.span, "remove the local", String::new(), applicability);
suggestions.push((local_stmt.span, String::new()));
suggestions.push((usage.stmt.span.shrink_to_lo(), format!("{let_snippet} = ")));

diag.span_suggestion_verbose(
usage.stmt.span.shrink_to_lo(),
format!("declare `{binding_name}` here"),
format!("{let_snippet} = "),
applicability,
);
if usage.needs_semi {
suggestions.push((usage.stmt.span.shrink_to_hi(), ";".to_owned()));
}

diag.multipart_suggestion(
"remove the assignments from the `match` arms",
format!("move the declaration `{binding_name}` here and remove the assignments from the `match` arms"),
suggestions,
applicability,
);

if usage.needs_semi {
diag.span_suggestion(
usage.stmt.span.shrink_to_hi(),
"add a semicolon after the `match` expression",
";",
applicability,
);
}
},
);
},
Expand Down
Loading

0 comments on commit 30b3b73

Please sign in to comment.