5 - Make more use of let_chains

This commit is contained in:
Caio 2022-02-28 15:52:36 -03:00
parent 48132caac2
commit d956c8b816
5 changed files with 63 additions and 68 deletions

View File

@ -1113,22 +1113,20 @@ impl CheckAttrVisitor<'_> {
/// Warns against some misuses of `#[must_use]` /// Warns against some misuses of `#[must_use]`
fn check_must_use(&self, hir_id: HirId, attr: &Attribute, span: Span, _target: Target) -> bool { fn check_must_use(&self, hir_id: HirId, attr: &Attribute, span: Span, _target: Target) -> bool {
let node = self.tcx.hir().get(hir_id); let node = self.tcx.hir().get(hir_id);
if let Some(fn_node) = node.fn_kind() { if let Some(kind) = node.fn_kind() && let rustc_hir::IsAsync::Async = kind.asyncness() {
if let rustc_hir::IsAsync::Async = fn_node.asyncness() { self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| { lint.build(
lint.build( "`must_use` attribute on `async` functions \
"`must_use` attribute on `async` functions \ applies to the anonymous `Future` returned by the \
applies to the anonymous `Future` returned by the \ function, not the value within",
function, not the value within", )
) .span_label(
.span_label( span,
span, "this attribute does nothing, the `Future`s \
"this attribute does nothing, the `Future`s \ returned by async functions are already `must_use`",
returned by async functions are already `must_use`", )
) .emit();
.emit(); });
});
}
} }
// For now, its always valid // For now, its always valid

View File

@ -683,34 +683,33 @@ impl<'tcx> DeadVisitor<'tcx> {
let descr = self.tcx.def_kind(def_id).descr(def_id.to_def_id()); 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 mut err = lint.build(&format!("{} is never {}: `{}`", descr, participle, name));
let hir = self.tcx.hir(); let hir = self.tcx.hir();
if let Some(encl_scope) = hir.get_enclosing_scope(id) { if let Some(encl_scope) = hir.get_enclosing_scope(id)
if let Some(encl_def_id) = hir.opt_local_def_id(encl_scope) { && 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 Some(ign_traits) = self.ignored_derived_traits.get(&encl_def_id)
let traits_str = ign_traits {
.iter() let traits_str = ign_traits
.map(|(trait_id, _)| format!("`{}`", self.tcx.item_name(*trait_id))) .iter()
.collect::<Vec<_>>() .map(|(trait_id, _)| format!("`{}`", self.tcx.item_name(*trait_id)))
.join(" and "); .collect::<Vec<_>>()
let plural_s = pluralize!(ign_traits.len()); .join(" and ");
let article = if ign_traits.len() > 1 { "" } else { "a " }; let plural_s = pluralize!(ign_traits.len());
let is_are = if ign_traits.len() > 1 { "these are" } else { "this is" }; let article = if ign_traits.len() > 1 { "" } else { "a " };
let msg = format!( let is_are = if ign_traits.len() > 1 { "these are" } else { "this is" };
"`{}` has {}derived impl{} for the trait{} {}, but {} \ let msg = format!(
intentionally ignored during dead code analysis", "`{}` has {}derived impl{} for the trait{} {}, but {} \
self.tcx.item_name(encl_def_id.to_def_id()), intentionally ignored during dead code analysis",
article, self.tcx.item_name(encl_def_id.to_def_id()),
plural_s, article,
plural_s, plural_s,
traits_str, plural_s,
is_are traits_str,
); is_are
let multispan = ign_traits );
.iter() let multispan = ign_traits
.map(|(_, impl_id)| self.tcx.def_span(*impl_id)) .iter()
.collect::<Vec<_>>(); .map(|(_, impl_id)| self.tcx.def_span(*impl_id))
err.span_note(multispan, &msg); .collect::<Vec<_>>();
} err.span_note(multispan, &msg);
}
} }
err.emit(); err.emit();
}); });

View File

@ -4,16 +4,17 @@
//! //!
//! This API is completely unstable and subject to change. //! 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/")] #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![feature(crate_visibility_modifier)] #![feature(crate_visibility_modifier)]
#![feature(iter_intersperse)] #![feature(iter_intersperse)]
#![feature(let_else)] #![feature(let_else)]
#![feature(let_chains)]
#![feature(map_try_insert)] #![feature(map_try_insert)]
#![feature(min_specialization)] #![feature(min_specialization)]
#![feature(nll)] #![feature(nll)]
#![feature(try_blocks)] #![feature(try_blocks)]
#![recursion_limit = "256"] #![recursion_limit = "256"]
#![allow(rustc::potential_query_instability)]
#[macro_use] #[macro_use]
extern crate rustc_middle; extern crate rustc_middle;

View File

@ -332,12 +332,11 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
let def_id = local_def_id.to_def_id(); let def_id = local_def_id.to_def_id();
// Don't run unused pass for #[derive()] // Don't run unused pass for #[derive()]
if let Some(parent) = self.tcx.parent(def_id) { if let Some(parent) = self.tcx.parent(def_id)
if let DefKind::Impl = self.tcx.def_kind(parent.expect_local()) { && let DefKind::Impl = self.tcx.def_kind(parent.expect_local())
if self.tcx.has_attr(parent, sym::automatically_derived) { && self.tcx.has_attr(parent, sym::automatically_derived)
return; {
} return;
}
} }
// Don't run unused pass for #[naked] // Don't run unused pass for #[naked]

View File

@ -94,24 +94,22 @@ impl<'tcx> Visitor<'tcx> for ReachableContext<'tcx> {
_ => None, _ => None,
}; };
if let Some(res) = res { if let Some(res) = res && let Some(def_id) = res.opt_def_id().and_then(|el| el.as_local()) {
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()) {
if self.def_id_represents_local_inlined_item(def_id.to_def_id()) { self.worklist.push(def_id);
self.worklist.push(def_id); } else {
} else { match res {
match res { // If this path leads to a constant, then we need to
// If this path leads to a constant, then we need to // recurse into the constant to continue finding
// recurse into the constant to continue finding // items that are reachable.
// items that are reachable. Res::Def(DefKind::Const | DefKind::AssocConst, _) => {
Res::Def(DefKind::Const | DefKind::AssocConst, _) => { self.worklist.push(def_id);
self.worklist.push(def_id); }
}
// If this wasn't a static, then the destination is // If this wasn't a static, then the destination is
// surely reachable. // surely reachable.
_ => { _ => {
self.reachable_symbols.insert(def_id); self.reachable_symbols.insert(def_id);
}
} }
} }
} }