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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 4 additions & 6 deletions
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 10 additions & 14 deletions
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

Lines changed: 7 additions & 18 deletions
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

Lines changed: 4 additions & 7 deletions
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

Lines changed: 3 additions & 7 deletions
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

Lines changed: 2 additions & 3 deletions
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 {

0 commit comments

Comments
 (0)