diff options
| author | Michael Wright <mikerite@lavabit.com> | 2020-08-23 07:50:59 +0200 |
|---|---|---|
| committer | Michael Wright <mikerite@lavabit.com> | 2020-08-23 07:50:59 +0200 |
| commit | e8d33d73dc3f9d0daf9b3affe65a2431f5a3e13a (patch) | |
| tree | b0bc083d86082b8bf05ee732574c15839736e443 | |
| parent | dd07860b839a46782e3750a8924ecf380486c460 (diff) | |
| download | rust-e8d33d73dc3f9d0daf9b3affe65a2431f5a3e13a.tar.gz rust-e8d33d73dc3f9d0daf9b3affe65a2431f5a3e13a.zip | |
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
| -rw-r--r-- | clippy_lints/src/returns.rs | 5 | ||||
| -rw-r--r-- | tests/ui/let_and_return.rs | 21 | ||||
| -rw-r--r-- | tests/ui/let_and_return.stderr | 16 |
3 files changed, 40 insertions, 2 deletions
diff --git a/clippy_lints/src/returns.rs b/clippy_lints/src/returns.rs index 3c5541e64b4..a6e4252a0c8 100644 --- a/clippy_lints/src/returns.rs +++ b/clippy_lints/src/returns.rs @@ -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