1
Fork 0

Address review comments

Co-authored-by: Léo Lanteri Thauvin <leseulartichaut@gmail.com>
This commit is contained in:
mibac138 2020-12-17 13:24:51 +01:00 committed by LeSeulArtichaut
commit e603f994b1
3 changed files with 23 additions and 14 deletions

View file

@ -366,16 +366,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
false
}
crate fn hir_id_sole_block_element(
&self,
hir_id: hir::HirId,
) -> Option<&'tcx rustc_hir::Expr<'tcx>> {
let node: Option<Node<'_>> = self.tcx.hir().find(hir_id);
match node {
Some(Node::Expr(rustc_hir::Expr {
kind: rustc_hir::ExprKind::Block(block, ..),
..
})) if block.stmts.len() == 0 => block.expr,
/// If the given `HirId` corresponds to a block with a trailing expression, return that expression
crate fn maybe_get_block_expr(&self, hir_id: hir::HirId) -> Option<&'tcx hir::Expr<'tcx>> {
match self.tcx.hir().find(hir_id)? {
Node::Expr(hir::Expr { kind: hir::ExprKind::Block(block, ..), .. }) => block.expr,
_ => None,
}
}
@ -666,9 +660,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
};
let suggestion = if is_struct_pat_shorthand_field {
format!("{}: *{}", code, code)
} else if let Some(expr) =
self.hir_id_sole_block_element(expr.hir_id)
{
} else if let Some(expr) = self.maybe_get_block_expr(expr.hir_id) {
if let Ok(inner_code) = sm.span_to_snippet(expr.span) {
format!("*{}", inner_code)
} else {

View file

@ -55,4 +55,12 @@ fn main() {
b
//~^ ERROR mismatched types
};
let val: i32 = if true {
let _ = 2;
a + 1
} else {
let _ = 2;
b
//~^ ERROR mismatched types
};
}

View file

@ -98,6 +98,15 @@ LL | b
| expected `i32`, found `&{integer}`
| help: consider dereferencing the borrow: `*b`
error: aborting due to 11 previous errors
error[E0308]: mismatched types
--> $DIR/deref-suggestion.rs:63:9
|
LL | b
| ^
| |
| expected `i32`, found `&{integer}`
| help: consider dereferencing the borrow: `*b`
error: aborting due to 12 previous errors
For more information about this error, try `rustc --explain E0308`.