Skip to content

Commit f2d6770

Browse files
authored
Rollup merge of rust-lang#94146 - est31:let_else, r=cjgillot
Adopt let else in more places Continuation of rust-lang#89933, rust-lang#91018, rust-lang#91481, rust-lang#93046, rust-lang#93590, rust-lang#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 is the biggest of these PRs and handles the changes outside of rustdoc, rustc_typeck, rustc_const_eval, rustc_trait_selection, which were handled in PRs rust-lang#94139, rust-lang#94142, rust-lang#94143, rust-lang#94144.
2 parents 7ca1c48 + 2ef8af6 commit f2d6770

File tree

132 files changed

+539
-881
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+539
-881
lines changed

compiler/rustc_ast_lowering/src/asm.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
339339

340340
let idx2 = *o.get();
341341
let &(ref op2, op_sp2) = &operands[idx2];
342-
let reg2 = match op2.reg() {
343-
Some(asm::InlineAsmRegOrRegClass::Reg(r)) => r,
344-
_ => unreachable!(),
342+
let Some(asm::InlineAsmRegOrRegClass::Reg(reg2)) = op2.reg() else {
343+
unreachable!();
345344
};
346345

347346
let msg = format!(

compiler/rustc_ast_lowering/src/expr.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
330330
args: Vec<AstP<Expr>>,
331331
legacy_args_idx: &[usize],
332332
) -> hir::ExprKind<'hir> {
333-
let path = match f.kind {
334-
ExprKind::Path(None, ref mut path) => path,
335-
_ => unreachable!(),
333+
let ExprKind::Path(None, ref mut path) = f.kind else {
334+
unreachable!();
336335
};
337336

338337
// Split the arguments into const generics and normal arguments

compiler/rustc_ast_lowering/src/item.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1376,9 +1376,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
13761376
// keep track of the Span info. Now, `add_implicitly_sized` in `AstConv` checks both param bounds and
13771377
// where clauses for `?Sized`.
13781378
for pred in &generics.where_clause.predicates {
1379-
let bound_pred = match *pred {
1380-
WherePredicate::BoundPredicate(ref bound_pred) => bound_pred,
1381-
_ => continue,
1379+
let WherePredicate::BoundPredicate(ref bound_pred) = *pred else {
1380+
continue;
13821381
};
13831382
let compute_is_param = || {
13841383
// Check if the where clause type is a plain type parameter.

compiler/rustc_ast_passes/src/ast_validation.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,8 @@ impl<'a> AstValidator<'a> {
482482
}
483483

484484
fn check_foreign_kind_bodyless(&self, ident: Ident, kind: &str, body: Option<Span>) {
485-
let body = match body {
486-
None => return,
487-
Some(body) => body,
485+
let Some(body) = body else {
486+
return;
488487
};
489488
self.err_handler()
490489
.struct_span_err(ident.span, &format!("incorrect `{}` inside `extern` block", kind))
@@ -504,9 +503,8 @@ impl<'a> AstValidator<'a> {
504503

505504
/// An `fn` in `extern { ... }` cannot have a body `{ ... }`.
506505
fn check_foreign_fn_bodyless(&self, ident: Ident, body: Option<&Block>) {
507-
let body = match body {
508-
None => return,
509-
Some(body) => body,
506+
let Some(body) = body else {
507+
return;
510508
};
511509
self.err_handler()
512510
.struct_span_err(ident.span, "incorrect function inside `extern` block")

compiler/rustc_ast_passes/src/show_span.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,8 @@ impl<'a> Visitor<'a> for ShowSpanVisitor<'a> {
5757
}
5858

5959
pub fn run(span_diagnostic: &rustc_errors::Handler, mode: &str, krate: &ast::Crate) {
60-
let mode = match mode.parse().ok() {
61-
Some(mode) => mode,
62-
None => return,
60+
let Ok(mode) = mode.parse() else {
61+
return;
6362
};
6463
let mut v = ShowSpanVisitor { span_diagnostic, mode };
6564
visit::walk_crate(&mut v, krate);

compiler/rustc_attr/src/builtin.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -556,17 +556,14 @@ pub fn eval_condition(
556556
return false;
557557
}
558558
};
559-
let min_version = match parse_version(min_version.as_str(), false) {
560-
Some(ver) => ver,
561-
None => {
562-
sess.span_diagnostic
563-
.struct_span_warn(
564-
*span,
565-
"unknown version literal format, assuming it refers to a future version",
566-
)
567-
.emit();
568-
return false;
569-
}
559+
let Some(min_version) = parse_version(min_version.as_str(), false) else {
560+
sess.span_diagnostic
561+
.struct_span_warn(
562+
*span,
563+
"unknown version literal format, assuming it refers to a future version",
564+
)
565+
.emit();
566+
return false;
570567
};
571568
let rustc_version = parse_version(env!("CFG_RELEASE"), true).unwrap();
572569

@@ -669,9 +666,8 @@ where
669666
break;
670667
}
671668

672-
let meta = match attr.meta() {
673-
Some(meta) => meta,
674-
None => continue,
669+
let Some(meta) = attr.meta() else {
670+
continue;
675671
};
676672
let mut since = None;
677673
let mut note = None;

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+7-18
Original file line numberDiff line numberDiff line change
@@ -2071,22 +2071,17 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
20712071
) = rvalue
20722072
{
20732073
for operand in operands {
2074-
let assigned_from = match operand {
2075-
Operand::Copy(assigned_from) | Operand::Move(assigned_from) => {
2076-
assigned_from
2077-
}
2078-
_ => continue,
2074+
let (Operand::Copy(assigned_from) | Operand::Move(assigned_from)) = operand else {
2075+
continue;
20792076
};
20802077
debug!(
20812078
"annotate_argument_and_return_for_borrow: assigned_from={:?}",
20822079
assigned_from
20832080
);
20842081

20852082
// Find the local from the operand.
2086-
let assigned_from_local = match assigned_from.local_or_deref_local()
2087-
{
2088-
Some(local) => local,
2089-
None => continue,
2083+
let Some(assigned_from_local) = assigned_from.local_or_deref_local() else {
2084+
continue;
20902085
};
20912086

20922087
if assigned_from_local != target {
@@ -2138,10 +2133,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
21382133
);
21392134

21402135
// Find the local from the rvalue.
2141-
let assigned_from_local = match assigned_from.local_or_deref_local() {
2142-
Some(local) => local,
2143-
None => continue,
2144-
};
2136+
let Some(assigned_from_local) = assigned_from.local_or_deref_local() else { continue };
21452137
debug!(
21462138
"annotate_argument_and_return_for_borrow: \
21472139
assigned_from_local={:?}",
@@ -2189,11 +2181,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
21892181
assigned_to, args
21902182
);
21912183
for operand in args {
2192-
let assigned_from = match operand {
2193-
Operand::Copy(assigned_from) | Operand::Move(assigned_from) => {
2194-
assigned_from
2195-
}
2196-
_ => continue,
2184+
let (Operand::Copy(assigned_from) | Operand::Move(assigned_from)) = operand else {
2185+
continue;
21972186
};
21982187
debug!(
21992188
"annotate_argument_and_return_for_borrow: assigned_from={:?}",

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -650,13 +650,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
650650

651651
// The only kind of statement that we care about is assignments...
652652
if let StatementKind::Assign(box (place, rvalue)) = &stmt.kind {
653-
let into = match place.local_or_deref_local() {
654-
Some(into) => into,
655-
None => {
656-
// Continue at the next location.
657-
queue.push(current_location.successor_within_block());
658-
continue;
659-
}
653+
let Some(into) = place.local_or_deref_local() else {
654+
// Continue at the next location.
655+
queue.push(current_location.successor_within_block());
656+
continue;
660657
};
661658

662659
match rvalue {

compiler/rustc_borrowck/src/diagnostics/mod.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
444444
debug!("borrowed_content_source: init={:?}", init);
445445
// We're only interested in statements that initialized a value, not the
446446
// initializations from arguments.
447-
let loc = match init.location {
448-
InitLocation::Statement(stmt) => stmt,
449-
_ => continue,
450-
};
447+
let InitLocation::Statement(loc) = init.location else { continue };
451448

452449
let bbd = &self.body[loc.block];
453450
let is_terminator = bbd.statements.len() == loc.statement_index;
@@ -787,9 +784,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
787784
) -> UseSpans<'tcx> {
788785
use self::UseSpans::*;
789786

790-
let stmt = match self.body[location.block].statements.get(location.statement_index) {
791-
Some(stmt) => stmt,
792-
None => return OtherUse(self.body.source_info(location).span),
787+
let Some(stmt) = self.body[location.block].statements.get(location.statement_index) else {
788+
return OtherUse(self.body.source_info(location).span);
793789
};
794790

795791
debug!("move_spans: moved_place={:?} location={:?} stmt={:?}", moved_place, location, stmt);

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,9 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
188188
}
189189
// Error with the pattern
190190
LookupResult::Exact(_) => {
191-
let mpi = match self.move_data.rev_lookup.find(move_from.as_ref()) {
192-
LookupResult::Parent(Some(mpi)) => mpi,
191+
let LookupResult::Parent(Some(mpi)) = self.move_data.rev_lookup.find(move_from.as_ref()) else {
193192
// move_from should be a projection from match_place.
194-
_ => unreachable!("Probably not unreachable..."),
193+
unreachable!("Probably not unreachable...");
195194
};
196195
for ge in &mut *grouped_errors {
197196
if let GroupedMoveError::MovesFromValue {

compiler/rustc_borrowck/src/lib.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1914,10 +1914,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
19141914
// without going over a Deref.
19151915
let mut shortest_uninit_seen = None;
19161916
for prefix in this.prefixes(base, PrefixSet::Shallow) {
1917-
let mpi = match this.move_path_for_place(prefix) {
1918-
Some(mpi) => mpi,
1919-
None => continue,
1920-
};
1917+
let Some(mpi) = this.move_path_for_place(prefix) else { continue };
19211918

19221919
if maybe_uninits.contains(mpi) {
19231920
debug!(

compiler/rustc_borrowck/src/region_infer/mod.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -913,9 +913,8 @@ impl<'tcx> RegionInferenceContext<'tcx> {
913913
let TypeTest { generic_kind, lower_bound, locations, verify_bound: _ } = type_test;
914914

915915
let generic_ty = generic_kind.to_ty(tcx);
916-
let subject = match self.try_promote_type_test_subject(infcx, generic_ty) {
917-
Some(s) => s,
918-
None => return false,
916+
let Some(subject) = self.try_promote_type_test_subject(infcx, generic_ty) else {
917+
return false;
919918
};
920919

921920
// For each region outlived by lower_bound find a non-local,
@@ -1623,15 +1622,14 @@ impl<'tcx> RegionInferenceContext<'tcx> {
16231622
// If we have some bound universal region `'a`, then the only
16241623
// elements it can contain is itself -- we don't know anything
16251624
// else about it!
1626-
let error_element = match {
1625+
let Some(error_element) = ({
16271626
self.scc_values.elements_contained_in(longer_fr_scc).find(|element| match element {
16281627
RegionElement::Location(_) => true,
16291628
RegionElement::RootUniversalRegion(_) => true,
16301629
RegionElement::PlaceholderRegion(placeholder1) => placeholder != *placeholder1,
16311630
})
1632-
} {
1633-
Some(v) => v,
1634-
None => return,
1631+
}) else {
1632+
return;
16351633
};
16361634
debug!("check_bound_universal_region: error_element = {:?}", error_element);
16371635

compiler/rustc_borrowck/src/type_check/mod.rs

+33-43
Original file line numberDiff line numberDiff line change
@@ -810,13 +810,12 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
810810
ty::Adt(adt_def, substs) => (&adt_def.variants[variant_index], substs),
811811
ty::Generator(def_id, substs, _) => {
812812
let mut variants = substs.as_generator().state_tys(def_id, tcx);
813-
let mut variant = match variants.nth(variant_index.into()) {
814-
Some(v) => v,
815-
None => bug!(
813+
let Some(mut variant) = variants.nth(variant_index.into()) else {
814+
bug!(
816815
"variant_index of generator out of range: {:?}/{:?}",
817816
variant_index,
818817
substs.as_generator().state_tys(def_id, tcx).count()
819-
),
818+
);
820819
};
821820
return match variant.nth(field.index()) {
822821
Some(ty) => Ok(ty),
@@ -2178,35 +2177,29 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
21782177
}
21792178

21802179
CastKind::Pointer(PointerCast::MutToConstPointer) => {
2181-
let ty_from = match op.ty(body, tcx).kind() {
2182-
ty::RawPtr(ty::TypeAndMut {
2183-
ty: ty_from,
2184-
mutbl: hir::Mutability::Mut,
2185-
}) => ty_from,
2186-
_ => {
2187-
span_mirbug!(
2188-
self,
2189-
rvalue,
2190-
"unexpected base type for cast {:?}",
2191-
ty,
2192-
);
2193-
return;
2194-
}
2180+
let ty::RawPtr(ty::TypeAndMut {
2181+
ty: ty_from,
2182+
mutbl: hir::Mutability::Mut,
2183+
}) = op.ty(body, tcx).kind() else {
2184+
span_mirbug!(
2185+
self,
2186+
rvalue,
2187+
"unexpected base type for cast {:?}",
2188+
ty,
2189+
);
2190+
return;
21952191
};
2196-
let ty_to = match ty.kind() {
2197-
ty::RawPtr(ty::TypeAndMut {
2198-
ty: ty_to,
2199-
mutbl: hir::Mutability::Not,
2200-
}) => ty_to,
2201-
_ => {
2202-
span_mirbug!(
2203-
self,
2204-
rvalue,
2205-
"unexpected target type for cast {:?}",
2206-
ty,
2207-
);
2208-
return;
2209-
}
2192+
let ty::RawPtr(ty::TypeAndMut {
2193+
ty: ty_to,
2194+
mutbl: hir::Mutability::Not,
2195+
}) = ty.kind() else {
2196+
span_mirbug!(
2197+
self,
2198+
rvalue,
2199+
"unexpected target type for cast {:?}",
2200+
ty,
2201+
);
2202+
return;
22102203
};
22112204
if let Err(terr) = self.sub_types(
22122205
*ty_from,
@@ -2238,17 +2231,14 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
22382231
_ => None,
22392232
};
22402233

2241-
let (ty_elem, ty_mut) = match opt_ty_elem_mut {
2242-
Some(ty_elem_mut) => ty_elem_mut,
2243-
None => {
2244-
span_mirbug!(
2245-
self,
2246-
rvalue,
2247-
"ArrayToPointer cast from unexpected type {:?}",
2248-
ty_from,
2249-
);
2250-
return;
2251-
}
2234+
let Some((ty_elem, ty_mut)) = opt_ty_elem_mut else {
2235+
span_mirbug!(
2236+
self,
2237+
rvalue,
2238+
"ArrayToPointer cast from unexpected type {:?}",
2239+
ty_from,
2240+
);
2241+
return;
22522242
};
22532243

22542244
let (ty_to, ty_to_mut) = match ty.kind() {

compiler/rustc_borrowck/src/universal_regions.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,8 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
641641
let (&output, tuplized_inputs) =
642642
inputs_and_output.skip_binder().split_last().unwrap();
643643
assert_eq!(tuplized_inputs.len(), 1, "multiple closure inputs");
644-
let inputs = match tuplized_inputs[0].kind() {
645-
ty::Tuple(inputs) => inputs,
646-
_ => bug!("closure inputs not a tuple: {:?}", tuplized_inputs[0]),
644+
let ty::Tuple(inputs) = tuplized_inputs[0].kind() else {
645+
bug!("closure inputs not a tuple: {:?}", tuplized_inputs[0]);
647646
};
648647

649648
ty::Binder::bind_with_vars(

compiler/rustc_builtin_macros/src/cfg_accessible.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ impl MultiItemModifier for Expander {
4444
template,
4545
);
4646

47-
let path = match validate_input(ecx, meta_item) {
48-
Some(path) => path,
49-
None => return ExpandResult::Ready(Vec::new()),
47+
let Some(path) = validate_input(ecx, meta_item) else {
48+
return ExpandResult::Ready(Vec::new());
5049
};
5150

5251
match ecx.resolver.cfg_accessible(ecx.current_expansion.id, path) {

compiler/rustc_builtin_macros/src/compile_error.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ pub fn expand_compile_error<'cx>(
99
sp: Span,
1010
tts: TokenStream,
1111
) -> Box<dyn base::MacResult + 'cx> {
12-
let var = match get_single_str_from_tts(cx, sp, tts, "compile_error!") {
13-
None => return DummyResult::any(sp),
14-
Some(v) => v,
12+
let Some(var) = get_single_str_from_tts(cx, sp, tts, "compile_error!") else {
13+
return DummyResult::any(sp);
1514
};
1615

1716
cx.span_err(sp, &var);

0 commit comments

Comments
 (0)