Skip to content

Commit 9323028

Browse files
committed
Auto merge of #94144 - est31:let_else_trait_selection, r=cjgillot
rustc_trait_selection: adopt let else in more places Continuation of #89933, #91018, #91481, #93046, #93590, #94011. I have extended my clippy lint to also recognize tuple passing and match statements. The diff caused by fixing it is way above 1 thousand lines. Thus, I split it up into multiple pull requests to make reviewing easier. This PR handles rustc_trait_selection.
2 parents 6abd8cd + dab5c44 commit 9323028

File tree

10 files changed

+75
-117
lines changed

10 files changed

+75
-117
lines changed

compiler/rustc_trait_selection/src/traits/auto_trait.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,16 @@ impl<'tcx> AutoTraitFinder<'tcx> {
148148
// traits::project will see that 'T: SomeTrait' is in our ParamEnv, allowing
149149
// SelectionContext to return it back to us.
150150

151-
let (new_env, user_env) = match self.evaluate_predicates(
151+
let Some((new_env, user_env)) = self.evaluate_predicates(
152152
&infcx,
153153
trait_did,
154154
ty,
155155
orig_env,
156156
orig_env,
157157
&mut fresh_preds,
158158
false,
159-
) {
160-
Some(e) => e,
161-
None => return AutoTraitResult::NegativeImpl,
159+
) else {
160+
return AutoTraitResult::NegativeImpl;
162161
};
163162

164163
let (full_env, full_user_env) = self

compiler/rustc_trait_selection/src/traits/coherence.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -328,18 +328,15 @@ fn negative_impl<'cx, 'tcx>(
328328
impl_trait_ref_and_oblig(selcx, impl1_env, impl2_def_id, impl2_substs);
329329

330330
// do the impls unify? If not, not disjoint.
331-
let more_obligations = match infcx
331+
let Ok(InferOk { obligations: more_obligations, .. }) = infcx
332332
.at(&ObligationCause::dummy(), impl1_env)
333333
.eq(impl1_trait_ref, impl2_trait_ref)
334-
{
335-
Ok(InferOk { obligations, .. }) => obligations,
336-
Err(_) => {
337-
debug!(
338-
"explicit_disjoint: {:?} does not unify with {:?}",
339-
impl1_trait_ref, impl2_trait_ref
340-
);
341-
return false;
342-
}
334+
else {
335+
debug!(
336+
"explicit_disjoint: {:?} does not unify with {:?}",
337+
impl1_trait_ref, impl2_trait_ref
338+
);
339+
return false;
343340
};
344341

345342
let opt_failing_obligation = obligations

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+15-20
Original file line numberDiff line numberDiff line change
@@ -804,9 +804,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
804804
return;
805805
}
806806

807-
let found_trait_ty = match found_trait_ref.self_ty().no_bound_vars() {
808-
Some(ty) => ty,
809-
None => return,
807+
let Some(found_trait_ty) = found_trait_ref.self_ty().no_bound_vars() else {
808+
return;
810809
};
811810

812811
let found_did = match *found_trait_ty.kind() {
@@ -2097,26 +2096,24 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
20972096
err: &mut Diagnostic,
20982097
obligation: &PredicateObligation<'tcx>,
20992098
) {
2100-
let (pred, item_def_id, span) = match (
2099+
let (
2100+
ty::PredicateKind::Trait(pred),
2101+
&ObligationCauseCode::BindingObligation(item_def_id, span),
2102+
) = (
21012103
obligation.predicate.kind().skip_binder(),
21022104
obligation.cause.code().peel_derives(),
2103-
) {
2104-
(
2105-
ty::PredicateKind::Trait(pred),
2106-
&ObligationCauseCode::BindingObligation(item_def_id, span),
2107-
) => (pred, item_def_id, span),
2108-
_ => return,
2105+
) else {
2106+
return;
21092107
};
21102108
debug!(
21112109
"suggest_unsized_bound_if_applicable: pred={:?} item_def_id={:?} span={:?}",
21122110
pred, item_def_id, span
21132111
);
2114-
let node = match (
2112+
let (Some(node), true) = (
21152113
self.tcx.hir().get_if_local(item_def_id),
21162114
Some(pred.def_id()) == self.tcx.lang_items().sized_trait(),
2117-
) {
2118-
(Some(node), true) => node,
2119-
_ => return,
2115+
) else {
2116+
return;
21202117
};
21212118
self.maybe_suggest_unsized_generics(err, span, node);
21222119
}
@@ -2127,9 +2124,8 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
21272124
span: Span,
21282125
node: Node<'hir>,
21292126
) {
2130-
let generics = match node.generics() {
2131-
Some(generics) => generics,
2132-
None => return,
2127+
let Some(generics) = node.generics() else {
2128+
return;
21332129
};
21342130
let sized_trait = self.tcx.lang_items().sized_trait();
21352131
debug!("maybe_suggest_unsized_generics: generics.params={:?}", generics.params);
@@ -2142,9 +2138,8 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
21422138
.iter()
21432139
.all(|bound| bound.trait_ref().and_then(|tr| tr.trait_def_id()) != sized_trait)
21442140
});
2145-
let param = match param {
2146-
Some(param) => param,
2147-
_ => return,
2141+
let Some(param) = param else {
2142+
return;
21482143
};
21492144
let param_def_id = self.tcx.hir().local_def_id(param.hir_id).to_def_id();
21502145
let preds = generics.where_clause.predicates.iter();

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+14-23
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
512512
| ObligationCauseCode::BuiltinDerivedObligation(cause) => cause.parent_trait_pred,
513513
_ => trait_pred,
514514
};
515-
let real_ty = match real_trait_pred.self_ty().no_bound_vars() {
516-
Some(ty) => ty,
517-
None => return,
515+
let Some(real_ty) = real_trait_pred.self_ty().no_bound_vars() else {
516+
return;
518517
};
519518

520519
if let ty::Ref(region, base_ty, mutbl) = *real_ty.kind() {
@@ -586,9 +585,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
586585
err: &mut Diagnostic,
587586
trait_pred: ty::PolyTraitPredicate<'tcx>,
588587
) {
589-
let self_ty = match trait_pred.self_ty().no_bound_vars() {
590-
None => return,
591-
Some(ty) => ty,
588+
let Some(self_ty) = trait_pred.self_ty().no_bound_vars() else {
589+
return;
592590
};
593591

594592
let (def_id, output_ty, callable) = match *self_ty.kind() {
@@ -600,9 +598,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
600598

601599
// `mk_trait_obligation_with_new_self_ty` only works for types with no escaping bound
602600
// variables, so bail out if we have any.
603-
let output_ty = match output_ty.no_bound_vars() {
604-
Some(ty) => ty,
605-
None => return,
601+
let Some(output_ty) = output_ty.no_bound_vars() else {
602+
return;
606603
};
607604

608605
let new_obligation =
@@ -624,9 +621,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
624621
..
625622
})) => {
626623
err.span_label(*span, "consider calling this closure");
627-
let name = match self.get_closure_name(def_id, err, &msg) {
628-
Some(name) => name,
629-
None => return,
624+
let Some(name) = self.get_closure_name(def_id, err, &msg) else {
625+
return;
630626
};
631627
let args = decl.inputs.iter().map(|_| "_").collect::<Vec<_>>().join(", ");
632628
let sugg = format!("({})", args);
@@ -823,9 +819,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
823819
return;
824820
}
825821

826-
let mut suggested_ty = match trait_pred.self_ty().no_bound_vars() {
827-
Some(ty) => ty,
828-
None => return,
822+
let Some(mut suggested_ty) = trait_pred.self_ty().no_bound_vars() else {
823+
return;
829824
};
830825

831826
for refs_remaining in 0..refs_number {
@@ -1039,9 +1034,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
10391034
fn return_type_span(&self, obligation: &PredicateObligation<'tcx>) -> Option<Span> {
10401035
let hir = self.tcx.hir();
10411036
let parent_node = hir.get_parent_node(obligation.cause.body_id);
1042-
let sig = match hir.find(parent_node) {
1043-
Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, ..), .. })) => sig,
1044-
_ => return None,
1037+
let Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, ..), .. })) = hir.find(parent_node) else {
1038+
return None;
10451039
};
10461040

10471041
if let hir::FnRetTy::Return(ret_ty) = sig.decl.output { Some(ret_ty.span) } else { None }
@@ -1491,11 +1485,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
14911485

14921486
// Only continue if a generator was found.
14931487
debug!(?generator, ?trait_ref, ?target_ty, "maybe_note_obligation_cause_for_async_await");
1494-
let (generator_did, trait_ref, target_ty) = match (generator, trait_ref, target_ty) {
1495-
(Some(generator_did), Some(trait_ref), Some(target_ty)) => {
1496-
(generator_did, trait_ref, target_ty)
1497-
}
1498-
_ => return false,
1488+
let (Some(generator_did), Some(trait_ref), Some(target_ty)) = (generator, trait_ref, target_ty) else {
1489+
return false;
14991490
};
15001491

15011492
let span = self.tcx.def_span(generator_did);

compiler/rustc_trait_selection/src/traits/mod.rs

+10-17
Original file line numberDiff line numberDiff line change
@@ -340,19 +340,16 @@ pub fn normalize_param_env_or_error<'tcx>(
340340
"normalize_param_env_or_error: predicates=(non-outlives={:?}, outlives={:?})",
341341
predicates, outlives_predicates
342342
);
343-
let non_outlives_predicates = match do_normalize_predicates(
343+
let Ok(non_outlives_predicates) = do_normalize_predicates(
344344
tcx,
345345
region_context,
346346
cause.clone(),
347347
elaborated_env,
348348
predicates,
349-
) {
350-
Ok(predicates) => predicates,
349+
) else {
351350
// An unnormalized env is better than nothing.
352-
Err(ErrorReported) => {
353-
debug!("normalize_param_env_or_error: errored resolving non-outlives predicates");
354-
return elaborated_env;
355-
}
351+
debug!("normalize_param_env_or_error: errored resolving non-outlives predicates");
352+
return elaborated_env;
356353
};
357354

358355
debug!("normalize_param_env_or_error: non-outlives predicates={:?}", non_outlives_predicates);
@@ -367,19 +364,16 @@ pub fn normalize_param_env_or_error<'tcx>(
367364
unnormalized_env.reveal(),
368365
unnormalized_env.constness(),
369366
);
370-
let outlives_predicates = match do_normalize_predicates(
367+
let Ok(outlives_predicates) = do_normalize_predicates(
371368
tcx,
372369
region_context,
373370
cause,
374371
outlives_env,
375372
outlives_predicates,
376-
) {
377-
Ok(predicates) => predicates,
373+
) else {
378374
// An unnormalized env is better than nothing.
379-
Err(ErrorReported) => {
380-
debug!("normalize_param_env_or_error: errored resolving outlives predicates");
381-
return elaborated_env;
382-
}
375+
debug!("normalize_param_env_or_error: errored resolving outlives predicates");
376+
return elaborated_env;
383377
};
384378
debug!("normalize_param_env_or_error: outlives predicates={:?}", outlives_predicates);
385379

@@ -834,9 +828,8 @@ pub fn vtable_trait_upcasting_coercion_new_vptr_slot<'tcx>(
834828
selcx.select(&obligation).unwrap()
835829
});
836830

837-
let implsrc_traitcasting = match implsrc {
838-
Some(ImplSource::TraitUpcasting(data)) => data,
839-
_ => bug!(),
831+
let Some(ImplSource::TraitUpcasting(implsrc_traitcasting)) = implsrc else {
832+
bug!();
840833
};
841834

842835
implsrc_traitcasting.vtable_vptr_slot

compiler/rustc_trait_selection/src/traits/object_safety.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,8 @@ fn trait_has_sized_self(tcx: TyCtxt<'_>, trait_def_id: DefId) -> bool {
322322
}
323323

324324
fn generics_require_sized_self(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
325-
let sized_def_id = match tcx.lang_items().sized_trait() {
326-
Some(def_id) => def_id,
327-
None => {
328-
return false; /* No Sized trait, can't require it! */
329-
}
325+
let Some(sized_def_id) = tcx.lang_items().sized_trait() else {
326+
return false; /* No Sized trait, can't require it! */
330327
};
331328

332329
// Search for a predicate like `Self : Sized` amongst the trait bounds.

compiler/rustc_trait_selection/src/traits/project.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1830,9 +1830,8 @@ fn confirm_impl_candidate<'cx, 'tcx>(
18301830
let trait_def_id = tcx.trait_id_of_impl(impl_def_id).unwrap();
18311831

18321832
let param_env = obligation.param_env;
1833-
let assoc_ty = match assoc_def(selcx, impl_def_id, assoc_item_id) {
1834-
Ok(assoc_ty) => assoc_ty,
1835-
Err(ErrorReported) => return Progress { term: tcx.ty_error().into(), obligations: nested },
1833+
let Ok(assoc_ty) = assoc_def(selcx, impl_def_id, assoc_item_id) else {
1834+
return Progress { term: tcx.ty_error().into(), obligations: nested };
18361835
};
18371836

18381837
if !assoc_ty.item.defaultness.has_value() {

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
436436
obligation: &TraitObligation<'tcx>,
437437
candidates: &mut SelectionCandidateSet<'tcx>,
438438
) {
439-
let kind = match self.tcx().fn_trait_kind_from_lang_item(obligation.predicate.def_id()) {
440-
Some(k) => k,
441-
None => {
442-
return;
443-
}
439+
let Some(kind) = self.tcx().fn_trait_kind_from_lang_item(obligation.predicate.def_id()) else {
440+
return;
444441
};
445442

446443
// Okay to skip binder because the substs on closure types never
@@ -763,12 +760,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
763760
// T: Trait
764761
// so it seems ok if we (conservatively) fail to accept that `Unsize`
765762
// obligation above. Should be possible to extend this in the future.
766-
let source = match obligation.self_ty().no_bound_vars() {
767-
Some(t) => t,
768-
None => {
769-
// Don't add any candidates if there are bound regions.
770-
return;
771-
}
763+
let Some(source) = obligation.self_ty().no_bound_vars() else {
764+
// Don't add any candidates if there are bound regions.
765+
return;
772766
};
773767
let target = obligation.predicate.skip_binder().trait_ref.substs.type_at(1);
774768

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
272272
} else {
273273
bug!("unexpected builtin trait {:?}", trait_def)
274274
};
275-
let nested = match conditions {
276-
BuiltinImplConditions::Where(nested) => nested,
277-
_ => bug!("obligation {:?} had matched a builtin impl but now doesn't", obligation),
275+
let BuiltinImplConditions::Where(nested) = conditions else {
276+
bug!("obligation {:?} had matched a builtin impl but now doesn't", obligation);
278277
};
279278

280279
let cause = obligation.derived_cause(BuiltinDerivedObligation);
@@ -421,9 +420,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
421420
let trait_predicate = self.infcx.replace_bound_vars_with_placeholders(obligation.predicate);
422421
let self_ty = self.infcx.shallow_resolve(trait_predicate.self_ty());
423422
let obligation_trait_ref = ty::Binder::dummy(trait_predicate.trait_ref);
424-
let data = match *self_ty.kind() {
425-
ty::Dynamic(data, ..) => data,
426-
_ => span_bug!(obligation.cause.span, "object candidate with non-object"),
423+
let ty::Dynamic(data, ..) = *self_ty.kind() else {
424+
span_bug!(obligation.cause.span, "object candidate with non-object");
427425
};
428426

429427
let object_trait_ref = data.principal().unwrap_or_else(|| {
@@ -593,9 +591,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
593591
// touch bound regions, they just capture the in-scope
594592
// type/region parameters.
595593
let self_ty = self.infcx.shallow_resolve(obligation.self_ty().skip_binder());
596-
let (generator_def_id, substs) = match *self_ty.kind() {
597-
ty::Generator(id, substs, _) => (id, substs),
598-
_ => bug!("closure candidate for non-closure {:?}", obligation),
594+
let ty::Generator(generator_def_id, substs, _) = *self_ty.kind() else {
595+
bug!("closure candidate for non-closure {:?}", obligation);
599596
};
600597

601598
debug!(?obligation, ?generator_def_id, ?substs, "confirm_generator_candidate");
@@ -622,9 +619,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
622619
// touch bound regions, they just capture the in-scope
623620
// type/region parameters.
624621
let self_ty = self.infcx.shallow_resolve(obligation.self_ty().skip_binder());
625-
let (closure_def_id, substs) = match *self_ty.kind() {
626-
ty::Closure(id, substs) => (id, substs),
627-
_ => bug!("closure candidate for non-closure {:?}", obligation),
622+
let ty::Closure(closure_def_id, substs) = *self_ty.kind() else {
623+
bug!("closure candidate for non-closure {:?}", obligation);
628624
};
629625

630626
let trait_ref = self.closure_trait_ref_unnormalized(obligation, substs);

compiler/rustc_trait_selection/src/traits/specialize/mod.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,15 @@ fn fulfill_implication<'a, 'tcx>(
192192
impl_trait_ref_and_oblig(selcx, param_env, target_impl, target_substs);
193193

194194
// do the impls unify? If not, no specialization.
195-
let more_obligations =
196-
match infcx.at(&ObligationCause::dummy(), param_env).eq(source_trait_ref, target_trait_ref)
197-
{
198-
Ok(InferOk { obligations, .. }) => obligations,
199-
Err(_) => {
200-
debug!(
201-
"fulfill_implication: {:?} does not unify with {:?}",
202-
source_trait_ref, target_trait_ref
203-
);
204-
return Err(());
205-
}
206-
};
195+
let Ok(InferOk { obligations: more_obligations, .. }) =
196+
infcx.at(&ObligationCause::dummy(), param_env).eq(source_trait_ref, target_trait_ref)
197+
else {
198+
debug!(
199+
"fulfill_implication: {:?} does not unify with {:?}",
200+
source_trait_ref, target_trait_ref
201+
);
202+
return Err(());
203+
};
207204

208205
// attempt to prove all of the predicates for impl2 given those for impl1
209206
// (which are packed up in penv)

0 commit comments

Comments
 (0)