Auto merge of #4137 - euclio:let-return, r=oli-obk

tweak `let_and_return` diagnostic

changelog: `let_and_return`: label the unnecessary let binding and convert the note to a structured
suggestion
This commit is contained in:
bors 2019-05-27 13:04:14 +00:00
commit 7b501f0f6a
3 changed files with 43 additions and 23 deletions

View file

@ -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");
}
},
);
}
}
}

View file

@ -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

View file

@ -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