Skip to content

Commit 362ef42

Browse files
committed
fix: merge multiple suggestions into a single multi-span suggestion in needless_late_init
1 parent befb659 commit 362ef42

File tree

2 files changed

+104
-142
lines changed

2 files changed

+104
-142
lines changed

clippy_lints/src/needless_late_init.rs

+23-46
Original file line numberDiff line numberDiff line change
@@ -273,85 +273,62 @@ fn check<'tcx>(
273273
msg_span,
274274
"unneeded late initialization",
275275
|diag| {
276-
diag.tool_only_span_suggestion(
277-
local_stmt.span,
278-
"remove the local",
279-
"",
280-
Applicability::MachineApplicable,
281-
);
282-
283-
diag.span_suggestion(
284-
assign.lhs_span,
285-
format!("declare `{binding_name}` here"),
286-
let_snippet,
276+
diag.multipart_suggestion(
277+
format!("move the declaration `{binding_name}` here"),
278+
vec![(local_stmt.span, String::new()), (assign.lhs_span, let_snippet)],
287279
Applicability::MachineApplicable,
288280
);
289281
},
290282
);
291283
},
292284
ExprKind::If(cond, then_expr, Some(else_expr)) if !contains_let(cond) => {
293-
let (applicability, suggestions) = assignment_suggestions(cx, binding_id, [then_expr, else_expr])?;
285+
let (applicability, mut suggestions) = assignment_suggestions(cx, binding_id, [then_expr, else_expr])?;
294286

295287
span_lint_and_then(
296288
cx,
297289
NEEDLESS_LATE_INIT,
298290
local_stmt.span,
299291
"unneeded late initialization",
300292
|diag| {
301-
diag.tool_only_span_suggestion(local_stmt.span, "remove the local", String::new(), applicability);
302-
303-
diag.span_suggestion_verbose(
304-
usage.stmt.span.shrink_to_lo(),
305-
format!("declare `{binding_name}` here"),
306-
format!("{let_snippet} = "),
307-
applicability,
308-
);
309-
310-
diag.multipart_suggestion("remove the assignments from the branches", suggestions, applicability);
293+
suggestions.push((local_stmt.span, String::new()));
294+
suggestions.push((usage.stmt.span.shrink_to_lo(), format!("{let_snippet} = ")));
311295

312296
if usage.needs_semi {
313-
diag.span_suggestion(
314-
usage.stmt.span.shrink_to_hi(),
315-
"add a semicolon after the `if` expression",
316-
";",
317-
applicability,
318-
);
297+
suggestions.push((usage.stmt.span.shrink_to_hi(), ";".to_owned()));
319298
}
299+
300+
diag.multipart_suggestion(
301+
format!(
302+
"move the declaration `{binding_name}` here and remove the assignments from the branches"
303+
),
304+
suggestions,
305+
applicability,
306+
);
320307
},
321308
);
322309
},
323310
ExprKind::Match(_, arms, MatchSource::Normal) => {
324-
let (applicability, suggestions) = assignment_suggestions(cx, binding_id, arms.iter().map(|arm| arm.body))?;
311+
let (applicability, mut suggestions) =
312+
assignment_suggestions(cx, binding_id, arms.iter().map(|arm| arm.body))?;
325313

326314
span_lint_and_then(
327315
cx,
328316
NEEDLESS_LATE_INIT,
329317
local_stmt.span,
330318
"unneeded late initialization",
331319
|diag| {
332-
diag.tool_only_span_suggestion(local_stmt.span, "remove the local", String::new(), applicability);
320+
suggestions.push((local_stmt.span, String::new()));
321+
suggestions.push((usage.stmt.span.shrink_to_lo(), format!("{let_snippet} = ")));
333322

334-
diag.span_suggestion_verbose(
335-
usage.stmt.span.shrink_to_lo(),
336-
format!("declare `{binding_name}` here"),
337-
format!("{let_snippet} = "),
338-
applicability,
339-
);
323+
if usage.needs_semi {
324+
suggestions.push((usage.stmt.span.shrink_to_hi(), ";".to_owned()));
325+
}
340326

341327
diag.multipart_suggestion(
342-
"remove the assignments from the `match` arms",
328+
format!("move the declaration `{binding_name}` here and remove the assignments from the `match` arms"),
343329
suggestions,
344330
applicability,
345331
);
346-
347-
if usage.needs_semi {
348-
diag.span_suggestion(
349-
usage.stmt.span.shrink_to_hi(),
350-
"add a semicolon after the `match` expression",
351-
";",
352-
applicability,
353-
);
354-
}
355332
},
356333
);
357334
},

0 commit comments

Comments
 (0)