Skip to content

Commit 1d8e053

Browse files
committed
move new error logic to its own function
1 parent 0c4c648 commit 1d8e053

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

compiler/rustc_hir_typeck/src/_match.rs

+33-22
Original file line numberDiff line numberDiff line change
@@ -140,28 +140,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
140140
Some(arm.body),
141141
arm_ty,
142142
|err| {
143-
if let hir::ExprKind::Block(block, _) = arm.body.kind
144-
&& let Some(expr) = block.expr
145-
&& let arm_tail_ty = self.node_ty(expr.hir_id)
146-
&& arm_tail_ty.is_never()
147-
&& !arm_ty.is_never()
148-
{
149-
err.span_label(
150-
expr.span,
151-
format!(
152-
"this expression is of type `!`, but it is coerced to \
153-
`{arm_ty}` due to its surrounding expression",
154-
),
155-
);
156-
self.suggest_mismatched_types_on_tail(
157-
err,
158-
expr,
159-
arm_ty,
160-
prior_arm.map_or(arm_tail_ty, |(_, ty, _)| ty),
161-
expr.hir_id,
162-
);
163-
}
164-
self.suggest_removing_semicolon_for_coerce(err, expr, arm_ty, prior_arm)
143+
self.explain_never_type_coerced_to_unit(err, arm, arm_ty, prior_arm, expr);
165144
},
166145
false,
167146
);
@@ -200,6 +179,38 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
200179
coercion.complete(self)
201180
}
202181

182+
fn explain_never_type_coerced_to_unit(
183+
&self,
184+
err: &mut Diagnostic,
185+
arm: &hir::Arm<'tcx>,
186+
arm_ty: Ty<'tcx>,
187+
prior_arm: Option<(Option<hir::HirId>, Ty<'tcx>, Span)>,
188+
expr: &hir::Expr<'tcx>,
189+
) {
190+
if let hir::ExprKind::Block(block, _) = arm.body.kind
191+
&& let Some(expr) = block.expr
192+
&& let arm_tail_ty = self.node_ty(expr.hir_id)
193+
&& arm_tail_ty.is_never()
194+
&& !arm_ty.is_never()
195+
{
196+
err.span_label(
197+
expr.span,
198+
format!(
199+
"this expression is of type `!`, but it is coerced to `{arm_ty}` due to its \
200+
surrounding expression",
201+
),
202+
);
203+
self.suggest_mismatched_types_on_tail(
204+
err,
205+
expr,
206+
arm_ty,
207+
prior_arm.map_or(arm_tail_ty, |(_, ty, _)| ty),
208+
expr.hir_id,
209+
);
210+
}
211+
self.suggest_removing_semicolon_for_coerce(err, expr, arm_ty, prior_arm)
212+
}
213+
203214
fn suggest_removing_semicolon_for_coerce(
204215
&self,
205216
diag: &mut Diagnostic,

0 commit comments

Comments
 (0)