Skip to content

Commit 7b501f0

Browse files
committed
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
2 parents eb0a288 + effea41 commit 7b501f0

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

clippy_lints/src/returns.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use syntax::source_map::Span;
77
use syntax::visit::FnKind;
88
use syntax_pos::BytePos;
99

10-
use crate::utils::{in_macro_or_desugar, match_path_ast, snippet_opt, span_lint_and_then, span_note_and_lint};
10+
use crate::utils::{in_macro_or_desugar, match_path_ast, snippet_opt, span_lint_and_then};
1111

1212
declare_clippy_lint! {
1313
/// **What it does:** Checks for return statements at the end of a block.
@@ -164,13 +164,28 @@ impl Return {
164164
if match_path_ast(path, &[&*ident.name.as_str()]);
165165
if !in_external_macro(cx.sess(), initexpr.span);
166166
then {
167-
span_note_and_lint(cx,
168-
LET_AND_RETURN,
169-
retexpr.span,
170-
"returning the result of a let binding from a block. \
171-
Consider returning the expression directly.",
172-
initexpr.span,
173-
"this expression can be directly returned");
167+
span_lint_and_then(
168+
cx,
169+
LET_AND_RETURN,
170+
retexpr.span,
171+
"returning the result of a let binding from a block",
172+
|err| {
173+
err.span_label(local.span, "unnecessary let binding");
174+
175+
if let Some(snippet) = snippet_opt(cx, initexpr.span) {
176+
err.multipart_suggestion(
177+
"return the expression directly",
178+
vec![
179+
(local.span, String::new()),
180+
(retexpr.span, snippet),
181+
],
182+
Applicability::MachineApplicable,
183+
);
184+
} else {
185+
err.span_help(initexpr.span, "this expression can be directly returned");
186+
}
187+
},
188+
);
174189
}
175190
}
176191
}

tests/ui/author/matches.stderr

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
error: returning the result of a let binding from a block. Consider returning the expression directly.
1+
error: returning the result of a let binding from a block
22
--> $DIR/matches.rs:9:13
33
|
4+
LL | let x = 3;
5+
| ---------- unnecessary let binding
46
LL | x
57
| ^
68
|
79
= note: `-D clippy::let-and-return` implied by `-D warnings`
8-
note: this expression can be directly returned
9-
--> $DIR/matches.rs:8:21
10+
help: return the expression directly
11+
|
12+
LL |
13+
LL | 3
1014
|
11-
LL | let x = 3;
12-
| ^
1315

1416
error: aborting due to previous error
1517

tests/ui/let_return.stderr

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
error: returning the result of a let binding from a block. Consider returning the expression directly.
1+
error: returning the result of a let binding from a block
22
--> $DIR/let_return.rs:7:5
33
|
4+
LL | let x = 5;
5+
| ---------- unnecessary let binding
46
LL | x
57
| ^
68
|
79
= note: `-D clippy::let-and-return` implied by `-D warnings`
8-
note: this expression can be directly returned
9-
--> $DIR/let_return.rs:6:13
10+
help: return the expression directly
11+
|
12+
LL |
13+
LL | 5
1014
|
11-
LL | let x = 5;
12-
| ^
1315

14-
error: returning the result of a let binding from a block. Consider returning the expression directly.
16+
error: returning the result of a let binding from a block
1517
--> $DIR/let_return.rs:13:9
1618
|
19+
LL | let x = 5;
20+
| ---------- unnecessary let binding
1721
LL | x
1822
| ^
23+
help: return the expression directly
1924
|
20-
note: this expression can be directly returned
21-
--> $DIR/let_return.rs:12:17
25+
LL |
26+
LL | 5
2227
|
23-
LL | let x = 5;
24-
| ^
2528

2629
error: aborting due to 2 previous errors
2730

0 commit comments

Comments
 (0)