Skip to content

Commit 8cdafbf

Browse files
authored
Rollup merge of rust-lang#94396 - c410-f3r:yet-more-let-chains, r=Dylan-DPC
1 - Make more use of `let_chains` Continuation of rust-lang#94376. cc rust-lang#53667
2 parents 6a70556 + ca2ad69 commit 8cdafbf

File tree

11 files changed

+142
-164
lines changed

11 files changed

+142
-164
lines changed

compiler/rustc_typeck/src/astconv/mod.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1054,12 +1054,11 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10541054
let mut result = Vec::new();
10551055

10561056
for ast_bound in ast_bounds {
1057-
if let Some(trait_ref) = ast_bound.trait_ref() {
1058-
if let Some(trait_did) = trait_ref.trait_def_id() {
1059-
if self.tcx().trait_may_define_assoc_type(trait_did, assoc_name) {
1060-
result.push(ast_bound.clone());
1061-
}
1062-
}
1057+
if let Some(trait_ref) = ast_bound.trait_ref()
1058+
&& let Some(trait_did) = trait_ref.trait_def_id()
1059+
&& self.tcx().trait_may_define_assoc_type(trait_did, assoc_name)
1060+
{
1061+
result.push(ast_bound.clone());
10631062
}
10641063
}
10651064

compiler/rustc_typeck/src/check/check.rs

+17-22
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,12 @@ pub(super) fn check_fn<'a, 'tcx>(
282282
sess.span_err(decl.inputs[0].span, "argument should be `&PanicInfo`");
283283
}
284284

285-
if let Node::Item(item) = hir.get(fn_id) {
286-
if let ItemKind::Fn(_, ref generics, _) = item.kind {
287-
if !generics.params.is_empty() {
285+
if let Node::Item(item) = hir.get(fn_id)
286+
&& let ItemKind::Fn(_, ref generics, _) = item.kind
287+
&& !generics.params.is_empty()
288+
{
288289
sess.span_err(span, "should have no type parameters");
289290
}
290-
}
291-
}
292291
} else {
293292
let span = sess.source_map().guess_head_span(span);
294293
sess.span_err(span, "function should have one argument");
@@ -319,17 +318,15 @@ pub(super) fn check_fn<'a, 'tcx>(
319318
sess.span_err(decl.inputs[0].span, "argument should be `Layout`");
320319
}
321320

322-
if let Node::Item(item) = hir.get(fn_id) {
323-
if let ItemKind::Fn(_, ref generics, _) = item.kind {
324-
if !generics.params.is_empty() {
321+
if let Node::Item(item) = hir.get(fn_id)
322+
&& let ItemKind::Fn(_, ref generics, _) = item.kind
323+
&& !generics.params.is_empty()
324+
{
325325
sess.span_err(
326326
span,
327-
"`#[alloc_error_handler]` function should have no type \
328-
parameters",
327+
"`#[alloc_error_handler]` function should have no type parameters",
329328
);
330329
}
331-
}
332-
}
333330
} else {
334331
let span = sess.source_map().guess_head_span(span);
335332
sess.span_err(span, "function should have one argument");
@@ -1146,18 +1143,17 @@ pub(super) fn check_packed(tcx: TyCtxt<'_>, sp: Span, def: &ty::AdtDef) {
11461143
if repr.packed() {
11471144
for attr in tcx.get_attrs(def.did).iter() {
11481145
for r in attr::find_repr_attrs(&tcx.sess, attr) {
1149-
if let attr::ReprPacked(pack) = r {
1150-
if let Some(repr_pack) = repr.pack {
1151-
if pack as u64 != repr_pack.bytes() {
1146+
if let attr::ReprPacked(pack) = r
1147+
&& let Some(repr_pack) = repr.pack
1148+
&& pack as u64 != repr_pack.bytes()
1149+
{
11521150
struct_span_err!(
11531151
tcx.sess,
11541152
sp,
11551153
E0634,
11561154
"type has conflicting packed representation hints"
11571155
)
11581156
.emit();
1159-
}
1160-
}
11611157
}
11621158
}
11631159
}
@@ -1399,12 +1395,11 @@ fn display_discriminant_value<'tcx>(
13991395
) -> String {
14001396
if let Some(expr) = &variant.disr_expr {
14011397
let body = &tcx.hir().body(expr.body).value;
1402-
if let hir::ExprKind::Lit(lit) = &body.kind {
1403-
if let rustc_ast::LitKind::Int(lit_value, _int_kind) = &lit.node {
1404-
if evaluated != *lit_value {
1398+
if let hir::ExprKind::Lit(lit) = &body.kind
1399+
&& let rustc_ast::LitKind::Int(lit_value, _int_kind) = &lit.node
1400+
&& evaluated != *lit_value
1401+
{
14051402
return format!("`{}` (overflowed from `{}`)", evaluated, lit_value);
1406-
}
1407-
}
14081403
}
14091404
}
14101405
format!("`{}`", evaluated)

compiler/rustc_typeck/src/check/coercion.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1696,13 +1696,12 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
16961696
}
16971697

16981698
fn is_return_ty_unsized<'a>(&self, fcx: &FnCtxt<'a, 'tcx>, blk_id: hir::HirId) -> bool {
1699-
if let Some((fn_decl, _)) = fcx.get_fn_decl(blk_id) {
1700-
if let hir::FnRetTy::Return(ty) = fn_decl.output {
1701-
let ty = <dyn AstConv<'_>>::ast_ty_to_ty(fcx, ty);
1702-
if let ty::Dynamic(..) = ty.kind() {
1699+
if let Some((fn_decl, _)) = fcx.get_fn_decl(blk_id)
1700+
&& let hir::FnRetTy::Return(ty) = fn_decl.output
1701+
&& let ty = <dyn AstConv<'_>>::ast_ty_to_ty(fcx, ty)
1702+
&& let ty::Dynamic(..) = ty.kind()
1703+
{
17031704
return true;
1704-
}
1705-
}
17061705
}
17071706
false
17081707
}

compiler/rustc_typeck/src/check/demand.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -587,9 +587,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
587587
match (&expr.kind, expected.kind(), checked_ty.kind()) {
588588
(_, &ty::Ref(_, exp, _), &ty::Ref(_, check, _)) => match (exp.kind(), check.kind()) {
589589
(&ty::Str, &ty::Array(arr, _) | &ty::Slice(arr)) if arr == self.tcx.types.u8 => {
590-
if let hir::ExprKind::Lit(_) = expr.kind {
591-
if let Ok(src) = sm.span_to_snippet(sp) {
592-
if replace_prefix(&src, "b\"", "\"").is_some() {
590+
if let hir::ExprKind::Lit(_) = expr.kind
591+
&& let Ok(src) = sm.span_to_snippet(sp)
592+
&& replace_prefix(&src, "b\"", "\"").is_some()
593+
{
593594
let pos = sp.lo() + BytePos(1);
594595
return Some((
595596
sp.with_hi(pos),
@@ -600,21 +601,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
600601
));
601602
}
602603
}
603-
}
604-
}
605604
(&ty::Array(arr, _) | &ty::Slice(arr), &ty::Str) if arr == self.tcx.types.u8 => {
606-
if let hir::ExprKind::Lit(_) = expr.kind {
607-
if let Ok(src) = sm.span_to_snippet(sp) {
608-
if replace_prefix(&src, "\"", "b\"").is_some() {
605+
if let hir::ExprKind::Lit(_) = expr.kind
606+
&& let Ok(src) = sm.span_to_snippet(sp)
607+
&& replace_prefix(&src, "\"", "b\"").is_some()
608+
{
609609
return Some((
610610
sp.shrink_to_lo(),
611611
"consider adding a leading `b`",
612612
"b".to_string(),
613613
Applicability::MachineApplicable,
614614
true,
615615
));
616-
}
617-
}
616+
618617
}
619618
}
620619
_ => {}

compiler/rustc_typeck/src/check/expr.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -810,10 +810,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
810810
// Use the span of the trailing expression for our cause,
811811
// not the span of the entire function
812812
if !explicit_return {
813-
if let ExprKind::Block(body, _) = return_expr.kind {
814-
if let Some(last_expr) = body.expr {
815-
span = last_expr.span;
816-
}
813+
if let ExprKind::Block(body, _) = return_expr.kind && let Some(last_expr) = body.expr {
814+
span = last_expr.span;
817815
}
818816
}
819817
ret_coercion.borrow_mut().coerce(

compiler/rustc_typeck/src/check/fn_ctxt/checks.rs

+16-18
Original file line numberDiff line numberDiff line change
@@ -402,25 +402,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
402402
if arg_count == 0 || i + 1 == arg_count { &label } else { "" },
403403
);
404404
}
405-
if let Some(def_id) = fn_def_id {
406-
if let Some(def_span) = tcx.def_ident_span(def_id) {
407-
let mut spans: MultiSpan = def_span.into();
408-
409-
let params = tcx
410-
.hir()
411-
.get_if_local(def_id)
412-
.and_then(|node| node.body_id())
413-
.into_iter()
414-
.map(|id| tcx.hir().body(id).params)
415-
.flatten();
416-
417-
for param in params {
418-
spans.push_span_label(param.span, String::new());
419-
}
420-
421-
let def_kind = tcx.def_kind(def_id);
422-
err.span_note(spans, &format!("{} defined here", def_kind.descr(def_id)));
405+
if let Some(def_id) = fn_def_id && let Some(def_span) = tcx.def_ident_span(def_id) {
406+
let mut spans: MultiSpan = def_span.into();
407+
408+
let params = tcx
409+
.hir()
410+
.get_if_local(def_id)
411+
.and_then(|node| node.body_id())
412+
.into_iter()
413+
.map(|id| tcx.hir().body(id).params)
414+
.flatten();
415+
416+
for param in params {
417+
spans.push_span_label(param.span, String::new());
423418
}
419+
420+
let def_kind = tcx.def_kind(def_id);
421+
err.span_note(spans, &format!("{} defined here", def_kind.descr(def_id)));
424422
}
425423
if sugg_unit {
426424
let sugg_span = tcx.sess.source_map().end_point(call_expr.span);

compiler/rustc_typeck/src/check/method/suggest.rs

+39-43
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
430430
actual.prefix_string(self.tcx),
431431
ty_str_reported,
432432
);
433-
if let Mode::MethodCall = mode {
434-
if let SelfSource::MethodCall(call) = source {
435-
self.suggest_await_before_method(
436-
&mut err, item_name, actual, call, span,
437-
);
438-
}
433+
if let Mode::MethodCall = mode && let SelfSource::MethodCall(cal) = source {
434+
self.suggest_await_before_method(
435+
&mut err, item_name, actual, cal, span,
436+
);
439437
}
440438
if let Some(span) =
441439
tcx.resolutions(()).confused_type_with_std_module.get(&span)
@@ -1525,43 +1523,41 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15251523
(self.tcx.mk_diagnostic_item(*rcvr_ty, sym::Arc), "Arc::new"),
15261524
(self.tcx.mk_diagnostic_item(*rcvr_ty, sym::Rc), "Rc::new"),
15271525
] {
1528-
if let Some(new_rcvr_t) = *rcvr_ty {
1529-
if let Ok(pick) = self.lookup_probe(
1530-
span,
1531-
item_name,
1532-
new_rcvr_t,
1533-
rcvr,
1534-
crate::check::method::probe::ProbeScope::AllTraits,
1535-
) {
1536-
debug!("try_alt_rcvr: pick candidate {:?}", pick);
1537-
let did = Some(pick.item.container.id());
1538-
// We don't want to suggest a container type when the missing
1539-
// method is `.clone()` or `.deref()` otherwise we'd suggest
1540-
// `Arc::new(foo).clone()`, which is far from what the user wants.
1541-
// Explicitly ignore the `Pin::as_ref()` method as `Pin` does not
1542-
// implement the `AsRef` trait.
1543-
let skip = skippable.contains(&did)
1544-
|| (("Pin::new" == *pre) && (sym::as_ref == item_name.name));
1545-
// Make sure the method is defined for the *actual* receiver: we don't
1546-
// want to treat `Box<Self>` as a receiver if it only works because of
1547-
// an autoderef to `&self`
1548-
if pick.autoderefs == 0 && !skip {
1549-
err.span_label(
1550-
pick.item.ident(self.tcx).span,
1551-
&format!("the method is available for `{}` here", new_rcvr_t),
1552-
);
1553-
err.multipart_suggestion(
1554-
"consider wrapping the receiver expression with the \
1555-
appropriate type",
1556-
vec![
1557-
(rcvr.span.shrink_to_lo(), format!("{}({}", pre, post)),
1558-
(rcvr.span.shrink_to_hi(), ")".to_string()),
1559-
],
1560-
Applicability::MaybeIncorrect,
1561-
);
1562-
// We don't care about the other suggestions.
1563-
alt_rcvr_sugg = true;
1564-
}
1526+
if let Some(new_rcvr_t) = *rcvr_ty && let Ok(pick) = self.lookup_probe(
1527+
span,
1528+
item_name,
1529+
new_rcvr_t,
1530+
rcvr,
1531+
crate::check::method::probe::ProbeScope::AllTraits,
1532+
) {
1533+
debug!("try_alt_rcvr: pick candidate {:?}", pick);
1534+
let did = Some(pick.item.container.id());
1535+
// We don't want to suggest a container type when the missing
1536+
// method is `.clone()` or `.deref()` otherwise we'd suggest
1537+
// `Arc::new(foo).clone()`, which is far from what the user wants.
1538+
// Explicitly ignore the `Pin::as_ref()` method as `Pin` does not
1539+
// implement the `AsRef` trait.
1540+
let skip = skippable.contains(&did)
1541+
|| (("Pin::new" == *pre) && (sym::as_ref == item_name.name));
1542+
// Make sure the method is defined for the *actual* receiver: we don't
1543+
// want to treat `Box<Self>` as a receiver if it only works because of
1544+
// an autoderef to `&self`
1545+
if pick.autoderefs == 0 && !skip {
1546+
err.span_label(
1547+
pick.item.ident(self.tcx).span,
1548+
&format!("the method is available for `{}` here", new_rcvr_t),
1549+
);
1550+
err.multipart_suggestion(
1551+
"consider wrapping the receiver expression with the \
1552+
appropriate type",
1553+
vec![
1554+
(rcvr.span.shrink_to_lo(), format!("{}({}", pre, post)),
1555+
(rcvr.span.shrink_to_hi(), ")".to_string()),
1556+
],
1557+
Applicability::MaybeIncorrect,
1558+
);
1559+
// We don't care about the other suggestions.
1560+
alt_rcvr_sugg = true;
15651561
}
15661562
}
15671563
}

compiler/rustc_typeck/src/check/pat.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -685,9 +685,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
685685
}
686686

687687
pub fn check_dereferenceable(&self, span: Span, expected: Ty<'tcx>, inner: &Pat<'_>) -> bool {
688-
if let PatKind::Binding(..) = inner.kind {
689-
if let Some(mt) = self.shallow_resolve(expected).builtin_deref(true) {
690-
if let ty::Dynamic(..) = mt.ty.kind() {
688+
if let PatKind::Binding(..) = inner.kind
689+
&& let Some(mt) = self.shallow_resolve(expected).builtin_deref(true)
690+
&& let ty::Dynamic(..) = mt.ty.kind()
691+
{
691692
// This is "x = SomeTrait" being reduced from
692693
// "let &x = &SomeTrait" or "let box x = Box<SomeTrait>", an error.
693694
let type_str = self.ty_to_string(expected);
@@ -705,8 +706,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
705706
err.emit();
706707
return false;
707708
}
708-
}
709-
}
710709
true
711710
}
712711

0 commit comments

Comments
 (0)