diff options
| author | Dylan DPC <dylan.dpc@gmail.com> | 2021-04-08 01:01:45 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-08 01:01:45 +0200 |
| commit | 97c50d529b4e98840a71e89e979a3743f218b6cf (patch) | |
| tree | 875644fedf74a93826293fc1109aeddcdc30dde9 | |
| parent | a113240b91c7ad1955c98888a55e3eee9346122e (diff) | |
| parent | e1efa17faa234e3f69596001c1256c3fc10bedc8 (diff) | |
| download | rust-97c50d529b4e98840a71e89e979a3743f218b6cf.tar.gz rust-97c50d529b4e98840a71e89e979a3743f218b6cf.zip | |
Rollup merge of #83952 - estebank:issue-83943, r=petrochenkov
Account for `ExprKind::Block` when suggesting .into() and deref Fix #83943.
| -rw-r--r-- | compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs | 1 | ||||
| -rw-r--r-- | src/test/ui/suggestions/issue-82361.stderr | 8 | ||||
| -rw-r--r-- | src/test/ui/suggestions/issue-83943.fixed | 9 | ||||
| -rw-r--r-- | src/test/ui/suggestions/issue-83943.rs | 9 | ||||
| -rw-r--r-- | src/test/ui/suggestions/issue-83943.stderr | 18 |
5 files changed, 41 insertions, 4 deletions
diff --git a/compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs b/compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs index 6112a5fdc91..c87a808243d 100644 --- a/compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs +++ b/compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs @@ -205,6 +205,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { found: Ty<'tcx>, expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>, ) { + let expr = expr.peel_blocks(); if let Some((sp, msg, suggestion, applicability)) = self.check_ref(expr, found, expected) { err.span_suggestion(sp, msg, suggestion, applicability); } else if let (ty::FnDef(def_id, ..), true) = diff --git a/src/test/ui/suggestions/issue-82361.stderr b/src/test/ui/suggestions/issue-82361.stderr index c19d59ccd4c..4c78293ebb7 100644 --- a/src/test/ui/suggestions/issue-82361.stderr +++ b/src/test/ui/suggestions/issue-82361.stderr @@ -21,10 +21,10 @@ LL | | 1 | | - expected because of this LL | | } else { LL | | &1 - | | -^ + | | ^^ | | | | | expected integer, found `&{integer}` - | | help: consider removing the `&` + | | help: consider removing the borrow: `1` LL | | }; | |_____- `if` and `else` have incompatible types @@ -36,10 +36,10 @@ LL | | 1 | | - expected because of this LL | | } else { LL | | &mut 1 - | | -----^ + | | ^^^^^^ | | | | | expected integer, found `&mut {integer}` - | | help: consider removing the `&mut` + | | help: consider removing the borrow: `1` LL | | }; | |_____- `if` and `else` have incompatible types diff --git a/src/test/ui/suggestions/issue-83943.fixed b/src/test/ui/suggestions/issue-83943.fixed new file mode 100644 index 00000000000..e0d4ee29ebf --- /dev/null +++ b/src/test/ui/suggestions/issue-83943.fixed @@ -0,0 +1,9 @@ +// run-rustfix + +fn main() { + if true { + "A".to_string() + } else { + "B".to_string() //~ ERROR `if` and `else` have incompatible types + }; +} diff --git a/src/test/ui/suggestions/issue-83943.rs b/src/test/ui/suggestions/issue-83943.rs new file mode 100644 index 00000000000..68d50c1775c --- /dev/null +++ b/src/test/ui/suggestions/issue-83943.rs @@ -0,0 +1,9 @@ +// run-rustfix + +fn main() { + if true { + "A".to_string() + } else { + "B" //~ ERROR `if` and `else` have incompatible types + }; +} diff --git a/src/test/ui/suggestions/issue-83943.stderr b/src/test/ui/suggestions/issue-83943.stderr new file mode 100644 index 00000000000..a26700ea3c7 --- /dev/null +++ b/src/test/ui/suggestions/issue-83943.stderr @@ -0,0 +1,18 @@ +error[E0308]: `if` and `else` have incompatible types + --> $DIR/issue-83943.rs:7:9 + | +LL | / if true { +LL | | "A".to_string() + | | --------------- expected because of this +LL | | } else { +LL | | "B" + | | ^^^ + | | | + | | expected struct `String`, found `&str` + | | help: try using a conversion method: `"B".to_string()` +LL | | }; + | |_____- `if` and `else` have incompatible types + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. |
