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
This commit is contained in:
Michael Wright 2020-08-23 07:50:59 +02:00
parent dd07860b83
commit e8d33d73dc
3 changed files with 40 additions and 2 deletions

View File

@ -99,7 +99,10 @@ fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'_>) {
|err| {
err.span_label(local.span, "unnecessary `let` binding");
if let Some(snippet) = snippet_opt(cx, initexpr.span) {
if let Some(mut snippet) = snippet_opt(cx, initexpr.span) {
if !cx.typeck_results().expr_adjustments(&retexpr).is_empty() {
snippet.push_str(" as _");
}
err.multipart_suggestion(
"return the expression directly",
vec![

View File

@ -135,4 +135,25 @@ fn test() -> i32 {
}
}
mod issue_5729 {
use std::sync::Arc;
trait Foo {}
trait FooStorage {
fn foo_cloned(&self) -> Arc<dyn Foo>;
}
struct FooStorageImpl<T: Foo> {
foo: Arc<T>,
}
impl<T: Foo + 'static> FooStorage for FooStorageImpl<T> {
fn foo_cloned(&self) -> Arc<dyn Foo> {
let clone = Arc::clone(&self.foo);
clone
}
}
}
fn main() {}

View File

@ -27,5 +27,19 @@ LL |
LL | 5
|
error: aborting due to 2 previous errors
error: returning the result of a `let` binding from a block
--> $DIR/let_and_return.rs:154:13
|
LL | let clone = Arc::clone(&self.foo);
| ---------------------------------- unnecessary `let` binding
LL | clone
| ^^^^^
|
help: return the expression directly
|
LL |
LL | Arc::clone(&self.foo) as _
|
error: aborting due to 3 previous errors