Skip to content

Commit 2f0f308

Browse files
committed
Refactor notable_trait_buttons to use iterator combinators instead of for loop
1 parent 80c0919 commit 2f0f308

File tree

1 file changed

+13
-23
lines changed
  • src/librustdoc/html/render

1 file changed

+13
-23
lines changed

src/librustdoc/html/render/mod.rs

+13-23
Original file line numberDiff line numberDiff line change
@@ -1432,8 +1432,6 @@ fn should_render_item(item: &clean::Item, deref_mut_: bool, tcx: TyCtxt<'_>) ->
14321432
}
14331433

14341434
pub(crate) fn notable_traits_button(ty: &clean::Type, cx: &Context<'_>) -> Option<String> {
1435-
let mut has_notable_trait = false;
1436-
14371435
if ty.is_unit() {
14381436
// Very common fast path.
14391437
return None;
@@ -1451,27 +1449,19 @@ pub(crate) fn notable_traits_button(ty: &clean::Type, cx: &Context<'_>) -> Optio
14511449
return None;
14521450
}
14531451

1454-
if let Some(impls) = cx.cache().impls.get(&did) {
1455-
for i in impls {
1456-
let impl_ = i.inner_impl();
1457-
if impl_.polarity != ty::ImplPolarity::Positive {
1458-
continue;
1459-
}
1460-
1461-
if !ty.is_doc_subtype_of(&impl_.for_, cx.cache()) {
1462-
// Two different types might have the same did,
1463-
// without actually being the same.
1464-
continue;
1465-
}
1466-
if let Some(trait_) = &impl_.trait_ {
1467-
let trait_did = trait_.def_id();
1468-
1469-
if cx.cache().traits.get(&trait_did).is_some_and(|t| t.is_notable_trait(cx.tcx())) {
1470-
has_notable_trait = true;
1471-
}
1472-
}
1473-
}
1474-
}
1452+
let impls = cx.cache().impls.get(&did)?;
1453+
let has_notable_trait = impls
1454+
.iter()
1455+
.map(Impl::inner_impl)
1456+
.filter(|impl_| impl_.polarity == ty::ImplPolarity::Positive)
1457+
.filter(|impl_| {
1458+
// Two different types might have the same did,
1459+
// without actually being the same.
1460+
ty.is_doc_subtype_of(&impl_.for_, cx.cache())
1461+
})
1462+
.filter_map(|impl_| impl_.trait_.as_ref())
1463+
.filter_map(|trait_| cx.cache().traits.get(&trait_.def_id()))
1464+
.any(|t| t.is_notable_trait(cx.tcx()));
14751465

14761466
if has_notable_trait {
14771467
cx.types_with_notable_traits.borrow_mut().insert(ty.clone());

0 commit comments

Comments
 (0)