diff --git a/clippy_lints/src/returns.rs b/clippy_lints/src/returns.rs index d53a64c98..8976d73fc 100644 --- a/clippy_lints/src/returns.rs +++ b/clippy_lints/src/returns.rs @@ -7,7 +7,7 @@ use syntax::source_map::Span; use syntax::visit::FnKind; use syntax_pos::BytePos; -use crate::utils::{in_macro_or_desugar, match_path_ast, snippet_opt, span_lint_and_then, span_note_and_lint}; +use crate::utils::{in_macro_or_desugar, match_path_ast, snippet_opt, span_lint_and_then}; declare_clippy_lint! { /// **What it does:** Checks for return statements at the end of a block. @@ -164,13 +164,28 @@ impl Return { if match_path_ast(path, &[&*ident.name.as_str()]); if !in_external_macro(cx.sess(), initexpr.span); then { - span_note_and_lint(cx, - LET_AND_RETURN, - retexpr.span, - "returning the result of a let binding from a block. \ - Consider returning the expression directly.", - initexpr.span, - "this expression can be directly returned"); + span_lint_and_then( + cx, + LET_AND_RETURN, + retexpr.span, + "returning the result of a let binding from a block", + |err| { + err.span_label(local.span, "unnecessary let binding"); + + if let Some(snippet) = snippet_opt(cx, initexpr.span) { + err.multipart_suggestion( + "return the expression directly", + vec![ + (local.span, String::new()), + (retexpr.span, snippet), + ], + Applicability::MachineApplicable, + ); + } else { + err.span_help(initexpr.span, "this expression can be directly returned"); + } + }, + ); } } } diff --git a/tests/ui/author/matches.stderr b/tests/ui/author/matches.stderr index fa7e5cce4..74d070dc4 100644 --- a/tests/ui/author/matches.stderr +++ b/tests/ui/author/matches.stderr @@ -1,15 +1,17 @@ -error: returning the result of a let binding from a block. Consider returning the expression directly. +error: returning the result of a let binding from a block --> $DIR/matches.rs:9:13 | +LL | let x = 3; + | ---------- unnecessary let binding LL | x | ^ | = note: `-D clippy::let-and-return` implied by `-D warnings` -note: this expression can be directly returned - --> $DIR/matches.rs:8:21 +help: return the expression directly + | +LL | +LL | 3 | -LL | let x = 3; - | ^ error: aborting due to previous error diff --git a/tests/ui/let_return.stderr b/tests/ui/let_return.stderr index 69c4720c9..319f45c0a 100644 --- a/tests/ui/let_return.stderr +++ b/tests/ui/let_return.stderr @@ -1,27 +1,30 @@ -error: returning the result of a let binding from a block. Consider returning the expression directly. +error: returning the result of a let binding from a block --> $DIR/let_return.rs:7:5 | +LL | let x = 5; + | ---------- unnecessary let binding LL | x | ^ | = note: `-D clippy::let-and-return` implied by `-D warnings` -note: this expression can be directly returned - --> $DIR/let_return.rs:6:13 +help: return the expression directly + | +LL | +LL | 5 | -LL | let x = 5; - | ^ -error: returning the result of a let binding from a block. Consider returning the expression directly. +error: returning the result of a let binding from a block --> $DIR/let_return.rs:13:9 | +LL | let x = 5; + | ---------- unnecessary let binding LL | x | ^ +help: return the expression directly | -note: this expression can be directly returned - --> $DIR/let_return.rs:12:17 +LL | +LL | 5 | -LL | let x = 5; - | ^ error: aborting due to 2 previous errors