Skip to content

Commit 637d8b8

Browse files
authored
Rollup merge of #94011 - est31:let_else, r=lcnr
Even more let_else adoptions Continuation of #89933, #91018, #91481, #93046, #93590.
2 parents 09350d2 + 60f969a commit 637d8b8

File tree

26 files changed

+50
-104
lines changed

26 files changed

+50
-104
lines changed

compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,9 @@ trait TypeOpInfo<'tcx> {
142142
let tcx = mbcx.infcx.tcx;
143143
let base_universe = self.base_universe();
144144

145-
let adjusted_universe = if let Some(adjusted) =
145+
let Some(adjusted_universe) =
146146
placeholder.universe.as_u32().checked_sub(base_universe.as_u32())
147-
{
148-
adjusted
149-
} else {
147+
else {
150148
mbcx.buffer_error(self.fallback_error(tcx, cause.span));
151149
return;
152150
};

compiler/rustc_borrowck/src/diagnostics/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -867,15 +867,14 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
867867
kind: TerminatorKind::Call { fn_span, from_hir_call, .. }, ..
868868
}) = &self.body[location.block].terminator
869869
{
870-
let (method_did, method_substs) = if let Some(info) =
870+
let Some((method_did, method_substs)) =
871871
rustc_const_eval::util::find_self_call(
872872
self.infcx.tcx,
873873
&self.body,
874874
target_temp,
875875
location.block,
876-
) {
877-
info
878-
} else {
876+
)
877+
else {
879878
return normal_ret;
880879
};
881880

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -639,11 +639,9 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
639639
let hir_map = self.infcx.tcx.hir();
640640
let my_def = self.body.source.def_id();
641641
let my_hir = hir_map.local_def_id_to_hir_id(my_def.as_local().unwrap());
642-
let td = if let Some(a) =
642+
let Some(td) =
643643
self.infcx.tcx.impl_of_method(my_def).and_then(|x| self.infcx.tcx.trait_id_of_impl(x))
644-
{
645-
a
646-
} else {
644+
else {
647645
return (false, None);
648646
};
649647
(

compiler/rustc_builtin_macros/src/concat_bytes.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use rustc_expand::base::{self, DummyResult};
66

77
/// Emits errors for literal expressions that are invalid inside and outside of an array.
88
fn invalid_type_err(cx: &mut base::ExtCtxt<'_>, expr: &P<rustc_ast::Expr>, is_nested: bool) {
9-
let lit = if let ast::ExprKind::Lit(lit) = &expr.kind {
10-
lit
11-
} else {
9+
let ast::ExprKind::Lit(lit) = &expr.kind else {
1210
unreachable!();
1311
};
1412
match lit.kind {

compiler/rustc_builtin_macros/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#![feature(decl_macro)]
1010
#![feature(is_sorted)]
1111
#![feature(nll)]
12+
#![feature(let_else)]
1213
#![feature(proc_macro_internals)]
1314
#![feature(proc_macro_quote)]
1415
#![recursion_limit = "256"]

compiler/rustc_codegen_ssa/src/back/link.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -216,17 +216,18 @@ pub fn each_linked_rlib(
216216
}
217217
let name = &info.crate_name[&cnum];
218218
let used_crate_source = &info.used_crate_source[&cnum];
219-
let path = if let Some((path, _)) = &used_crate_source.rlib {
220-
path
221-
} else if used_crate_source.rmeta.is_some() {
222-
return Err(format!(
223-
"could not find rlib for: `{}`, found rmeta (metadata) file",
224-
name
225-
));
219+
if let Some((path, _)) = &used_crate_source.rlib {
220+
f(cnum, &path);
226221
} else {
227-
return Err(format!("could not find rlib for: `{}`", name));
228-
};
229-
f(cnum, &path);
222+
if used_crate_source.rmeta.is_some() {
223+
return Err(format!(
224+
"could not find rlib for: `{}`, found rmeta (metadata) file",
225+
name
226+
));
227+
} else {
228+
return Err(format!("could not find rlib for: `{}`", name));
229+
}
230+
}
230231
}
231232
Ok(())
232233
}

compiler/rustc_codegen_ssa/src/back/metadata.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,7 @@ fn create_object_file(sess: &Session) -> Option<write::Object<'static>> {
200200
// `SHF_EXCLUDE` flag we can set on sections in an object file to get
201201
// automatically removed from the final output.
202202
pub fn create_rmeta_file(sess: &Session, metadata: &[u8]) -> Vec<u8> {
203-
let mut file = if let Some(file) = create_object_file(sess) {
204-
file
205-
} else {
203+
let Some(mut file) = create_object_file(sess) else {
206204
// This is used to handle all "other" targets. This includes targets
207205
// in two categories:
208206
//
@@ -262,9 +260,7 @@ pub fn create_compressed_metadata_file(
262260
) -> Vec<u8> {
263261
let mut compressed = rustc_metadata::METADATA_HEADER.to_vec();
264262
FrameEncoder::new(&mut compressed).write_all(metadata.raw_data()).unwrap();
265-
let mut file = if let Some(file) = create_object_file(sess) {
266-
file
267-
} else {
263+
let Some(mut file) = create_object_file(sess) else {
268264
return compressed.to_vec();
269265
};
270266
let section = file.add_section(

compiler/rustc_index/src/bit_set.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -852,11 +852,7 @@ impl<T: Idx> HybridBitSet<T> {
852852
Bound::Excluded(end) => end.index(),
853853
Bound::Unbounded => self.domain_size() - 1,
854854
};
855-
let len = if let Some(l) = end.checked_sub(start) {
856-
l
857-
} else {
858-
return;
859-
};
855+
let Some(len) = end.checked_sub(start) else { return };
860856
match self {
861857
HybridBitSet::Sparse(sparse) if sparse.len() + len < SPARSE_MAX => {
862858
// The set is sparse and has space for `elems`.

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
553553
let ty_msg = match (local_visitor.found_node_ty, local_visitor.found_exact_method_call) {
554554
(_, Some(_)) => String::new(),
555555
(Some(ty), _) if ty.is_closure() => {
556-
let substs =
557-
if let ty::Closure(_, substs) = *ty.kind() { substs } else { unreachable!() };
556+
let ty::Closure(_, substs) = *ty.kind() else { unreachable!() };
558557
let fn_sig = substs.as_closure().sig();
559558
let args = closure_args(&fn_sig);
560559
let ret = fn_sig.output().skip_binder().to_string();
@@ -597,8 +596,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
597596
let param_type = arg_data.kind.descr();
598597
let suffix = match local_visitor.found_node_ty {
599598
Some(ty) if ty.is_closure() => {
600-
let substs =
601-
if let ty::Closure(_, substs) = *ty.kind() { substs } else { unreachable!() };
599+
let ty::Closure(_, substs) = *ty.kind() else { unreachable!() };
602600
let fn_sig = substs.as_closure().sig();
603601
let ret = fn_sig.output().skip_binder().to_string();
604602

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
982982
for local_id in hir.iter_local_def_id() {
983983
let def_id = local_id.to_def_id();
984984
let def_kind = tcx.opt_def_kind(local_id);
985-
let def_kind = if let Some(def_kind) = def_kind { def_kind } else { continue };
985+
let Some(def_kind) = def_kind else { continue };
986986
record!(self.tables.def_kind[def_id] <- match def_kind {
987987
// Replace Ctor by the enclosing object to avoid leaking details in children crates.
988988
DefKind::Ctor(CtorOf::Struct, _) => DefKind::Struct,

compiler/rustc_middle/src/dep_graph/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl rustc_query_system::dep_graph::DepKind for DepKind {
6161
OP: for<'a> FnOnce(TaskDepsRef<'a>),
6262
{
6363
ty::tls::with_context_opt(|icx| {
64-
let icx = if let Some(icx) = icx { icx } else { return };
64+
let Some(icx) = icx else { return };
6565
op(icx.task_deps)
6666
})
6767
}

compiler/rustc_mir_transform/src/generator.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1241,9 +1241,7 @@ impl<'tcx> MirPass<'tcx> for StateTransform {
12411241
}
12421242

12431243
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
1244-
let yield_ty = if let Some(yield_ty) = body.yield_ty() {
1245-
yield_ty
1246-
} else {
1244+
let Some(yield_ty) = body.yield_ty() else {
12471245
// This only applies to generators
12481246
return;
12491247
};

compiler/rustc_mir_transform/src/normalize_array_len.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,7 @@ fn normalize_array_len_call<'tcx>(
212212
let Some(local) = place.as_local() else { return };
213213
match operand {
214214
Operand::Copy(place) | Operand::Move(place) => {
215-
let operand_local =
216-
if let Some(local) = place.local_or_deref_local() {
217-
local
218-
} else {
219-
return;
220-
};
215+
let Some(operand_local) = place.local_or_deref_local() else { return; };
221216
if !interesting_locals.contains(operand_local) {
222217
return;
223218
}

compiler/rustc_monomorphize/src/collector.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -947,9 +947,7 @@ fn visit_instance_use<'tcx>(
947947
/// Returns `true` if we should codegen an instance in the local crate, or returns `false` if we
948948
/// can just link to the upstream crate and therefore don't need a mono item.
949949
fn should_codegen_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx>) -> bool {
950-
let def_id = if let Some(def_id) = instance.def.def_id_if_not_guaranteed_local_codegen() {
951-
def_id
952-
} else {
950+
let Some(def_id) = instance.def.def_id_if_not_guaranteed_local_codegen() else {
953951
return true;
954952
};
955953

compiler/rustc_monomorphize/src/util.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ use std::io::prelude::*;
88
/// During the same compile all closures dump the information in the same file
99
/// "closure_profile_XXXXX.csv", which is created in the directory where the compiler is invoked.
1010
crate fn dump_closure_profile<'tcx>(tcx: TyCtxt<'tcx>, closure_instance: Instance<'tcx>) {
11-
let mut file = if let Ok(file) = OpenOptions::new()
11+
let Ok(mut file) = OpenOptions::new()
1212
.create(true)
1313
.append(true)
1414
.open(&format!("closure_profile_{}.csv", std::process::id()))
15-
{
16-
file
17-
} else {
15+
else {
1816
eprintln!("Cound't open file for writing closure profile");
1917
return;
2018
};

compiler/rustc_parse/src/lexer/mod.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,7 @@ impl<'a> StringReader<'a> {
158158
Some(match token {
159159
rustc_lexer::TokenKind::LineComment { doc_style } => {
160160
// Skip non-doc comments
161-
let doc_style = if let Some(doc_style) = doc_style {
162-
doc_style
163-
} else {
161+
let Some(doc_style) = doc_style else {
164162
self.lint_unicode_text_flow(start);
165163
return None;
166164
};
@@ -185,9 +183,7 @@ impl<'a> StringReader<'a> {
185183
}
186184

187185
// Skip non-doc comments
188-
let doc_style = if let Some(doc_style) = doc_style {
189-
doc_style
190-
} else {
186+
let Some(doc_style) = doc_style else {
191187
self.lint_unicode_text_flow(start);
192188
return None;
193189
};

compiler/rustc_parse/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![feature(crate_visibility_modifier)]
55
#![feature(if_let_guard)]
66
#![feature(box_patterns)]
7+
#![feature(let_else)]
78
#![recursion_limit = "256"]
89

910
#[macro_use]

compiler/rustc_resolve/src/late/diagnostics.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -704,17 +704,15 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
704704
) = &bounded_ty.kind
705705
{
706706
// use this to verify that ident is a type param.
707-
let partial_res = if let Ok(Some(partial_res)) = self.resolve_qpath_anywhere(
707+
let Ok(Some(partial_res)) = self.resolve_qpath_anywhere(
708708
bounded_ty.id,
709709
None,
710710
&Segment::from_path(path),
711711
Namespace::TypeNS,
712712
span,
713713
true,
714714
CrateLint::No,
715-
) {
716-
partial_res
717-
} else {
715+
) else {
718716
return false;
719717
};
720718
if !(matches!(
@@ -731,17 +729,15 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
731729

732730
if let ast::TyKind::Path(None, type_param_path) = &ty.peel_refs().kind {
733731
// Confirm that the `SelfTy` is a type parameter.
734-
let partial_res = if let Ok(Some(partial_res)) = self.resolve_qpath_anywhere(
732+
let Ok(Some(partial_res)) = self.resolve_qpath_anywhere(
735733
bounded_ty.id,
736734
None,
737735
&Segment::from_path(type_param_path),
738736
Namespace::TypeNS,
739737
span,
740738
true,
741739
CrateLint::No,
742-
) {
743-
partial_res
744-
} else {
740+
) else {
745741
return false;
746742
};
747743
if !(matches!(

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1099,9 +1099,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
10991099
_ => return false,
11001100
};
11011101

1102-
let ret_ty = if let hir::FnRetTy::Return(ret_ty) = sig.decl.output {
1103-
ret_ty
1104-
} else {
1102+
let hir::FnRetTy::Return(ret_ty) = sig.decl.output else {
11051103
return false;
11061104
};
11071105

@@ -1168,17 +1166,15 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
11681166
};
11691167

11701168
let sm = self.tcx.sess.source_map();
1171-
let snippet = if let (true, hir::TyKind::TraitObject(..), Ok(snippet), true) = (
1169+
let (true, hir::TyKind::TraitObject(..), Ok(snippet), true) = (
11721170
// Verify that we're dealing with a return `dyn Trait`
11731171
ret_ty.span.overlaps(span),
11741172
&ret_ty.kind,
11751173
sm.span_to_snippet(ret_ty.span),
11761174
// If any of the return types does not conform to the trait, then we can't
11771175
// suggest `impl Trait` nor trait objects: it is a type mismatch error.
11781176
all_returns_conform_to_trait,
1179-
) {
1180-
snippet
1181-
} else {
1177+
) else {
11821178
return false;
11831179
};
11841180
err.code(error_code!(E0746));

compiler/rustc_typeck/src/check/expr.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1318,10 +1318,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13181318
base_expr: &'tcx Option<&'tcx hir::Expr<'tcx>>,
13191319
) -> Ty<'tcx> {
13201320
// Find the relevant variant
1321-
let (variant, adt_ty) = if let Some(variant_ty) = self.check_struct_path(qpath, expr.hir_id)
1322-
{
1323-
variant_ty
1324-
} else {
1321+
let Some((variant, adt_ty)) = self.check_struct_path(qpath, expr.hir_id) else {
13251322
self.check_struct_fields_on_error(fields, base_expr);
13261323
return self.tcx.ty_error();
13271324
};

src/librustdoc/clean/simplify.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ crate fn where_clauses(cx: &DocContext<'_>, clauses: Vec<WP>) -> Vec<WP> {
5151
// Look for equality predicates on associated types that can be merged into
5252
// general bound predicates
5353
equalities.retain(|&(ref lhs, ref rhs)| {
54-
let (self_, trait_did, name) = if let Some(p) = lhs.projection() {
55-
p
56-
} else {
54+
let Some((self_, trait_did, name)) = lhs.projection() else {
5755
return true;
5856
};
5957
let generic = match self_ {

src/librustdoc/html/markdown.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
236236
let should_panic;
237237
let ignore;
238238
let edition;
239-
let kind = if let Some(Event::Start(Tag::CodeBlock(kind))) = event {
240-
kind
241-
} else {
239+
let Some(Event::Start(Tag::CodeBlock(kind))) = event else {
242240
return event;
243241
};
244242

src/librustdoc/html/render/print_item.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1752,9 +1752,7 @@ fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
17521752
<ul>",
17531753
);
17541754

1755-
let adt = if let Adt(adt, _) = ty_layout.ty.kind() {
1756-
adt
1757-
} else {
1755+
let Adt(adt, _) = ty_layout.ty.kind() else {
17581756
span_bug!(tcx.def_span(ty_def_id), "not an adt")
17591757
};
17601758

src/librustdoc/passes/collect_intra_doc_links.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1226,9 +1226,7 @@ impl LinkCollector<'_, '_> {
12261226
let base_node =
12271227
if item.is_mod() && inner_docs { self.mod_ids.last().copied() } else { parent_node };
12281228

1229-
let mut module_id = if let Some(id) = base_node {
1230-
id
1231-
} else {
1229+
let Some(mut module_id) = base_node else {
12321230
// This is a bug.
12331231
debug!("attempting to resolve item without parent module: {}", path_str);
12341232
resolution_failure(
@@ -1977,9 +1975,7 @@ fn resolution_failure(
19771975
// If so, report it and say the first which failed; if not, say the first path segment didn't resolve.
19781976
let mut name = path_str;
19791977
'outer: loop {
1980-
let (start, end) = if let Some(x) = split(name) {
1981-
x
1982-
} else {
1978+
let Some((start, end)) = split(name) else {
19831979
// avoid bug that marked [Quux::Z] as missing Z, not Quux
19841980
if partial_res.is_none() {
19851981
*unresolved = name.into();

src/librustdoc/scrape_examples.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,7 @@ where
152152
}
153153
hir::ExprKind::MethodCall(_, _, span) => {
154154
let types = tcx.typeck(ex.hir_id.owner);
155-
let def_id = if let Some(def_id) = types.type_dependent_def_id(ex.hir_id) {
156-
def_id
157-
} else {
155+
let Some(def_id) = types.type_dependent_def_id(ex.hir_id) else {
158156
trace!("type_dependent_def_id({}) = None", ex.hir_id);
159157
return;
160158
};

0 commit comments

Comments
 (0)