Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tweak let_and_return diagnostic #4137

Merged
merged 1 commit into from
May 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions clippy_lints/src/returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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");
}
},
);
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions tests/ui/author/matches.stderr
Original file line number Diff line number Diff line change
@@ -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

23 changes: 13 additions & 10 deletions tests/ui/let_return.stderr
Original file line number Diff line number Diff line change
@@ -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 |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heh, I guess we should teach rustc not to print whitespace only or empty lines

LL | 5
|
LL | let x = 5;
| ^

error: aborting due to 2 previous errors