From d956c8b816c1bcc38053d6dfd79e34cd12b0f4b1 Mon Sep 17 00:00:00 2001 From: Caio Date: Mon, 28 Feb 2022 15:52:36 -0300 Subject: [PATCH] 5 - Make more use of let_chains --- compiler/rustc_passes/src/check_attr.rs | 30 +++++++------- compiler/rustc_passes/src/dead.rs | 55 ++++++++++++------------- compiler/rustc_passes/src/lib.rs | 3 +- compiler/rustc_passes/src/liveness.rs | 11 +++-- compiler/rustc_passes/src/reachable.rs | 32 +++++++------- 5 files changed, 63 insertions(+), 68 deletions(-) diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 6e4907fe518ae..6c296b5909277 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -1113,22 +1113,20 @@ impl CheckAttrVisitor<'_> { /// Warns against some misuses of `#[must_use]` fn check_must_use(&self, hir_id: HirId, attr: &Attribute, span: Span, _target: Target) -> bool { let node = self.tcx.hir().get(hir_id); - if let Some(fn_node) = node.fn_kind() { - if let rustc_hir::IsAsync::Async = fn_node.asyncness() { - self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| { - lint.build( - "`must_use` attribute on `async` functions \ - applies to the anonymous `Future` returned by the \ - function, not the value within", - ) - .span_label( - span, - "this attribute does nothing, the `Future`s \ - returned by async functions are already `must_use`", - ) - .emit(); - }); - } + if let Some(kind) = node.fn_kind() && let rustc_hir::IsAsync::Async = kind.asyncness() { + self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| { + lint.build( + "`must_use` attribute on `async` functions \ + applies to the anonymous `Future` returned by the \ + function, not the value within", + ) + .span_label( + span, + "this attribute does nothing, the `Future`s \ + returned by async functions are already `must_use`", + ) + .emit(); + }); } // For now, its always valid diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index e52fbc8ab92d6..e438b521a952b 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -683,34 +683,33 @@ impl<'tcx> DeadVisitor<'tcx> { let descr = self.tcx.def_kind(def_id).descr(def_id.to_def_id()); let mut err = lint.build(&format!("{} is never {}: `{}`", descr, participle, name)); let hir = self.tcx.hir(); - if let Some(encl_scope) = hir.get_enclosing_scope(id) { - if let Some(encl_def_id) = hir.opt_local_def_id(encl_scope) { - if let Some(ign_traits) = self.ignored_derived_traits.get(&encl_def_id) { - let traits_str = ign_traits - .iter() - .map(|(trait_id, _)| format!("`{}`", self.tcx.item_name(*trait_id))) - .collect::>() - .join(" and "); - let plural_s = pluralize!(ign_traits.len()); - let article = if ign_traits.len() > 1 { "" } else { "a " }; - let is_are = if ign_traits.len() > 1 { "these are" } else { "this is" }; - let msg = format!( - "`{}` has {}derived impl{} for the trait{} {}, but {} \ - intentionally ignored during dead code analysis", - self.tcx.item_name(encl_def_id.to_def_id()), - article, - plural_s, - plural_s, - traits_str, - is_are - ); - let multispan = ign_traits - .iter() - .map(|(_, impl_id)| self.tcx.def_span(*impl_id)) - .collect::>(); - err.span_note(multispan, &msg); - } - } + if let Some(encl_scope) = hir.get_enclosing_scope(id) + && let Some(encl_def_id) = hir.opt_local_def_id(encl_scope) + && let Some(ign_traits) = self.ignored_derived_traits.get(&encl_def_id) + { + let traits_str = ign_traits + .iter() + .map(|(trait_id, _)| format!("`{}`", self.tcx.item_name(*trait_id))) + .collect::>() + .join(" and "); + let plural_s = pluralize!(ign_traits.len()); + let article = if ign_traits.len() > 1 { "" } else { "a " }; + let is_are = if ign_traits.len() > 1 { "these are" } else { "this is" }; + let msg = format!( + "`{}` has {}derived impl{} for the trait{} {}, but {} \ + intentionally ignored during dead code analysis", + self.tcx.item_name(encl_def_id.to_def_id()), + article, + plural_s, + plural_s, + traits_str, + is_are + ); + let multispan = ign_traits + .iter() + .map(|(_, impl_id)| self.tcx.def_span(*impl_id)) + .collect::>(); + err.span_note(multispan, &msg); } err.emit(); }); diff --git a/compiler/rustc_passes/src/lib.rs b/compiler/rustc_passes/src/lib.rs index 71381f1d86931..71d49d8b7ea9c 100644 --- a/compiler/rustc_passes/src/lib.rs +++ b/compiler/rustc_passes/src/lib.rs @@ -4,16 +4,17 @@ //! //! This API is completely unstable and subject to change. +#![allow(rustc::potential_query_instability)] #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")] #![feature(crate_visibility_modifier)] #![feature(iter_intersperse)] #![feature(let_else)] +#![feature(let_chains)] #![feature(map_try_insert)] #![feature(min_specialization)] #![feature(nll)] #![feature(try_blocks)] #![recursion_limit = "256"] -#![allow(rustc::potential_query_instability)] #[macro_use] extern crate rustc_middle; diff --git a/compiler/rustc_passes/src/liveness.rs b/compiler/rustc_passes/src/liveness.rs index a959089ebb318..ea99a90e937c3 100644 --- a/compiler/rustc_passes/src/liveness.rs +++ b/compiler/rustc_passes/src/liveness.rs @@ -332,12 +332,11 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> { let def_id = local_def_id.to_def_id(); // Don't run unused pass for #[derive()] - if let Some(parent) = self.tcx.parent(def_id) { - if let DefKind::Impl = self.tcx.def_kind(parent.expect_local()) { - if self.tcx.has_attr(parent, sym::automatically_derived) { - return; - } - } + if let Some(parent) = self.tcx.parent(def_id) + && let DefKind::Impl = self.tcx.def_kind(parent.expect_local()) + && self.tcx.has_attr(parent, sym::automatically_derived) + { + return; } // Don't run unused pass for #[naked] diff --git a/compiler/rustc_passes/src/reachable.rs b/compiler/rustc_passes/src/reachable.rs index adbfb4fcf01ca..b520e5d04eab9 100644 --- a/compiler/rustc_passes/src/reachable.rs +++ b/compiler/rustc_passes/src/reachable.rs @@ -94,24 +94,22 @@ impl<'tcx> Visitor<'tcx> for ReachableContext<'tcx> { _ => None, }; - if let Some(res) = res { - if let Some(def_id) = res.opt_def_id().and_then(|def_id| def_id.as_local()) { - if self.def_id_represents_local_inlined_item(def_id.to_def_id()) { - self.worklist.push(def_id); - } else { - match res { - // If this path leads to a constant, then we need to - // recurse into the constant to continue finding - // items that are reachable. - Res::Def(DefKind::Const | DefKind::AssocConst, _) => { - self.worklist.push(def_id); - } + if let Some(res) = res && let Some(def_id) = res.opt_def_id().and_then(|el| el.as_local()) { + if self.def_id_represents_local_inlined_item(def_id.to_def_id()) { + self.worklist.push(def_id); + } else { + match res { + // If this path leads to a constant, then we need to + // recurse into the constant to continue finding + // items that are reachable. + Res::Def(DefKind::Const | DefKind::AssocConst, _) => { + self.worklist.push(def_id); + } - // If this wasn't a static, then the destination is - // surely reachable. - _ => { - self.reachable_symbols.insert(def_id); - } + // If this wasn't a static, then the destination is + // surely reachable. + _ => { + self.reachable_symbols.insert(def_id); } } }