Skip to content

Commit 97c50d5

Browse files
authored
Rollup merge of #83952 - estebank:issue-83943, r=petrochenkov
Account for `ExprKind::Block` when suggesting .into() and deref Fix #83943.
2 parents a113240 + e1efa17 commit 97c50d5

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

Diff for: compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs

+1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
205205
found: Ty<'tcx>,
206206
expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
207207
) {
208+
let expr = expr.peel_blocks();
208209
if let Some((sp, msg, suggestion, applicability)) = self.check_ref(expr, found, expected) {
209210
err.span_suggestion(sp, msg, suggestion, applicability);
210211
} else if let (ty::FnDef(def_id, ..), true) =

Diff for: src/test/ui/suggestions/issue-82361.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ LL | | 1
2121
| | - expected because of this
2222
LL | | } else {
2323
LL | | &1
24-
| | -^
24+
| | ^^
2525
| | |
2626
| | expected integer, found `&{integer}`
27-
| | help: consider removing the `&`
27+
| | help: consider removing the borrow: `1`
2828
LL | | };
2929
| |_____- `if` and `else` have incompatible types
3030

@@ -36,10 +36,10 @@ LL | | 1
3636
| | - expected because of this
3737
LL | | } else {
3838
LL | | &mut 1
39-
| | -----^
39+
| | ^^^^^^
4040
| | |
4141
| | expected integer, found `&mut {integer}`
42-
| | help: consider removing the `&mut`
42+
| | help: consider removing the borrow: `1`
4343
LL | | };
4444
| |_____- `if` and `else` have incompatible types
4545

Diff for: src/test/ui/suggestions/issue-83943.fixed

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// run-rustfix
2+
3+
fn main() {
4+
if true {
5+
"A".to_string()
6+
} else {
7+
"B".to_string() //~ ERROR `if` and `else` have incompatible types
8+
};
9+
}

Diff for: src/test/ui/suggestions/issue-83943.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// run-rustfix
2+
3+
fn main() {
4+
if true {
5+
"A".to_string()
6+
} else {
7+
"B" //~ ERROR `if` and `else` have incompatible types
8+
};
9+
}

Diff for: src/test/ui/suggestions/issue-83943.stderr

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0308]: `if` and `else` have incompatible types
2+
--> $DIR/issue-83943.rs:7:9
3+
|
4+
LL | / if true {
5+
LL | | "A".to_string()
6+
| | --------------- expected because of this
7+
LL | | } else {
8+
LL | | "B"
9+
| | ^^^
10+
| | |
11+
| | expected struct `String`, found `&str`
12+
| | help: try using a conversion method: `"B".to_string()`
13+
LL | | };
14+
| |_____- `if` and `else` have incompatible types
15+
16+
error: aborting due to previous error
17+
18+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)