Skip to content

Commit b32845c

Browse files
authored
Rollup merge of #148837 - estebank:let-else, r=Kivooeo
Use `let...else` instead of `match foo { ... _ => return };` and `if let ... else return`
2 parents 397339e + 97c7742 commit b32845c

File tree

47 files changed

+284
-391
lines changed

Some content is hidden

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

47 files changed

+284
-391
lines changed

compiler/rustc_ast/src/ast.rs

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

15431542
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
@@ -447,20 +447,17 @@ impl MetaItem {
447447
thin_vec![PathSegment::path_root(span)]
448448
};
449449
loop {
450-
if let Some(&TokenTree::Token(Token { kind: token::Ident(name, _), span }, _)) =
450+
let Some(&TokenTree::Token(Token { kind: token::Ident(name, _), span }, _)) =
451451
iter.next().map(|tt| TokenTree::uninterpolate(tt)).as_deref()
452-
{
453-
segments.push(PathSegment::from_ident(Ident::new(name, span)));
454-
} else {
452+
else {
455453
return None;
456-
}
457-
if let Some(TokenTree::Token(Token { kind: token::PathSep, .. }, _)) =
458-
iter.peek()
459-
{
460-
iter.next();
461-
} else {
454+
};
455+
segments.push(PathSegment::from_ident(Ident::new(name, span)));
456+
let Some(TokenTree::Token(Token { kind: token::PathSep, .. }, _)) = iter.peek()
457+
else {
462458
break;
463-
}
459+
};
460+
iter.next();
464461
}
465462
let span = span.with_hi(segments.last().unwrap().ident.span.hi());
466463
Path { span, segments, tokens: None }

compiler/rustc_ast/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
// tidy-alphabetical-start
88
#![cfg_attr(bootstrap, feature(array_windows))]
9+
#![deny(clippy::manual_let_else)]
910
#![doc(test(attr(deny(warnings), allow(internal_features))))]
1011
#![feature(associated_type_defaults)]
1112
#![feature(box_patterns)]

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/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
// tidy-alphabetical-start
44
#![allow(internal_features)]
5+
#![deny(clippy::manual_let_else)]
56
#![feature(assert_matches)]
67
#![feature(box_patterns)]
78
#![feature(file_buffered)]

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)));

0 commit comments

Comments
 (0)