Rollup merge of #83489 - LeSeulArtichaut:deref-else, r=davidtwco
Properly suggest deref in else block Continues #79755, fixes #79736 r? `@davidtwco`
This commit is contained in:
commit
d9f123a5ae
3 changed files with 92 additions and 1 deletions
|
@ -366,6 +366,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
false
|
||||
}
|
||||
|
||||
/// 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,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns whether the given expression is an `else if`.
|
||||
crate fn is_else_if_block(&self, expr: &hir::Expr<'_>) -> bool {
|
||||
if let hir::ExprKind::If(..) = expr.kind {
|
||||
let parent_id = self.tcx.hir().get_parent_node(expr.hir_id);
|
||||
if let Some(Node::Expr(hir::Expr {
|
||||
kind: hir::ExprKind::If(_, _, Some(else_expr)),
|
||||
..
|
||||
})) = self.tcx.hir().find(parent_id)
|
||||
{
|
||||
return else_expr.hir_id == expr.hir_id;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// This function is used to determine potential "simple" improvements or users' errors and
|
||||
/// provide them useful help. For example:
|
||||
///
|
||||
|
@ -652,6 +675,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
};
|
||||
let suggestion = if is_struct_pat_shorthand_field {
|
||||
format!("{}: *{}", code, code)
|
||||
} else if self.is_else_if_block(expr) {
|
||||
// Don't suggest nonsense like `else *if`
|
||||
return None;
|
||||
} else if let Some(expr) = self.maybe_get_block_expr(expr.hir_id) {
|
||||
format!("*{}", sm.span_to_snippet(expr.span).unwrap_or(code))
|
||||
} else {
|
||||
format!("*{}", code)
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue