From 15ec5d26084fe13c984b4e6c2933e048e070680c Mon Sep 17 00:00:00 2001 From: kraktus Date: Sun, 18 Sep 2022 21:26:23 +0200 Subject: [PATCH] further refactor --- clippy_lints/src/returns.rs | 51 ++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/clippy_lints/src/returns.rs b/clippy_lints/src/returns.rs index e4692a8d84b..26b6898e588 100644 --- a/clippy_lints/src/returns.rs +++ b/clippy_lints/src/returns.rs @@ -232,36 +232,29 @@ fn emit_return_lint( if ret_span.from_expansion() { return; } - if let Some(inner_span) = inner_span { - let mut applicability = Applicability::MachineApplicable; - span_lint_hir_and_then( - cx, - NEEDLESS_RETURN, - emission_place, - ret_span, - "unneeded `return` statement", - |diag| { - let (snippet, _) = snippet_with_context(cx, inner_span, ret_span.ctxt(), "..", &mut applicability); - diag.span_suggestion(ret_span, "remove `return`", snippet, applicability); - }, - ); + let mut applicability = Applicability::MachineApplicable; + let return_replacement = inner_span.map_or_else( + || replacement.to_string(), + |inner_span| { + let (snippet, _) = snippet_with_context(cx, inner_span, ret_span.ctxt(), "..", &mut applicability); + snippet.to_string() + }, + ); + let sugg_help = if inner_span.is_some() { + "remove `return`" } else { - span_lint_hir_and_then( - cx, - NEEDLESS_RETURN, - emission_place, - ret_span, - "unneeded `return` statement", - |diag| { - diag.span_suggestion( - ret_span, - replacement.sugg_help(), - replacement.to_string(), - Applicability::MachineApplicable, - ); - }, - ) - } + replacement.sugg_help() + }; + span_lint_hir_and_then( + cx, + NEEDLESS_RETURN, + emission_place, + ret_span, + "unneeded `return` statement", + |diag| { + diag.span_suggestion(ret_span, sugg_help, return_replacement, applicability); + }, + ); } fn last_statement_borrows<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {