Skip to content

Commit d84c4cd

Browse files
committed
Ignore ExprKind::DropTemps for some ref suggestions
1 parent ec557aa commit d84c4cd

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

src/librustc/hir/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,23 @@ impl Expr {
15481548
}
15491549
}
15501550
}
1551+
1552+
/// If `Self.kind` is `ExprKind::DropTemps(expr)`, drill down until we get a non-`DropTemps`
1553+
/// `Expr`. This is used in suggestions to ignore this `ExprKind` as it is semantically
1554+
/// silent, only signaling the ownership system. By doing this, suggestions that check the
1555+
/// `ExprKind` of any given `Expr` for presentation don't have to care about `DropTemps`
1556+
/// beyond remembering to call this function before doing analysis on it.
1557+
pub fn peel_drop_temps(&self) -> &Self {
1558+
let mut base_expr = self;
1559+
loop {
1560+
match &base_expr.kind {
1561+
ExprKind::DropTemps(expr) => {
1562+
base_expr = &expr;
1563+
}
1564+
_ => return base_expr,
1565+
}
1566+
}
1567+
}
15511568
}
15521569

15531570
impl fmt::Debug for Expr {

src/librustc_typeck/check/demand.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
355355
};
356356
let is_macro = sp.from_expansion() && !is_desugaring;
357357

358+
// `ExprKind::DropTemps` is semantically irrelevant for these suggestions.
359+
let expr = expr.peel_drop_temps();
360+
358361
match (&expr.kind, &expected.kind, &checked_ty.kind) {
359362
(_, &ty::Ref(_, exp, _), &ty::Ref(_, check, _)) => match (&exp.kind, &check.kind) {
360363
(&ty::Str, &ty::Array(arr, _)) |

src/test/ui/if/if-no-match-bindings.stderr

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/if-no-match-bindings.rs:18:8
33
|
44
LL | if b_ref() {}
5-
| ^^^^^^^
6-
| |
7-
| expected bool, found &bool
8-
| help: consider dereferencing the borrow: `*b_ref()`
5+
| ^^^^^^^ expected bool, found &bool
96
|
107
= note: expected type `bool`
118
found type `&bool`
@@ -14,10 +11,7 @@ error[E0308]: mismatched types
1411
--> $DIR/if-no-match-bindings.rs:19:8
1512
|
1613
LL | if b_mut_ref() {}
17-
| ^^^^^^^^^^^
18-
| |
19-
| expected bool, found &mut bool
20-
| help: consider dereferencing the borrow: `*b_mut_ref()`
14+
| ^^^^^^^^^^^ expected bool, found &mut bool
2115
|
2216
= note: expected type `bool`
2317
found type `&mut bool`
@@ -29,7 +23,7 @@ LL | if &true {}
2923
| ^^^^^
3024
| |
3125
| expected bool, found &bool
32-
| help: consider dereferencing the borrow: `*&true`
26+
| help: consider removing the borrow: `true`
3327
|
3428
= note: expected type `bool`
3529
found type `&bool`
@@ -41,7 +35,7 @@ LL | if &mut true {}
4135
| ^^^^^^^^^
4236
| |
4337
| expected bool, found &mut bool
44-
| help: consider dereferencing the borrow: `*&mut true`
38+
| help: consider removing the borrow: `true`
4539
|
4640
= note: expected type `bool`
4741
found type `&mut bool`
@@ -50,10 +44,7 @@ error[E0308]: mismatched types
5044
--> $DIR/if-no-match-bindings.rs:24:11
5145
|
5246
LL | while b_ref() {}
53-
| ^^^^^^^
54-
| |
55-
| expected bool, found &bool
56-
| help: consider dereferencing the borrow: `*b_ref()`
47+
| ^^^^^^^ expected bool, found &bool
5748
|
5849
= note: expected type `bool`
5950
found type `&bool`
@@ -62,10 +53,7 @@ error[E0308]: mismatched types
6253
--> $DIR/if-no-match-bindings.rs:25:11
6354
|
6455
LL | while b_mut_ref() {}
65-
| ^^^^^^^^^^^
66-
| |
67-
| expected bool, found &mut bool
68-
| help: consider dereferencing the borrow: `*b_mut_ref()`
56+
| ^^^^^^^^^^^ expected bool, found &mut bool
6957
|
7058
= note: expected type `bool`
7159
found type `&mut bool`
@@ -77,7 +65,7 @@ LL | while &true {}
7765
| ^^^^^
7866
| |
7967
| expected bool, found &bool
80-
| help: consider dereferencing the borrow: `*&true`
68+
| help: consider removing the borrow: `true`
8169
|
8270
= note: expected type `bool`
8371
found type `&bool`
@@ -89,7 +77,7 @@ LL | while &mut true {}
8977
| ^^^^^^^^^
9078
| |
9179
| expected bool, found &mut bool
92-
| help: consider dereferencing the borrow: `*&mut true`
80+
| help: consider removing the borrow: `true`
9381
|
9482
= note: expected type `bool`
9583
found type `&mut bool`

src/test/ui/rfc-2497-if-let-chains/disallowed-positions.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ LL | if &let 0 = 0 {}
520520
| ^^^^^^^^^^
521521
| |
522522
| expected bool, found &bool
523-
| help: consider dereferencing the borrow: `*&let 0 = 0`
523+
| help: consider removing the borrow: `let 0 = 0`
524524
|
525525
= note: expected type `bool`
526526
found type `&bool`
@@ -708,7 +708,7 @@ LL | while &let 0 = 0 {}
708708
| ^^^^^^^^^^
709709
| |
710710
| expected bool, found &bool
711-
| help: consider dereferencing the borrow: `*&let 0 = 0`
711+
| help: consider removing the borrow: `let 0 = 0`
712712
|
713713
= note: expected type `bool`
714714
found type `&bool`

0 commit comments

Comments
 (0)