Skip to content

Commit 9b84056

Browse files
committed
Use let...else instead of match foo { ... _ => return }; and if let ... else return
1 parent 6e41e61 commit 9b84056

File tree

42 files changed

+298
-436
lines changed

Some content is hidden

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

42 files changed

+298
-436
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,11 +1509,10 @@ impl Expr {
15091509
// then type of result is trait object.
15101510
// Otherwise we don't assume the result type.
15111511
ExprKind::Binary(binop, lhs, rhs) if binop.node == BinOpKind::Add => {
1512-
if let (Some(lhs), Some(rhs)) = (lhs.to_bound(), rhs.to_bound()) {
1513-
TyKind::TraitObject(vec![lhs, rhs], TraitObjectSyntax::None)
1514-
} else {
1512+
let (Some(lhs), Some(rhs)) = (lhs.to_bound(), rhs.to_bound()) else {
15151513
return None;
1516-
}
1514+
};
1515+
TyKind::TraitObject(vec![lhs, rhs], TraitObjectSyntax::None)
15171516
}
15181517

15191518
ExprKind::Underscore => TyKind::Infer,

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -402,20 +402,17 @@ impl MetaItem {
402402
thin_vec![PathSegment::path_root(span)]
403403
};
404404
loop {
405-
if let Some(&TokenTree::Token(Token { kind: token::Ident(name, _), span }, _)) =
405+
let Some(&TokenTree::Token(Token { kind: token::Ident(name, _), span }, _)) =
406406
iter.next().map(|tt| TokenTree::uninterpolate(tt)).as_deref()
407-
{
408-
segments.push(PathSegment::from_ident(Ident::new(name, span)));
409-
} else {
407+
else {
410408
return None;
411-
}
412-
if let Some(TokenTree::Token(Token { kind: token::PathSep, .. }, _)) =
413-
iter.peek()
414-
{
415-
iter.next();
416-
} else {
409+
};
410+
segments.push(PathSegment::from_ident(Ident::new(name, span)));
411+
let Some(TokenTree::Token(Token { kind: token::PathSep, .. }, _)) = iter.peek()
412+
else {
417413
break;
418-
}
414+
};
415+
iter.next();
419416
}
420417
let span = span.with_hi(segments.last().unwrap().ident.span.hi());
421418
Path { span, segments, tokens: None }

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -561,11 +561,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
561561
VarDebugInfoContents::Place(ref p) => p == place,
562562
_ => false,
563563
});
564-
let arg_name = if let Some(var_info) = var_info {
565-
var_info.name
566-
} else {
567-
return;
568-
};
564+
let Some(var_info) = var_info else { return };
565+
let arg_name = var_info.name;
569566
struct MatchArgFinder {
570567
expr_span: Span,
571568
match_arg_span: Option<Span>,

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -849,16 +849,10 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
849849
// will only ever have one item at any given time, but by using a vector, we can pop from
850850
// it which simplifies the termination logic.
851851
let mut queue = vec![location];
852-
let mut target =
853-
if let Some(Statement { kind: StatementKind::Assign(box (place, _)), .. }) = stmt {
854-
if let Some(local) = place.as_local() {
855-
local
856-
} else {
857-
return false;
858-
}
859-
} else {
860-
return false;
861-
};
852+
let Some(Statement { kind: StatementKind::Assign(box (place, _)), .. }) = stmt else {
853+
return false;
854+
};
855+
let Some(mut target) = place.as_local() else { return false };
862856

863857
debug!("was_captured_by_trait: target={:?} queue={:?}", target, queue);
864858
while let Some(current_location) = queue.pop() {

compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,16 +1124,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
11241124
use self::UseSpans::*;
11251125
debug!("borrow_spans: use_span={:?} location={:?}", use_span, location);
11261126

1127-
let target = match self.body[location.block].statements.get(location.statement_index) {
1128-
Some(Statement { kind: StatementKind::Assign(box (place, _)), .. }) => {
1129-
if let Some(local) = place.as_local() {
1130-
local
1131-
} else {
1132-
return OtherUse(use_span);
1133-
}
1134-
}
1135-
_ => return OtherUse(use_span),
1127+
let Some(Statement { kind: StatementKind::Assign(box (place, _)), .. }) =
1128+
self.body[location.block].statements.get(location.statement_index)
1129+
else {
1130+
return OtherUse(use_span);
11361131
};
1132+
let Some(target) = place.as_local() else { return OtherUse(use_span) };
11371133

11381134
if self.body.local_kind(target) != LocalKind::Temp {
11391135
// operands are always temporaries.

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,11 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
142142
} else {
143143
item_msg = access_place_desc;
144144
let local_info = self.body.local_decls[local].local_info();
145-
if let LocalInfo::StaticRef { def_id, .. } = *local_info {
146-
let static_name = &self.infcx.tcx.item_name(def_id);
147-
reason = format!(", as `{static_name}` is an immutable static item");
148-
} else {
145+
let LocalInfo::StaticRef { def_id, .. } = *local_info else {
149146
bug!("is_ref_to_static return true, but not ref to static?");
150-
}
147+
};
148+
let static_name = &self.infcx.tcx.item_name(def_id);
149+
reason = format!(", as `{static_name}` is an immutable static item");
151150
}
152151
}
153152
PlaceRef { local, projection: [proj_base @ .., ProjectionElem::Deref] } => {

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -819,11 +819,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
819819

820820
let fn_returns = self.infcx.tcx.return_type_impl_or_dyn_traits(suitable_region.scope);
821821

822-
let param = if let Some(param) =
822+
let Some(param) =
823823
find_param_with_region(self.infcx.tcx, self.mir_def_id(), f, outlived_f)
824-
{
825-
param
826-
} else {
824+
else {
827825
return;
828826
};
829827

@@ -902,31 +900,22 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
902900

903901
let tcx = self.infcx.tcx;
904902

905-
let instance = if let ConstraintCategory::CallArgument(Some(func_ty)) = category {
906-
let (fn_did, args) = match func_ty.kind() {
907-
ty::FnDef(fn_did, args) => (fn_did, args),
908-
_ => return,
909-
};
910-
debug!(?fn_did, ?args);
911-
912-
// Only suggest this on function calls, not closures
913-
let ty = tcx.type_of(fn_did).instantiate_identity();
914-
debug!("ty: {:?}, ty.kind: {:?}", ty, ty.kind());
915-
if let ty::Closure(_, _) = ty.kind() {
916-
return;
917-
}
903+
let ConstraintCategory::CallArgument(Some(func_ty)) = category else { return };
904+
let ty::FnDef(fn_did, args) = func_ty.kind() else { return };
905+
debug!(?fn_did, ?args);
918906

919-
if let Ok(Some(instance)) = ty::Instance::try_resolve(
920-
tcx,
921-
self.infcx.typing_env(self.infcx.param_env),
922-
*fn_did,
923-
self.infcx.resolve_vars_if_possible(args),
924-
) {
925-
instance
926-
} else {
927-
return;
928-
}
929-
} else {
907+
// Only suggest this on function calls, not closures
908+
let ty = tcx.type_of(fn_did).instantiate_identity();
909+
debug!("ty: {:?}, ty.kind: {:?}", ty, ty.kind());
910+
if let ty::Closure(_, _) = ty.kind() {
911+
return;
912+
}
913+
let Ok(Some(instance)) = ty::Instance::try_resolve(
914+
tcx,
915+
self.infcx.typing_env(self.infcx.param_env),
916+
*fn_did,
917+
self.infcx.resolve_vars_if_possible(args),
918+
) else {
930919
return;
931920
};
932921

compiler/rustc_codegen_cranelift/src/debuginfo/unwind.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,9 @@ impl UnwindContext {
5353
return;
5454
}
5555

56-
let unwind_info = if let Some(unwind_info) =
56+
let Some(unwind_info) =
5757
context.compiled_code().unwrap().create_unwind_info(module.isa()).unwrap()
58-
{
59-
unwind_info
60-
} else {
58+
else {
6159
return;
6260
};
6361

compiler/rustc_codegen_cranelift/src/optimize/peephole.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ pub(crate) fn maybe_known_branch_taken(
2929
arg: Value,
3030
test_zero: bool,
3131
) -> Option<bool> {
32-
let arg_inst = if let ValueDef::Result(arg_inst, 0) = bcx.func.dfg.value_def(arg) {
33-
arg_inst
34-
} else {
35-
return None;
36-
};
32+
let ValueDef::Result(arg_inst, 0) = bcx.func.dfg.value_def(arg) else { return None };
3733

3834
match bcx.func.dfg.insts[arg_inst] {
3935
InstructionData::UnaryImm { opcode: Opcode::Iconst, imm } => {

compiler/rustc_codegen_gcc/src/intrinsic/simd.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -766,24 +766,23 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(
766766
return Err(());
767767
}};
768768
}
769-
let (elem_ty_str, elem_ty, cast_type) = if let ty::Float(ref f) = *in_elem.kind() {
770-
let elem_ty = bx.cx.type_float_from_ty(*f);
771-
match f.bit_width() {
772-
16 => ("", elem_ty, Some(bx.cx.double_type)),
773-
32 => ("f", elem_ty, None),
774-
64 => ("", elem_ty, None),
775-
_ => {
776-
return_error!(InvalidMonomorphization::FloatingPointVector {
777-
span,
778-
name,
779-
f_ty: *f,
780-
in_ty
781-
});
782-
}
783-
}
784-
} else {
769+
let ty::Float(ref f) = *in_elem.kind() else {
785770
return_error!(InvalidMonomorphization::FloatingPointType { span, name, in_ty });
786771
};
772+
let elem_ty = bx.cx.type_float_from_ty(*f);
773+
let (elem_ty_str, elem_ty, cast_type) = match f.bit_width() {
774+
16 => ("", elem_ty, Some(bx.cx.double_type)),
775+
32 => ("f", elem_ty, None),
776+
64 => ("", elem_ty, None),
777+
_ => {
778+
return_error!(InvalidMonomorphization::FloatingPointVector {
779+
span,
780+
name,
781+
f_ty: *f,
782+
in_ty
783+
});
784+
}
785+
};
787786

788787
let vec_ty = bx.cx.type_vector(elem_ty, in_len);
789788

0 commit comments

Comments
 (0)