Skip to content

Commit e8d33d7

Browse files
author
Michael Wright
committed
Fix let_and_return bad suggestion
Add a cast to the suggestion when the return expression has adjustments. These adjustments are lost when the suggestion is applied. This is similar to the problem in issue #4437. Closes #5729
1 parent dd07860 commit e8d33d7

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

clippy_lints/src/returns.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ impl<'tcx> LateLintPass<'tcx> for Return {
9999
|err| {
100100
err.span_label(local.span, "unnecessary `let` binding");
101101

102-
if let Some(snippet) = snippet_opt(cx, initexpr.span) {
102+
if let Some(mut snippet) = snippet_opt(cx, initexpr.span) {
103+
if !cx.typeck_results().expr_adjustments(&retexpr).is_empty() {
104+
snippet.push_str(" as _");
105+
}
103106
err.multipart_suggestion(
104107
"return the expression directly",
105108
vec![

tests/ui/let_and_return.rs

+21
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,25 @@ mod no_lint_if_stmt_borrows {
135135
}
136136
}
137137

138+
mod issue_5729 {
139+
use std::sync::Arc;
140+
141+
trait Foo {}
142+
143+
trait FooStorage {
144+
fn foo_cloned(&self) -> Arc<dyn Foo>;
145+
}
146+
147+
struct FooStorageImpl<T: Foo> {
148+
foo: Arc<T>,
149+
}
150+
151+
impl<T: Foo + 'static> FooStorage for FooStorageImpl<T> {
152+
fn foo_cloned(&self) -> Arc<dyn Foo> {
153+
let clone = Arc::clone(&self.foo);
154+
clone
155+
}
156+
}
157+
}
158+
138159
fn main() {}

tests/ui/let_and_return.stderr

+15-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,19 @@ LL |
2727
LL | 5
2828
|
2929

30-
error: aborting due to 2 previous errors
30+
error: returning the result of a `let` binding from a block
31+
--> $DIR/let_and_return.rs:154:13
32+
|
33+
LL | let clone = Arc::clone(&self.foo);
34+
| ---------------------------------- unnecessary `let` binding
35+
LL | clone
36+
| ^^^^^
37+
|
38+
help: return the expression directly
39+
|
40+
LL |
41+
LL | Arc::clone(&self.foo) as _
42+
|
43+
44+
error: aborting due to 3 previous errors
3145

0 commit comments

Comments
 (0)