Skip to content

Commit d956c8b

Browse files
committed
5 - Make more use of let_chains
1 parent 48132ca commit d956c8b

File tree

5 files changed

+63
-68
lines changed

5 files changed

+63
-68
lines changed

Diff for: compiler/rustc_passes/src/check_attr.rs

+14-16
Original file line numberDiff line numberDiff line change
@@ -1113,22 +1113,20 @@ impl CheckAttrVisitor<'_> {
11131113
/// Warns against some misuses of `#[must_use]`
11141114
fn check_must_use(&self, hir_id: HirId, attr: &Attribute, span: Span, _target: Target) -> bool {
11151115
let node = self.tcx.hir().get(hir_id);
1116-
if let Some(fn_node) = node.fn_kind() {
1117-
if let rustc_hir::IsAsync::Async = fn_node.asyncness() {
1118-
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
1119-
lint.build(
1120-
"`must_use` attribute on `async` functions \
1121-
applies to the anonymous `Future` returned by the \
1122-
function, not the value within",
1123-
)
1124-
.span_label(
1125-
span,
1126-
"this attribute does nothing, the `Future`s \
1127-
returned by async functions are already `must_use`",
1128-
)
1129-
.emit();
1130-
});
1131-
}
1116+
if let Some(kind) = node.fn_kind() && let rustc_hir::IsAsync::Async = kind.asyncness() {
1117+
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
1118+
lint.build(
1119+
"`must_use` attribute on `async` functions \
1120+
applies to the anonymous `Future` returned by the \
1121+
function, not the value within",
1122+
)
1123+
.span_label(
1124+
span,
1125+
"this attribute does nothing, the `Future`s \
1126+
returned by async functions are already `must_use`",
1127+
)
1128+
.emit();
1129+
});
11321130
}
11331131

11341132
// For now, its always valid

Diff for: compiler/rustc_passes/src/dead.rs

+27-28
Original file line numberDiff line numberDiff line change
@@ -683,34 +683,33 @@ impl<'tcx> DeadVisitor<'tcx> {
683683
let descr = self.tcx.def_kind(def_id).descr(def_id.to_def_id());
684684
let mut err = lint.build(&format!("{} is never {}: `{}`", descr, participle, name));
685685
let hir = self.tcx.hir();
686-
if let Some(encl_scope) = hir.get_enclosing_scope(id) {
687-
if let Some(encl_def_id) = hir.opt_local_def_id(encl_scope) {
688-
if let Some(ign_traits) = self.ignored_derived_traits.get(&encl_def_id) {
689-
let traits_str = ign_traits
690-
.iter()
691-
.map(|(trait_id, _)| format!("`{}`", self.tcx.item_name(*trait_id)))
692-
.collect::<Vec<_>>()
693-
.join(" and ");
694-
let plural_s = pluralize!(ign_traits.len());
695-
let article = if ign_traits.len() > 1 { "" } else { "a " };
696-
let is_are = if ign_traits.len() > 1 { "these are" } else { "this is" };
697-
let msg = format!(
698-
"`{}` has {}derived impl{} for the trait{} {}, but {} \
699-
intentionally ignored during dead code analysis",
700-
self.tcx.item_name(encl_def_id.to_def_id()),
701-
article,
702-
plural_s,
703-
plural_s,
704-
traits_str,
705-
is_are
706-
);
707-
let multispan = ign_traits
708-
.iter()
709-
.map(|(_, impl_id)| self.tcx.def_span(*impl_id))
710-
.collect::<Vec<_>>();
711-
err.span_note(multispan, &msg);
712-
}
713-
}
686+
if let Some(encl_scope) = hir.get_enclosing_scope(id)
687+
&& let Some(encl_def_id) = hir.opt_local_def_id(encl_scope)
688+
&& let Some(ign_traits) = self.ignored_derived_traits.get(&encl_def_id)
689+
{
690+
let traits_str = ign_traits
691+
.iter()
692+
.map(|(trait_id, _)| format!("`{}`", self.tcx.item_name(*trait_id)))
693+
.collect::<Vec<_>>()
694+
.join(" and ");
695+
let plural_s = pluralize!(ign_traits.len());
696+
let article = if ign_traits.len() > 1 { "" } else { "a " };
697+
let is_are = if ign_traits.len() > 1 { "these are" } else { "this is" };
698+
let msg = format!(
699+
"`{}` has {}derived impl{} for the trait{} {}, but {} \
700+
intentionally ignored during dead code analysis",
701+
self.tcx.item_name(encl_def_id.to_def_id()),
702+
article,
703+
plural_s,
704+
plural_s,
705+
traits_str,
706+
is_are
707+
);
708+
let multispan = ign_traits
709+
.iter()
710+
.map(|(_, impl_id)| self.tcx.def_span(*impl_id))
711+
.collect::<Vec<_>>();
712+
err.span_note(multispan, &msg);
714713
}
715714
err.emit();
716715
});

