Skip to content

Commit e603f99

Browse files
Address review comments
Co-authored-by: Léo Lanteri Thauvin <[email protected]>
1 parent 98ad0af commit e603f99

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

compiler/rustc_typeck/src/check/demand.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -366,16 +366,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
366366
false
367367
}
368368

369-
crate fn hir_id_sole_block_element(
370-
&self,
371-
hir_id: hir::HirId,
372-
) -> Option<&'tcx rustc_hir::Expr<'tcx>> {
373-
let node: Option<Node<'_>> = self.tcx.hir().find(hir_id);
374-
match node {
375-
Some(Node::Expr(rustc_hir::Expr {
376-
kind: rustc_hir::ExprKind::Block(block, ..),
377-
..
378-
})) if block.stmts.len() == 0 => block.expr,
369+
/// If the given `HirId` corresponds to a block with a trailing expression, return that expression
370+
crate fn maybe_get_block_expr(&self, hir_id: hir::HirId) -> Option<&'tcx hir::Expr<'tcx>> {
371+
match self.tcx.hir().find(hir_id)? {
372+
Node::Expr(hir::Expr { kind: hir::ExprKind::Block(block, ..), .. }) => block.expr,
379373
_ => None,
380374
}
381375
}
@@ -666,9 +660,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
666660
};
667661
let suggestion = if is_struct_pat_shorthand_field {
668662
format!("{}: *{}", code, code)
669-
} else if let Some(expr) =
670-
self.hir_id_sole_block_element(expr.hir_id)
671-
{
663+
} else if let Some(expr) = self.maybe_get_block_expr(expr.hir_id) {
672664
if let Ok(inner_code) = sm.span_to_snippet(expr.span) {
673665
format!("*{}", inner_code)
674666
} else {

src/test/ui/deref-suggestion.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,12 @@ fn main() {
5555
b
5656
//~^ ERROR mismatched types
5757
};
58+
let val: i32 = if true {
59+
let _ = 2;
60+
a + 1
61+
} else {
62+
let _ = 2;
63+
b
64+
//~^ ERROR mismatched types
65+
};
5866
}

src/test/ui/deref-suggestion.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ LL | b
9898
| expected `i32`, found `&{integer}`
9999
| help: consider dereferencing the borrow: `*b`
100100

101-
error: aborting due to 11 previous errors
101+
error[E0308]: mismatched types
102+
--> $DIR/deref-suggestion.rs:63:9
103+
|
104+
LL | b
105+
| ^
106+
| |
107+
| expected `i32`, found `&{integer}`
108+
| help: consider dereferencing the borrow: `*b`
109+
110+
error: aborting due to 12 previous errors
102111

103112
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)