Skip to content

Commit fe94f78

Browse files
committed
6 - Make more use of let_chains
Continuation of #94376. cc #53667
1 parent 4ce3749 commit fe94f78

File tree

8 files changed

+97
-118
lines changed

8 files changed

+97
-118
lines changed

compiler/rustc_mir_build/src/build/expr/stmt.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,20 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
116116
// it is usually better to focus on `the_value` rather
117117
// than the entirety of block(s) surrounding it.
118118
let adjusted_span = (|| {
119-
if let ExprKind::Block { body } = &expr.kind {
120-
if let Some(tail_expr) = body.expr {
121-
let mut expr = &this.thir[tail_expr];
122-
while let ExprKind::Block {
123-
body: Block { expr: Some(nested_expr), .. },
124-
}
125-
| ExprKind::Scope { value: nested_expr, .. } = expr.kind
126-
{
127-
expr = &this.thir[nested_expr];
128-
}
129-
this.block_context.push(BlockFrame::TailExpr {
130-
tail_result_is_ignored: true,
131-
span: expr.span,
132-
});
133-
return Some(expr.span);
119+
if let ExprKind::Block { body } = &expr.kind && let Some(tail_ex) = body.expr {
120+
let mut expr = &this.thir[tail_ex];
121+
while let ExprKind::Block {
122+
body: Block { expr: Some(nested_expr), .. },
134123
}
124+
| ExprKind::Scope { value: nested_expr, .. } = expr.kind
125+
{
126+
expr = &this.thir[nested_expr];
127+
}
128+
this.block_context.push(BlockFrame::TailExpr {
129+
tail_result_is_ignored: true,
130+
span: expr.span,
131+
});
132+
return Some(expr.span);
135133
}
136134
None
137135
})();

compiler/rustc_mir_build/src/build/matches/mod.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1597,13 +1597,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
15971597
}
15981598

15991599
// Insert a Shallow borrow of any places that is switched on.
1600-
if let Some(fb) = fake_borrows {
1601-
if let Ok(match_place_resolved) =
1602-
match_place.clone().try_upvars_resolved(self.tcx, self.typeck_results)
1603-
{
1604-
let resolved_place = match_place_resolved.into_place(self.tcx, self.typeck_results);
1605-
fb.insert(resolved_place);
1606-
}
1600+
if let Some(fb) = fake_borrows && let Ok(match_place_resolved) =
1601+
match_place.clone().try_upvars_resolved(self.tcx, self.typeck_results)
1602+
{
1603+
let resolved_place = match_place_resolved.into_place(self.tcx, self.typeck_results);
1604+
fb.insert(resolved_place);
16071605
}
16081606

16091607
// perform the test, branching to one of N blocks. For each of

compiler/rustc_mir_build/src/build/mod.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -877,14 +877,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
877877
let arg_local = self.local_decls.push(LocalDecl::with_source_info(ty, source_info));
878878

879879
// If this is a simple binding pattern, give debuginfo a nice name.
880-
if let Some(arg) = arg_opt {
881-
if let Some(ident) = arg.pat.simple_ident() {
882-
self.var_debug_info.push(VarDebugInfo {
883-
name: ident.name,
884-
source_info,
885-
value: VarDebugInfoContents::Place(arg_local.into()),
886-
});
887-
}
880+
if let Some(arg) = arg_opt && let Some(ident) = arg.pat.simple_ident() {
881+
self.var_debug_info.push(VarDebugInfo {
882+
name: ident.name,
883+
source_info,
884+
value: VarDebugInfoContents::Place(arg_local.into()),
885+
});
888886
}
889887
}
890888

compiler/rustc_mir_build/src/check_unsafety.rs

+15-19
Original file line numberDiff line numberDiff line change
@@ -416,23 +416,21 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
416416
}
417417
ExprKind::Field { lhs, .. } => {
418418
let lhs = &self.thir[lhs];
419-
if let ty::Adt(adt_def, _) = lhs.ty.kind() {
420-
if adt_def.is_union() {
421-
if let Some((assigned_ty, assignment_span)) = self.assignment_info {
422-
// To avoid semver hazard, we only consider `Copy` and `ManuallyDrop` non-dropping.
423-
if !(assigned_ty
424-
.ty_adt_def()
425-
.map_or(false, |adt| adt.is_manually_drop())
426-
|| assigned_ty
427-
.is_copy_modulo_regions(self.tcx.at(expr.span), self.param_env))
428-
{
429-
self.requires_unsafe(assignment_span, AssignToDroppingUnionField);
430-
} else {
431-
// write to non-drop union field, safe
432-
}
419+
if let ty::Adt(adt_def, _) = lhs.ty.kind() && adt_def.is_union() {
420+
if let Some((assigned_ty, assignment_span)) = self.assignment_info {
421+
// To avoid semver hazard, we only consider `Copy` and `ManuallyDrop` non-dropping.
422+
if !(assigned_ty
423+
.ty_adt_def()
424+
.map_or(false, |adt| adt.is_manually_drop())
425+
|| assigned_ty
426+
.is_copy_modulo_regions(self.tcx.at(expr.span), self.param_env))
427+
{
428+
self.requires_unsafe(assignment_span, AssignToDroppingUnionField);
433429
} else {
434-
self.requires_unsafe(expr.span, AccessToUnionField);
430+
// write to non-drop union field, safe
435431
}
432+
} else {
433+
self.requires_unsafe(expr.span, AccessToUnionField);
436434
}
437435
}
438436
}
@@ -476,10 +474,8 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
476474
}
477475
ExprKind::Let { expr: expr_id, .. } => {
478476
let let_expr = &self.thir[expr_id];
479-
if let ty::Adt(adt_def, _) = let_expr.ty.kind() {
480-
if adt_def.is_union() {
481-
self.requires_unsafe(expr.span, AccessToUnionField);
482-
}
477+
if let ty::Adt(adt_def, _) = let_expr.ty.kind() && adt_def.is_union() {
478+
self.requires_unsafe(expr.span, AccessToUnionField);
483479
}
484480
}
485481
_ => {}

compiler/rustc_mir_build/src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
//! Construction of MIR from HIR.
22
//!
33
//! This crate also contains the match exhaustiveness and usefulness checking.
4+
#![allow(rustc::potential_query_instability)]
5+
#![feature(bool_to_option)]
46
#![feature(box_patterns)]
57
#![feature(control_flow_enum)]
68
#![feature(crate_visibility_modifier)]
7-
#![feature(bool_to_option)]
9+
#![feature(let_chains)]
810
#![feature(let_else)]
9-
#![feature(once_cell)]
1011
#![feature(min_specialization)]
12+
#![feature(once_cell)]
1113
#![recursion_limit = "256"]
12-
#![allow(rustc::potential_query_instability)]
1314

1415
#[macro_use]
1516
extern crate tracing;

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+36-42
Original file line numberDiff line numberDiff line change
@@ -315,47 +315,43 @@ fn check_for_bindings_named_same_as_variants(
315315
rf: RefutableFlag,
316316
) {
317317
pat.walk_always(|p| {
318-
if let hir::PatKind::Binding(_, _, ident, None) = p.kind {
319-
if let Some(ty::BindByValue(hir::Mutability::Not)) =
318+
if let hir::PatKind::Binding(_, _, ident, None) = p.kind
319+
&& let Some(ty::BindByValue(hir::Mutability::Not)) =
320320
cx.typeck_results.extract_binding_mode(cx.tcx.sess, p.hir_id, p.span)
321-
{
322-
let pat_ty = cx.typeck_results.pat_ty(p).peel_refs();
323-
if let ty::Adt(edef, _) = pat_ty.kind() {
324-
if edef.is_enum()
325-
&& edef.variants.iter().any(|variant| {
326-
variant.ident(cx.tcx) == ident && variant.ctor_kind == CtorKind::Const
327-
})
328-
{
329-
let variant_count = edef.variants.len();
330-
cx.tcx.struct_span_lint_hir(
331-
BINDINGS_WITH_VARIANT_NAME,
332-
p.hir_id,
321+
&& let pat_ty = cx.typeck_results.pat_ty(p).peel_refs()
322+
&& let ty::Adt(edef, _) = pat_ty.kind()
323+
&& edef.is_enum()
324+
&& edef.variants.iter().any(|variant| {
325+
variant.ident(cx.tcx) == ident && variant.ctor_kind == CtorKind::Const
326+
})
327+
{
328+
let variant_count = edef.variants.len();
329+
cx.tcx.struct_span_lint_hir(
330+
BINDINGS_WITH_VARIANT_NAME,
331+
p.hir_id,
332+
p.span,
333+
|lint| {
334+
let ty_path = cx.tcx.def_path_str(edef.did);
335+
let mut err = lint.build(&format!(
336+
"pattern binding `{}` is named the same as one \
337+
of the variants of the type `{}`",
338+
ident, ty_path
339+
));
340+
err.code(error_code!(E0170));
341+
// If this is an irrefutable pattern, and there's > 1 variant,
342+
// then we can't actually match on this. Applying the below
343+
// suggestion would produce code that breaks on `check_irrefutable`.
344+
if rf == Refutable || variant_count == 1 {
345+
err.span_suggestion(
333346
p.span,
334-
|lint| {
335-
let ty_path = cx.tcx.def_path_str(edef.did);
336-
let mut err = lint.build(&format!(
337-
"pattern binding `{}` is named the same as one \
338-
of the variants of the type `{}`",
339-
ident, ty_path
340-
));
341-
err.code(error_code!(E0170));
342-
// If this is an irrefutable pattern, and there's > 1 variant,
343-
// then we can't actually match on this. Applying the below
344-
// suggestion would produce code that breaks on `check_irrefutable`.
345-
if rf == Refutable || variant_count == 1 {
346-
err.span_suggestion(
347-
p.span,
348-
"to match on the variant, qualify the path",
349-
format!("{}::{}", ty_path, ident),
350-
Applicability::MachineApplicable,
351-
);
352-
}
353-
err.emit();
354-
},
355-
)
347+
"to match on the variant, qualify the path",
348+
format!("{}::{}", ty_path, ident),
349+
Applicability::MachineApplicable,
350+
);
356351
}
357-
}
358-
}
352+
err.emit();
353+
},
354+
)
359355
}
360356
});
361357
}
@@ -622,10 +618,8 @@ fn maybe_point_at_variant<'a, 'p: 'a, 'tcx: 'a>(
622618
let mut covered = vec![];
623619
for pattern in patterns {
624620
if let Variant(variant_index) = pattern.ctor() {
625-
if let ty::Adt(this_def, _) = pattern.ty().kind() {
626-
if this_def.did != def.did {
627-
continue;
628-
}
621+
if let ty::Adt(this_def, _) = pattern.ty().kind() && this_def.did != def.did {
622+
continue;
629623
}
630624
let sp = def.variants[*variant_index].ident(cx.tcx).span;
631625
if covered.contains(&sp) {

compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -680,27 +680,23 @@ impl<'tcx> Constructor<'tcx> {
680680
///
681681
/// This means that the variant has a stdlib unstable feature marking it.
682682
pub(super) fn is_unstable_variant(&self, pcx: PatCtxt<'_, '_, 'tcx>) -> bool {
683-
if let Constructor::Variant(idx) = self {
684-
if let ty::Adt(adt, _) = pcx.ty.kind() {
685-
let variant_def_id = adt.variants[*idx].def_id;
686-
// Filter variants that depend on a disabled unstable feature.
687-
return matches!(
688-
pcx.cx.tcx.eval_stability(variant_def_id, None, DUMMY_SP, None),
689-
EvalResult::Deny { .. }
690-
);
691-
}
683+
if let Constructor::Variant(idx) = self && let ty::Adt(adt, _) = pcx.ty.kind() {
684+
let variant_def_id = adt.variants[*idx].def_id;
685+
// Filter variants that depend on a disabled unstable feature.
686+
return matches!(
687+
pcx.cx.tcx.eval_stability(variant_def_id, None, DUMMY_SP, None),
688+
EvalResult::Deny { .. }
689+
);
692690
}
693691
false
694692
}
695693

696694
/// Checks if the `Constructor` is a `Constructor::Variant` with a `#[doc(hidden)]`
697695
/// attribute.
698696
pub(super) fn is_doc_hidden_variant(&self, pcx: PatCtxt<'_, '_, 'tcx>) -> bool {
699-
if let Constructor::Variant(idx) = self {
700-
if let ty::Adt(adt, _) = pcx.ty.kind() {
701-
let variant_def_id = adt.variants[*idx].def_id;
702-
return pcx.cx.tcx.is_doc_hidden(variant_def_id);
703-
}
697+
if let Constructor::Variant(idx) = self && let ty::Adt(adt, _) = pcx.ty.kind() {
698+
let variant_def_id = adt.variants[*idx].def_id;
699+
return pcx.cx.tcx.is_doc_hidden(variant_def_id);
704700
}
705701
false
706702
}

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -790,16 +790,14 @@ crate fn compare_const_vals<'tcx>(
790790
};
791791
}
792792

793-
if let ty::Str = ty.kind() {
794-
if let (
795-
ty::ConstKind::Value(a_val @ ConstValue::Slice { .. }),
796-
ty::ConstKind::Value(b_val @ ConstValue::Slice { .. }),
797-
) = (a.val(), b.val())
798-
{
799-
let a_bytes = get_slice_bytes(&tcx, a_val);
800-
let b_bytes = get_slice_bytes(&tcx, b_val);
801-
return from_bool(a_bytes == b_bytes);
802-
}
793+
if let ty::Str = ty.kind() && let (
794+
ty::ConstKind::Value(a_val @ ConstValue::Slice { .. }),
795+
ty::ConstKind::Value(b_val @ ConstValue::Slice { .. }),
796+
) = (a.val(), b.val())
797+
{
798+
let a_bytes = get_slice_bytes(&tcx, a_val);
799+
let b_bytes = get_slice_bytes(&tcx, b_val);
800+
return from_bool(a_bytes == b_bytes);
803801
}
804802
fallback()
805803
}

0 commit comments

Comments
 (0)