Diff for: compiler/rustc_passes/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
//!
55
//! This API is completely unstable and subject to change.
66
7+
#![allow(rustc::potential_query_instability)]
78
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
89
#![feature(crate_visibility_modifier)]
910
#![feature(iter_intersperse)]
1011
#![feature(let_else)]
12+
#![feature(let_chains)]
1113
#![feature(map_try_insert)]
1214
#![feature(min_specialization)]
1315
#![feature(nll)]
1416
#![feature(try_blocks)]
1517
#![recursion_limit = "256"]
16-
#![allow(rustc::potential_query_instability)]
1718

1819
#[macro_use]
1920
extern crate rustc_middle;

Diff for: compiler/rustc_passes/src/liveness.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,11 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
332332
let def_id = local_def_id.to_def_id();
333333

334334
// Don't run unused pass for #[derive()]
335-
if let Some(parent) = self.tcx.parent(def_id) {
336-
if let DefKind::Impl = self.tcx.def_kind(parent.expect_local()) {
337-
if self.tcx.has_attr(parent, sym::automatically_derived) {
338-
return;
339-
}
340-
}
335+
if let Some(parent) = self.tcx.parent(def_id)
336+
&& let DefKind::Impl = self.tcx.def_kind(parent.expect_local())
337+
&& self.tcx.has_attr(parent, sym::automatically_derived)
338+
{
339+
return;
341340
}
342341

343342
// Don't run unused pass for #[naked]

Diff for: compiler/rustc_passes/src/reachable.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -94,24 +94,22 @@ impl<'tcx> Visitor<'tcx> for ReachableContext<'tcx> {
9494
_ => None,
9595
};
9696

97-
if let Some(res) = res {
98-
if let Some(def_id) = res.opt_def_id().and_then(|def_id| def_id.as_local()) {
99-
if self.def_id_represents_local_inlined_item(def_id.to_def_id()) {
100-
self.worklist.push(def_id);
101-
} else {
102-
match res {
103-
// If this path leads to a constant, then we need to
104-
// recurse into the constant to continue finding
105-
// items that are reachable.
106-
Res::Def(DefKind::Const | DefKind::AssocConst, _) => {
107-
self.worklist.push(def_id);
108-
}
97+
if let Some(res) = res && let Some(def_id) = res.opt_def_id().and_then(|el| el.as_local()) {
98+
if self.def_id_represents_local_inlined_item(def_id.to_def_id()) {
99+
self.worklist.push(def_id);
100+
} else {
101+
match res {
102+
// If this path leads to a constant, then we need to
103+
// recurse into the constant to continue finding
104+
// items that are reachable.
105+
Res::Def(DefKind::Const | DefKind::AssocConst, _) => {
106+
self.worklist.push(def_id);
107+
}
109108

110-
// If this wasn't a static, then the destination is
111-
// surely reachable.
112-
_ => {
113-
self.reachable_symbols.insert(def_id);
114-
}
109+
// If this wasn't a static, then the destination is
110+
// surely reachable.
111+
_ => {
112+
self.reachable_symbols.insert(def_id);
115113
}
116114
}
117115
}

0 commit comments

Comments
 (0)