Consolidate two arms doing the same thing

This commit is contained in:
Michael Goulet 2024-04-11 12:34:53 -04:00
parent addd931fdb
commit ffdf277618

View File

@ -1829,39 +1829,36 @@ fn report_return_mismatched_types<'a>(
// Verify that this is a tail expression of a function, otherwise the // Verify that this is a tail expression of a function, otherwise the
// label pointing out the cause for the type coercion will be wrong // label pointing out the cause for the type coercion will be wrong
// as prior return coercions would not be relevant (#57664). // as prior return coercions would not be relevant (#57664).
let fn_decl = if let (Some(expr), Some(blk_id)) = (expression, blk_id) { if let Some(expr) = expression
&& let Some(blk_id) = blk_id
{
fcx.suggest_missing_semicolon(&mut err, expr, expected, false); fcx.suggest_missing_semicolon(&mut err, expr, expected, false);
let pointing_at_return_type = let pointing_at_return_type =
fcx.suggest_mismatched_types_on_tail(&mut err, expr, expected, found, blk_id); fcx.suggest_mismatched_types_on_tail(&mut err, expr, expected, found, blk_id);
if let (Some(cond_expr), true, false) = ( if let Some(cond_expr) = fcx.tcx.hir().get_if_cause(expr.hir_id)
fcx.tcx.hir().get_if_cause(expr.hir_id), && expected.is_unit()
expected.is_unit(), && !pointing_at_return_type
pointing_at_return_type,
)
// If the block is from an external macro or try (`?`) desugaring, then // If the block is from an external macro or try (`?`) desugaring, then
// do not suggest adding a semicolon, because there's nowhere to put it. // do not suggest adding a semicolon, because there's nowhere to put it.
// See issues #81943 and #87051. // See issues #81943 and #87051.
&& matches!( && matches!(
cond_expr.span.desugaring_kind(), cond_expr.span.desugaring_kind(),
None | Some(DesugaringKind::WhileLoop) None | Some(DesugaringKind::WhileLoop)
) && !in_external_macro(fcx.tcx.sess, cond_expr.span) )
&& !matches!( && !in_external_macro(fcx.tcx.sess, cond_expr.span)
cond_expr.kind, && !matches!(
hir::ExprKind::Match(.., hir::MatchSource::TryDesugar(_)) cond_expr.kind,
) hir::ExprKind::Match(.., hir::MatchSource::TryDesugar(_))
)
{ {
err.span_label(cond_expr.span, "expected this to be `()`"); err.span_label(cond_expr.span, "expected this to be `()`");
if expr.can_have_side_effects() { if expr.can_have_side_effects() {
fcx.suggest_semicolon_at_end(cond_expr.span, &mut err); fcx.suggest_semicolon_at_end(cond_expr.span, &mut err);
} }
} }
fcx.get_node_fn_decl(parent)
.map(|(fn_id, fn_decl, _, is_main)| (fn_id, fn_decl, is_main))
} else {
fcx.get_fn_decl(parent_id)
}; };
if let Some((fn_id, fn_decl, can_suggest)) = fn_decl { if let Some((fn_id, fn_decl, can_suggest)) = fcx.get_fn_decl(parent_id) {
if blk_id.is_none() { if blk_id.is_none() {
fcx.suggest_missing_return_type( fcx.suggest_missing_return_type(
&mut err, &mut err,