Skip to content

Commit

Permalink
Auto merge of #5946 - mikerite:fix-5729, r=flip1995
Browse files Browse the repository at this point in the history
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

changelog: Fix `let_and_return` bad suggestion
  • Loading branch information
bors committed Aug 26, 2020
2 parents 9ef4479 + e8d33d7 commit 894581b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
5 changes: 4 additions & 1 deletion clippy_lints/src/returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ impl<'tcx> LateLintPass<'tcx> for Return {
|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![
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/let_and_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,25 @@ mod no_lint_if_stmt_borrows {
}
}

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() {}
16 changes: 15 additions & 1 deletion tests/ui/let_and_return.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 894581b

Please sign in to comment.