Skip to content

Commit c360ddd

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

File tree

39 files changed

+281
-399
lines changed

39 files changed

+281
-399
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,11 +1534,10 @@ impl Expr {
15341534
// then type of result is trait object.
15351535
// Otherwise we don't assume the result type.
15361536
ExprKind::Binary(binop, lhs, rhs) if binop.node == BinOpKind::Add => {
1537-
if let (Some(lhs), Some(rhs)) = (lhs.to_bound(), rhs.to_bound()) {
1538-
TyKind::TraitObject(vec![lhs, rhs], TraitObjectSyntax::None)
1539-
} else {
1537+
let (Some(lhs), Some(rhs)) = (lhs.to_bound(), rhs.to_bound()) else {
15401538
return None;
1541-
}
1539+
};
1540+
TyKind::TraitObject(vec![lhs, rhs], TraitObjectSyntax::None)
15421541
}
15431542

15441543
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
@@ -850,16 +850,10 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
850850
// will only ever have one item at any given time, but by using a vector, we can pop from
851851
// it which simplifies the termination logic.
852852
let mut queue = vec![location];
853-
let mut target =
854-
if let Some(Statement { kind: StatementKind::Assign(box (place, _)), .. }) = stmt {
855-
if let Some(local) = place.as_local() {
856-
local
857-
} else {
858-
return false;
859-
}
860-
} else {
861-
return false;
862-
};
853+
let Some(Statement { kind: StatementKind::Assign(box (place, _)), .. }) = stmt else {
854+
return false;
855+
};
856+
let Some(mut target) = place.as_local() else { return false };
863857

864858
debug!("was_captured_by_trait: target={:?} queue={:?}", target, queue);
865859
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: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -847,11 +847,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
847847

848848
let fn_returns = self.infcx.tcx.return_type_impl_or_dyn_traits(suitable_region.scope);
849849

850-
let param = if let Some(param) =
850+
let Some(param) =
851851
find_param_with_region(self.infcx.tcx, self.mir_def_id(), f, outlived_f)
852-
{
853-
param
854-
} else {
852+
else {
855853
return;
856854
};
857855

@@ -930,37 +928,27 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
930928

931929
let tcx = self.infcx.tcx;
932930

933-
let instance = if let ConstraintCategory::CallArgument(Some(func_ty)) = category {
934-
let (fn_did, args) = match func_ty.kind() {
935-
ty::FnDef(fn_did, args) => (fn_did, args),
936-
_ => return,
937-
};
938-
debug!(?fn_did, ?args);
931+
let ConstraintCategory::CallArgument(Some(func_ty)) = category else { return };
932+
let ty::FnDef(fn_did, args) = func_ty.kind() else { return };
933+
debug!(?fn_did, ?args);
939934

940-
// Only suggest this on function calls, not closures
941-
let ty = tcx.type_of(fn_did).instantiate_identity();
942-
debug!("ty: {:?}, ty.kind: {:?}", ty, ty.kind());
943-
if let ty::Closure(_, _) = ty.kind() {
944-
return;
945-
}
946-
947-
if let Ok(Some(instance)) = ty::Instance::try_resolve(
948-
tcx,
949-
self.infcx.typing_env(self.infcx.param_env),
950-
*fn_did,
951-
self.infcx.resolve_vars_if_possible(args),
952-
) {
953-
instance
954-
} else {
955-
return;
956-
}
957-
} else {
935+
// Only suggest this on function calls, not closures
936+
let ty = tcx.type_of(fn_did).instantiate_identity();
937+
debug!("ty: {:?}, ty.kind: {:?}", ty, ty.kind());
938+
if let ty::Closure(_, _) = ty.kind() {
939+
return;
940+
}
941+
let Ok(Some(instance)) = ty::Instance::try_resolve(
942+
tcx,
943+
self.infcx.typing_env(self.infcx.param_env),
944+
*fn_did,
945+
self.infcx.resolve_vars_if_possible(args),
946+
) else {
958947
return;
959948
};
960949

961-
let param = match find_param_with_region(tcx, self.mir_def_id(), f, o) {
962-
Some(param) => param,
963-
None => return,
950+
let Some(param) = find_param_with_region(tcx, self.mir_def_id(), f, o) else {
951+
return;
964952
};
965953
debug!(?param);
966954

compiler/rustc_borrowck/src/polonius/legacy/accesses.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,11 @@ impl<'a, 'tcx> Visitor<'tcx> for AccessFactsExtractor<'a, 'tcx> {
6767
match context {
6868
PlaceContext::NonMutatingUse(_)
6969
| PlaceContext::MutatingUse(MutatingUseContext::Borrow) => {
70-
let path = match self.move_data.rev_lookup.find(place.as_ref()) {
71-
LookupResult::Exact(path) | LookupResult::Parent(Some(path)) => path,
72-
_ => {
73-
// There's no path access to emit.
74-
return;
75-
}
70+
let (LookupResult::Exact(path) | LookupResult::Parent(Some(path))) =
71+
self.move_data.rev_lookup.find(place.as_ref())
72+
else {
73+
// There's no path access to emit.
74+
return;
7675
};
7776
debug!("AccessFactsExtractor - emit path access ({path:?}, {location:?})");
7877
self.facts.path_accessed_at_base.push((path, self.location_to_index(location)));

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 } => {

0 commit comments

Comments
 (0)