Skip to content

Commit bb0a2f9

Browse files
committed
rustc_typeck: adopt let else in more places
1 parent b8c56fa commit bb0a2f9

22 files changed

+135
-229
lines changed

compiler/rustc_typeck/src/astconv/mod.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1808,12 +1808,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
18081808
(_, Res::SelfTy { trait_: Some(_), alias_to: Some((impl_def_id, _)) }) => {
18091809
// `Self` in an impl of a trait -- we have a concrete self type and a
18101810
// trait reference.
1811-
let trait_ref = match tcx.impl_trait_ref(impl_def_id) {
1812-
Some(trait_ref) => trait_ref,
1813-
None => {
1814-
// A cycle error occurred, most likely.
1815-
return Err(ErrorReported);
1816-
}
1811+
let Some(trait_ref) = tcx.impl_trait_ref(impl_def_id) else {
1812+
// A cycle error occurred, most likely.
1813+
return Err(ErrorReported);
18171814
};
18181815

18191816
self.one_bound_for_assoc_type(

compiler/rustc_typeck/src/check/callee.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
219219
(self.tcx.lang_items().fn_mut_trait(), Ident::with_dummy_span(sym::call_mut), true),
220220
(self.tcx.lang_items().fn_once_trait(), Ident::with_dummy_span(sym::call_once), false),
221221
] {
222-
let trait_def_id = match opt_trait_def_id {
223-
Some(def_id) => def_id,
224-
None => continue,
225-
};
222+
let Some(trait_def_id) = opt_trait_def_id else { continue };
226223

227224
let opt_input_types = opt_arg_exprs.map(|arg_exprs| {
228225
[self.tcx.mk_tup(arg_exprs.iter().map(|e| {
@@ -246,11 +243,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
246243
if borrow {
247244
// Check for &self vs &mut self in the method signature. Since this is either
248245
// the Fn or FnMut trait, it should be one of those.
249-
let (region, mutbl) = if let ty::Ref(r, _, mutbl) =
250-
method.sig.inputs()[0].kind()
251-
{
252-
(r, mutbl)
253-
} else {
246+
let ty::Ref(region, _, mutbl) = method.sig.inputs()[0].kind() else {
254247
// The `fn`/`fn_mut` lang item is ill-formed, which should have
255248
// caused an error elsewhere.
256249
self.tcx

compiler/rustc_typeck/src/check/cast.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -799,21 +799,19 @@ impl<'a, 'tcx> CastCheck<'tcx> {
799799
let expr_kind = fcx.pointer_kind(m_expr.ty, self.span)?;
800800
let cast_kind = fcx.pointer_kind(m_cast.ty, self.span)?;
801801

802-
let cast_kind = match cast_kind {
802+
let Some(cast_kind) = cast_kind else {
803803
// We can't cast if target pointer kind is unknown
804-
None => return Err(CastError::UnknownCastPtrKind),
805-
Some(cast_kind) => cast_kind,
804+
return Err(CastError::UnknownCastPtrKind);
806805
};
807806

808807
// Cast to thin pointer is OK
809808
if cast_kind == PointerKind::Thin {
810809
return Ok(CastKind::PtrPtrCast);
811810
}
812811

813-
let expr_kind = match expr_kind {
812+
let Some(expr_kind) = expr_kind else {
814813
// We can't cast to fat pointer if source pointer kind is unknown
815-
None => return Err(CastError::UnknownExprPtrKind),
816-
Some(expr_kind) => expr_kind,
814+
return Err(CastError::UnknownExprPtrKind);
817815
};
818816

819817
// thin -> fat? report invalid cast (don't complain about vtable kinds)

compiler/rustc_typeck/src/check/check.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -415,13 +415,10 @@ fn check_static_inhabited<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, span: Spa
415415
// have UB during initialization if they are uninhabited, but there also seems to be no good
416416
// reason to allow any statics to be uninhabited.
417417
let ty = tcx.type_of(def_id);
418-
let layout = match tcx.layout_of(ParamEnv::reveal_all().and(ty)) {
419-
Ok(l) => l,
420-
Err(_) => {
421-
// Generic statics are rejected, but we still reach this case.
422-
tcx.sess.delay_span_bug(span, "generic static must be rejected");
423-
return;
424-
}
418+
let Ok(layout) = tcx.layout_of(ParamEnv::reveal_all().and(ty)) else {
419+
// Generic statics are rejected, but we still reach this case.
420+
tcx.sess.delay_span_bug(span, "generic static must be rejected");
421+
return;
425422
};
426423
if layout.abi.is_uninhabited() {
427424
tcx.struct_span_lint_hir(
@@ -852,10 +849,7 @@ pub(super) fn check_specialization_validity<'tcx>(
852849
impl_id: DefId,
853850
impl_item: &hir::ImplItemRef,
854851
) {
855-
let ancestors = match trait_def.ancestors(tcx, impl_id) {
856-
Ok(ancestors) => ancestors,
857-
Err(_) => return,
858-
};
852+
let Ok(ancestors) = trait_def.ancestors(tcx, impl_id) else { return };
859853
let mut ancestor_impls = ancestors.skip(1).filter_map(|parent| {
860854
if parent.is_from_trait() {
861855
None

compiler/rustc_typeck/src/check/closure.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -676,12 +676,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
676676

677677
// We do not expect any bound regions in our predicate, so
678678
// skip past the bound vars.
679-
let predicate = match predicate.no_bound_vars() {
680-
Some(p) => p,
681-
None => {
682-
debug!("deduce_future_output_from_projection: has late-bound regions");
683-
return None;
684-
}
679+
let Some(predicate) = predicate.no_bound_vars() else {
680+
debug!("deduce_future_output_from_projection: has late-bound regions");
681+
return None;
685682
};
686683

687684
// Check that this is a projection from the `Future` trait.

compiler/rustc_typeck/src/check/coercion.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -429,13 +429,10 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
429429
// (e.g., in example above, the failure from relating `Vec<T>`
430430
// to the target type), since that should be the least
431431
// confusing.
432-
let InferOk { value: ty, mut obligations } = match found {
433-
Some(d) => d,
434-
None => {
435-
let err = first_error.expect("coerce_borrowed_pointer had no error");
436-
debug!("coerce_borrowed_pointer: failed with err = {:?}", err);
437-
return Err(err);
438-
}
432+
let Some(InferOk { value: ty, mut obligations }) = found else {
433+
let err = first_error.expect("coerce_borrowed_pointer had no error");
434+
debug!("coerce_borrowed_pointer: failed with err = {:?}", err);
435+
return Err(err);
439436
};
440437

441438
if ty == a && mt_a.mutbl == hir::Mutability::Not && autoderef.step_count() == 1 {
@@ -461,9 +458,8 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
461458

462459
// Now apply the autoref. We have to extract the region out of
463460
// the final ref type we got.
464-
let r_borrow = match ty.kind() {
465-
ty::Ref(r_borrow, _, _) => r_borrow,
466-
_ => span_bug!(span, "expected a ref type, got {:?}", ty),
461+
let ty::Ref(r_borrow, _, _) = ty.kind() else {
462+
span_bug!(span, "expected a ref type, got {:?}", ty);
467463
};
468464
let mutbl = match mutbl_b {
469465
hir::Mutability::Not => AutoBorrowMutability::Not,
@@ -944,9 +940,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
944940
// We don't ever need two-phase here since we throw out the result of the coercion
945941
let coerce = Coerce::new(self, cause, AllowTwoPhase::No);
946942
self.probe(|_| {
947-
let ok = match coerce.coerce(source, target) {
948-
Ok(ok) => ok,
949-
_ => return false,
943+
let Ok(ok) = coerce.coerce(source, target) else {
944+
return false;
950945
};
951946
let mut fcx = traits::FulfillmentContext::new_in_snapshot();
952947
fcx.register_predicate_obligations(self, ok.obligations);

compiler/rustc_typeck/src/check/demand.rs

+20-25
Original file line numberDiff line numberDiff line change
@@ -435,44 +435,39 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
435435
/// opt.map(|param| { takes_ref(param) });
436436
/// ```
437437
fn can_use_as_ref(&self, expr: &hir::Expr<'_>) -> Option<(Span, &'static str, String)> {
438-
let path = match expr.kind {
439-
hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) => path,
440-
_ => return None,
438+
let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = expr.kind else {
439+
return None;
441440
};
442441

443-
let local_id = match path.res {
444-
hir::def::Res::Local(id) => id,
445-
_ => return None,
442+
let hir::def::Res::Local(local_id) = path.res else {
443+
return None;
446444
};
447445

448446
let local_parent = self.tcx.hir().get_parent_node(local_id);
449-
let param_hir_id = match self.tcx.hir().find(local_parent) {
450-
Some(Node::Param(hir::Param { hir_id, .. })) => hir_id,
451-
_ => return None,
447+
let Some(Node::Param(hir::Param { hir_id: param_hir_id, .. })) = self.tcx.hir().find(local_parent) else {
448+
return None;
452449
};
453450

454451
let param_parent = self.tcx.hir().get_parent_node(*param_hir_id);
455-
let (expr_hir_id, closure_fn_decl) = match self.tcx.hir().find(param_parent) {
456-
Some(Node::Expr(hir::Expr {
457-
hir_id,
458-
kind: hir::ExprKind::Closure(_, decl, ..),
459-
..
460-
})) => (hir_id, decl),
461-
_ => return None,
452+
let Some(Node::Expr(hir::Expr {
453+
hir_id: expr_hir_id,
454+
kind: hir::ExprKind::Closure(_, closure_fn_decl, ..),
455+
..
456+
})) = self.tcx.hir().find(param_parent) else {
457+
return None;
462458
};
463459

464460
let expr_parent = self.tcx.hir().get_parent_node(*expr_hir_id);
465461
let hir = self.tcx.hir().find(expr_parent);
466462
let closure_params_len = closure_fn_decl.inputs.len();
467-
let (method_path, method_expr) = match (hir, closure_params_len) {
468-
(
469-
Some(Node::Expr(hir::Expr {
470-
kind: hir::ExprKind::MethodCall(segment, expr, _),
471-
..
472-
})),
473-
1,
474-
) => (segment, expr),
475-
_ => return None,
463+
let (
464+
Some(Node::Expr(hir::Expr {
465+
kind: hir::ExprKind::MethodCall(method_path, method_expr, _),
466+
..
467+
})),
468+
1,
469+
) = (hir, closure_params_len) else {
470+
return None;
476471
};
477472

478473
let self_ty = self.typeck_results.borrow().node_type(method_expr[0].hir_id);

compiler/rustc_typeck/src/check/expr.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -622,15 +622,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
622622
// the `enclosing_loops` field and let's coerce the
623623
// type of `expr_opt` into what is expected.
624624
let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
625-
let ctxt = match enclosing_breakables.opt_find_breakable(target_id) {
626-
Some(ctxt) => ctxt,
627-
None => {
628-
// Avoid ICE when `break` is inside a closure (#65383).
629-
return tcx.ty_error_with_message(
630-
expr.span,
631-
"break was outside loop, but no error was emitted",
632-
);
633-
}
625+
let Some(ctxt) = enclosing_breakables.opt_find_breakable(target_id) else {
626+
// Avoid ICE when `break` is inside a closure (#65383).
627+
return tcx.ty_error_with_message(
628+
expr.span,
629+
"break was outside loop, but no error was emitted",
630+
);
634631
};
635632

636633
if let Some(ref mut coerce) = ctxt.coerce {

compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -745,10 +745,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
745745
formal_args: &[Ty<'tcx>],
746746
) -> Vec<Ty<'tcx>> {
747747
let formal_ret = self.resolve_vars_with_obligations(formal_ret);
748-
let ret_ty = match expected_ret.only_has_type(self) {
749-
Some(ret) => ret,
750-
None => return Vec::new(),
751-
};
748+
let Some(ret_ty) = expected_ret.only_has_type(self) else { return Vec::new() };
752749
let expect_args = self
753750
.fudge_inference_if_ok(|| {
754751
// Attempt to apply a subtyping relationship between the formal
@@ -1044,9 +1041,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10441041
// Be helpful when the user wrote `{... expr;}` and
10451042
// taking the `;` off is enough to fix the error.
10461043
let last_stmt = blk.stmts.last()?;
1047-
let last_expr = match last_stmt.kind {
1048-
hir::StmtKind::Semi(ref e) => e,
1049-
_ => return None,
1044+
let hir::StmtKind::Semi(ref last_expr) = last_stmt.kind else {
1045+
return None;
10501046
};
10511047
let last_expr_ty = self.node_ty(last_expr.hir_id);
10521048
let needs_box = match (last_expr_ty.kind(), expected_ty.kind()) {
@@ -1061,11 +1057,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10611057
last_def_id, last_bounds, exp_def_id, exp_bounds
10621058
);
10631059

1064-
let (last_local_id, exp_local_id) =
1065-
match (last_def_id.as_local(), exp_def_id.as_local()) {
1066-
(Some(last_hir_id), Some(exp_hir_id)) => (last_hir_id, exp_hir_id),
1067-
(_, _) => return None,
1068-
};
1060+
let last_local_id = last_def_id.as_local()?;
1061+
let exp_local_id = exp_def_id.as_local()?;
10691062

10701063
match (
10711064
&self.tcx.hir().expect_item(last_local_id).kind,

compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
434434
// is and we were expecting a Box, ergo Pin<Box<expected>>, we
435435
// can suggest Box::pin.
436436
let parent = self.tcx.hir().get_parent_node(expr.hir_id);
437-
let fn_name = match self.tcx.hir().find(parent) {
438-
Some(Node::Expr(Expr { kind: ExprKind::Call(fn_name, _), .. })) => fn_name,
439-
_ => return false,
437+
let Some(Node::Expr(Expr { kind: ExprKind::Call(fn_name, _), .. })) = self.tcx.hir().find(parent) else {
438+
return false;
440439
};
441440
match fn_name.kind {
442441
ExprKind::Path(QPath::TypeRelative(

compiler/rustc_typeck/src/check/method/confirm.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,11 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
149149
// time writing the results into the various typeck results.
150150
let mut autoderef =
151151
self.autoderef_overloaded_span(self.span, unadjusted_self_ty, self.call_expr.span);
152-
let (ty, n) = match autoderef.nth(pick.autoderefs) {
153-
Some(n) => n,
154-
None => {
155-
return self.tcx.ty_error_with_message(
156-
rustc_span::DUMMY_SP,
157-
&format!("failed autoderef {}", pick.autoderefs),
158-
);
159-
}
152+
let Some((ty, n)) = autoderef.nth(pick.autoderefs) else {
153+
return self.tcx.ty_error_with_message(
154+
rustc_span::DUMMY_SP,
155+
&format!("failed autoderef {}", pick.autoderefs),
156+
);
160157
};
161158
assert_eq!(n, pick.autoderefs);
162159

@@ -520,10 +517,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
520517
&self,
521518
predicates: &ty::InstantiatedPredicates<'tcx>,
522519
) -> Option<Span> {
523-
let sized_def_id = match self.tcx.lang_items().sized_trait() {
524-
Some(def_id) => def_id,
525-
None => return None,
526-
};
520+
let sized_def_id = self.tcx.lang_items().sized_trait()?;
527521

528522
traits::elaborate_predicates(self.tcx, predicates.predicates.iter().copied())
529523
// We don't care about regions here.

compiler/rustc_typeck/src/check/method/mod.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -371,15 +371,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
371371
// Trait must have a method named `m_name` and it should not have
372372
// type parameters or early-bound regions.
373373
let tcx = self.tcx;
374-
let method_item = match self.associated_value(trait_def_id, m_name) {
375-
Some(method_item) => method_item,
376-
None => {
377-
tcx.sess.delay_span_bug(
378-
span,
379-
"operator trait does not have corresponding operator method",
380-
);
381-
return None;
382-
}
374+
let Some(method_item) = self.associated_value(trait_def_id, m_name) else {
375+
tcx.sess.delay_span_bug(
376+
span,
377+
"operator trait does not have corresponding operator method",
378+
);
379+
return None;
383380
};
384381
let def_id = method_item.def_id;
385382
let generics = tcx.generics_of(def_id);

compiler/rustc_typeck/src/check/method/probe.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1246,9 +1246,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
12461246
return None;
12471247
}
12481248

1249-
let ty = match self_ty.kind() {
1250-
&ty::RawPtr(ty::TypeAndMut { ty, mutbl: hir::Mutability::Mut }) => ty,
1251-
_ => return None,
1249+
let &ty::RawPtr(ty::TypeAndMut { ty, mutbl: hir::Mutability::Mut }) = self_ty.kind() else {
1250+
return None;
12521251
};
12531252

12541253
let const_self_ty = ty::TypeAndMut { ty, mutbl: hir::Mutability::Not };

0 commit comments

Comments
 (0)