Skip to content

Commit f61284d

Browse files
committed
Use let...else instead of match foo { ... _ => return }; and if let ... else return
1 parent 5bc3450 commit f61284d

File tree

35 files changed

+244
-351
lines changed

35 files changed

+244
-351
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,11 +1521,10 @@ impl Expr {
15211521
// then type of result is trait object.
15221522
// Otherwise we don't assume the result type.
15231523
ExprKind::Binary(binop, lhs, rhs) if binop.node == BinOpKind::Add => {
1524-
if let (Some(lhs), Some(rhs)) = (lhs.to_bound(), rhs.to_bound()) {
1525-
TyKind::TraitObject(vec![lhs, rhs], TraitObjectSyntax::None)
1526-
} else {
1524+
let (Some(lhs), Some(rhs)) = (lhs.to_bound(), rhs.to_bound()) else {
15271525
return None;
1528-
}
1526+
};
1527+
TyKind::TraitObject(vec![lhs, rhs], TraitObjectSyntax::None)
15291528
}
15301529

15311530
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
@@ -830,11 +830,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
830830

831831
let fn_returns = self.infcx.tcx.return_type_impl_or_dyn_traits(suitable_region.scope);
832832

833-
let param = if let Some(param) =
833+
let Some(param) =
834834
find_param_with_region(self.infcx.tcx, self.mir_def_id(), f, outlived_f)
835-
{
836-
param
837-
} else {
835+
else {
838836
return;
839837
};
840838

@@ -913,31 +911,22 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
913911

914912
let tcx = self.infcx.tcx;
915913

916-
let instance = if let ConstraintCategory::CallArgument(Some(func_ty)) = category {
917-
let (fn_did, args) = match func_ty.kind() {
918-
ty::FnDef(fn_did, args) => (fn_did, args),
919-
_ => return,
920-
};
921-
debug!(?fn_did, ?args);
922-
923-
// Only suggest this on function calls, not closures
924-
let ty = tcx.type_of(fn_did).instantiate_identity();
925-
debug!("ty: {:?}, ty.kind: {:?}", ty, ty.kind());
926-
if let ty::Closure(_, _) = ty.kind() {
927-
return;
928-
}
914+
let ConstraintCategory::CallArgument(Some(func_ty)) = category else { return };
915+
let ty::FnDef(fn_did, args) = func_ty.kind() else { return };
916+
debug!(?fn_did, ?args);
929917

930-
if let Ok(Some(instance)) = ty::Instance::try_resolve(
931-
tcx,
932-
self.infcx.typing_env(self.infcx.param_env),
933-
*fn_did,
934-
self.infcx.resolve_vars_if_possible(args),
935-
) {
936-
instance
937-
} else {
938-
return;
939-
}
940-
} else {
918+
// Only suggest this on function calls, not closures
919+
let ty = tcx.type_of(fn_did).instantiate_identity();
920+
debug!("ty: {:?}, ty.kind: {:?}", ty, ty.kind());
921+
if let ty::Closure(_, _) = ty.kind() {
922+
return;
923+
}
924+
let Ok(Some(instance)) = ty::Instance::try_resolve(
925+
tcx,
926+
self.infcx.typing_env(self.infcx.param_env),
927+
*fn_did,
928+
self.infcx.resolve_vars_if_possible(args),
929+
) else {
941930
return;
942931
};
943932

compiler/rustc_codegen_cranelift/src/debuginfo/unwind.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,9 @@ impl UnwindContext {
130130
return;
131131
}
132132

133-
let unwind_info = if let Some(unwind_info) =
133+
let Some(unwind_info) =
134134
context.compiled_code().unwrap().create_unwind_info(module.isa()).unwrap()
135-
{
136-
unwind_info
137-
} else {
135+
else {
138136
return;
139137
};
140138

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
@@ -774,24 +774,23 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(
774774
return Err(());
775775
}};
776776
}
777-
let (elem_ty_str, elem_ty, cast_type) = if let ty::Float(ref f) = *in_elem.kind() {
778-
let elem_ty = bx.cx.type_float_from_ty(*f);
779-
match f.bit_width() {
780-
16 => ("", elem_ty, Some(bx.cx.double_type)),
781-
32 => ("f", elem_ty, None),
782-
64 => ("", elem_ty, None),
783-
_ => {
784-
return_error!(InvalidMonomorphization::FloatingPointVector {
785-
span,
786-
name,
787-
f_ty: *f,
788-
in_ty
789-
});
790-
}
791-
}
792-
} else {
777+
let ty::Float(ref f) = *in_elem.kind() else {
793778
return_error!(InvalidMonomorphization::FloatingPointType { span, name, in_ty });
794779
};
780+
let elem_ty = bx.cx.type_float_from_ty(*f);
781+
let (elem_ty_str, elem_ty, cast_type) = match f.bit_width() {
782+
16 => ("", elem_ty, Some(bx.cx.double_type)),
783+
32 => ("f", elem_ty, None),
784+
64 => ("", elem_ty, None),
785+
_ => {
786+
return_error!(InvalidMonomorphization::FloatingPointVector {
787+
span,
788+
name,
789+
f_ty: *f,
790+
in_ty
791+
});
792+
}
793+
};
795794

796795
let vec_ty = bx.cx.type_vector(elem_ty, in_len);
797796

0 commit comments

Comments
 (0)