Skip to content

Commit 5187e92

Browse files
committed
Auto merge of rust-lang#10819 - Alexendoo:needless-returns-suggestion, r=giraffate
Display the `needless_return` suggestion Fixes rust-lang#10816 Makes it a multipart suggestion so it can be displayed in a single frame which also fixes rust-lang#10816 (comment) changelog: [`needless_return`]: Display the suggested change
2 parents a0fd17d + 47a024e commit 5187e92

File tree

2 files changed

+283
-67
lines changed

2 files changed

+283
-67
lines changed

clippy_lints/src/returns.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ declare_clippy_lint! {
7070
"using a return statement like `return expr;` where an expression would suffice"
7171
}
7272

73-
#[derive(PartialEq, Eq, Clone)]
73+
#[derive(PartialEq, Eq)]
7474
enum RetReplacement<'tcx> {
7575
Empty,
7676
Block,
@@ -80,18 +80,19 @@ enum RetReplacement<'tcx> {
8080
}
8181

8282
impl<'tcx> RetReplacement<'tcx> {
83-
fn sugg_help(self) -> &'static str {
83+
fn sugg_help(&self) -> &'static str {
8484
match self {
8585
Self::Empty | Self::Expr(..) => "remove `return`",
8686
Self::Block => "replace `return` with an empty block",
8787
Self::Unit => "replace `return` with a unit value",
8888
Self::IfSequence(..) => "remove `return` and wrap the sequence with parentheses",
8989
}
9090
}
91-
fn applicability(&self) -> Option<Applicability> {
91+
92+
fn applicability(&self) -> Applicability {
9293
match self {
93-
Self::Expr(_, ap) | Self::IfSequence(_, ap) => Some(*ap),
94-
_ => None,
94+
Self::Expr(_, ap) | Self::IfSequence(_, ap) => *ap,
95+
_ => Applicability::MachineApplicable,
9596
}
9697
}
9798
}
@@ -271,7 +272,7 @@ fn check_final_expr<'tcx>(
271272
return;
272273
}
273274

274-
emit_return_lint(cx, ret_span, semi_spans, replacement);
275+
emit_return_lint(cx, ret_span, semi_spans, &replacement);
275276
},
276277
ExprKind::If(_, then, else_clause_opt) => {
277278
check_block_return(cx, &then.kind, peeled_drop_expr.span, semi_spans.clone());
@@ -306,20 +307,17 @@ fn expr_contains_conjunctive_ifs<'tcx>(expr: &'tcx Expr<'tcx>) -> bool {
306307
contains_if(expr, false)
307308
}
308309

309-
fn emit_return_lint(cx: &LateContext<'_>, ret_span: Span, semi_spans: Vec<Span>, replacement: RetReplacement<'_>) {
310+
fn emit_return_lint(cx: &LateContext<'_>, ret_span: Span, semi_spans: Vec<Span>, replacement: &RetReplacement<'_>) {
310311
if ret_span.from_expansion() {
311312
return;
312313
}
313314

314-
let applicability = replacement.applicability().unwrap_or(Applicability::MachineApplicable);
315-
let return_replacement = replacement.to_string();
316-
let sugg_help = replacement.sugg_help();
317315
span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded `return` statement", |diag| {
318-
diag.span_suggestion_hidden(ret_span, sugg_help, return_replacement, applicability);
319-
// for each parent statement, we need to remove the semicolon
320-
for semi_stmt_span in semi_spans {
321-
diag.tool_only_span_suggestion(semi_stmt_span, "remove this semicolon", "", applicability);
322-
}
316+
let suggestions = std::iter::once((ret_span, replacement.to_string()))
317+
.chain(semi_spans.into_iter().map(|span| (span, String::new())))
318+
.collect();
319+
320+
diag.multipart_suggestion_verbose(replacement.sugg_help(), suggestions, replacement.applicability());
323321
});
324322
}
325323

0 commit comments

Comments
 (0)