diff options
| author | Yotam Ofek <yotam.ofek@gmail.com> | 2025-03-06 07:59:11 +0000 |
|---|---|---|
| committer | Yotam Ofek <yotam.ofek@gmail.com> | 2025-03-06 08:16:28 +0000 |
| commit | fde37335aab7df1f9fcdfb0f5079ba76e56d7fe1 (patch) | |
| tree | 1307fcc46e9f46914e7eed02451f9d82640498c5 /src/librustdoc/clean | |
| parent | 4559163ccb500affc424fb9228dae5003672ffc7 (diff) | |
| download | rust-fde37335aab7df1f9fcdfb0f5079ba76e56d7fe1.tar.gz rust-fde37335aab7df1f9fcdfb0f5079ba76e56d7fe1.zip | |
`librustdoc`: flatten nested ifs
Diffstat (limited to 'src/librustdoc/clean')
| -rw-r--r-- | src/librustdoc/clean/inline.rs | 10 | ||||
| -rw-r--r-- | src/librustdoc/clean/mod.rs | 112 | ||||
| -rw-r--r-- | src/librustdoc/clean/types.rs | 36 |
3 files changed, 74 insertions, 84 deletions
diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index e10a74221ae..8c6ea00d489 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -563,11 +563,13 @@ pub(crate) fn build_impl( // Return if the trait itself or any types of the generic parameters are doc(hidden). let mut stack: Vec<&Type> = vec![&for_]; - if let Some(did) = trait_.as_ref().map(|t| t.def_id()) { - if !document_hidden && tcx.is_doc_hidden(did) { - return; - } + if let Some(did) = trait_.as_ref().map(|t| t.def_id()) + && !document_hidden + && tcx.is_doc_hidden(did) + { + return; } + if let Some(generics) = trait_.as_ref().and_then(|t| t.generics()) { stack.extend(generics); } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index bb12e4a706e..fd528786d03 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -828,30 +828,26 @@ fn clean_ty_generics<'tcx>( .iter() .flat_map(|(pred, _)| { let mut projection = None; - let param_idx = (|| { + let param_idx = { let bound_p = pred.kind(); match bound_p.skip_binder() { - ty::ClauseKind::Trait(pred) => { - if let ty::Param(param) = pred.self_ty().kind() { - return Some(param.index); - } + ty::ClauseKind::Trait(pred) if let ty::Param(param) = pred.self_ty().kind() => { + Some(param.index) } - ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(ty, _reg)) => { - if let ty::Param(param) = ty.kind() { - return Some(param.index); - } + ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(ty, _reg)) + if let ty::Param(param) = ty.kind() => + { + Some(param.index) } - ty::ClauseKind::Projection(p) => { - if let ty::Param(param) = p.projection_term.self_ty().kind() { - projection = Some(bound_p.rebind(p)); - return Some(param.index); - } + ty::ClauseKind::Projection(p) + if let ty::Param(param) = p.projection_term.self_ty().kind() => + { + projection = Some(bound_p.rebind(p)); + Some(param.index) } - _ => (), + _ => None, } - - None - })(); + }; if let Some(param_idx) = param_idx && let Some(bounds) = impl_trait.get_mut(¶m_idx) @@ -1378,12 +1374,12 @@ pub(crate) fn clean_middle_assoc_item(assoc_item: &ty::AssocItem, cx: &mut DocCo tcx.fn_sig(assoc_item.def_id).instantiate_identity().input(0).skip_binder(); if self_arg_ty == self_ty { item.decl.inputs.values[0].type_ = SelfTy; - } else if let ty::Ref(_, ty, _) = *self_arg_ty.kind() { - if ty == self_ty { - match item.decl.inputs.values[0].type_ { - BorrowedRef { ref mut type_, .. } => **type_ = SelfTy, - _ => unreachable!(), - } + } else if let ty::Ref(_, ty, _) = *self_arg_ty.kind() + && ty == self_ty + { + match item.decl.inputs.values[0].type_ { + BorrowedRef { ref mut type_, .. } => **type_ = SelfTy, + _ => unreachable!(), } } } @@ -2331,25 +2327,22 @@ fn clean_middle_opaque_bounds<'tcx>( let bindings: ThinVec<_> = bounds .iter() .filter_map(|(bound, _)| { - if let ty::ClauseKind::Projection(proj) = bound.kind().skip_binder() { - if proj.projection_term.trait_ref(cx.tcx) == trait_ref.skip_binder() { - Some(AssocItemConstraint { - assoc: projection_to_path_segment( - // FIXME: This needs to be made resilient for `AliasTerm`s that - // are associated consts. - bound.kind().rebind(proj.projection_term.expect_ty(cx.tcx)), - cx, - ), - kind: AssocItemConstraintKind::Equality { - term: clean_middle_term(bound.kind().rebind(proj.term), cx), - }, - }) - } else { - None - } - } else { - None + if let ty::ClauseKind::Projection(proj) = bound.kind().skip_binder() + && proj.projection_term.trait_ref(cx.tcx) == trait_ref.skip_binder() + { + return Some(AssocItemConstraint { + assoc: projection_to_path_segment( + // FIXME: This needs to be made resilient for `AliasTerm`s that + // are associated consts. + bound.kind().rebind(proj.projection_term.expect_ty(cx.tcx)), + cx, + ), + kind: AssocItemConstraintKind::Equality { + term: clean_middle_term(bound.kind().rebind(proj.term), cx), + }, + }); } + None }) .collect(); @@ -2743,23 +2736,20 @@ fn add_without_unwanted_attributes<'hir>( } let mut attr = attr.clone(); match attr { - hir::Attribute::Unparsed(ref mut normal) => { - if let [ident] = &*normal.path.segments { - let ident = ident.name; - if ident == sym::doc { - filter_doc_attr(&mut normal.args, is_inline); - attrs.push((Cow::Owned(attr), import_parent)); - } else if is_inline || ident != sym::cfg { - // If it's not a `cfg()` attribute, we keep it. - attrs.push((Cow::Owned(attr), import_parent)); - } - } - } - hir::Attribute::Parsed(..) => { - if is_inline { + hir::Attribute::Unparsed(ref mut normal) if let [ident] = &*normal.path.segments => { + let ident = ident.name; + if ident == sym::doc { + filter_doc_attr(&mut normal.args, is_inline); + attrs.push((Cow::Owned(attr), import_parent)); + } else if is_inline || ident != sym::cfg { + // If it's not a `cfg()` attribute, we keep it. attrs.push((Cow::Owned(attr), import_parent)); } } + hir::Attribute::Parsed(..) if is_inline => { + attrs.push((Cow::Owned(attr), import_parent)); + } + _ => {} } } } @@ -2961,16 +2951,16 @@ fn clean_extern_crate<'tcx>( && !cx.is_json_output(); let krate_owner_def_id = krate.owner_id.def_id; - if please_inline { - if let Some(items) = inline::try_inline( + if please_inline + && let Some(items) = inline::try_inline( cx, Res::Def(DefKind::Mod, crate_def_id), name, Some((attrs, Some(krate_owner_def_id))), &mut Default::default(), - ) { - return items; - } + ) + { + return items; } vec![Item::from_def_id_and_parts( diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 5906a720e0f..0d33c234f93 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -208,11 +208,11 @@ impl ExternalCrate { .get_attrs(def_id, sym::doc) .flat_map(|attr| attr.meta_item_list().unwrap_or_default()); for meta in meta_items { - if meta.has_name(sym::keyword) { - if let Some(v) = meta.value_str() { - keyword = Some(v); - break; - } + if meta.has_name(sym::keyword) + && let Some(v) = meta.value_str() + { + keyword = Some(v); + break; } } return keyword.map(|p| (def_id, p)); @@ -1071,16 +1071,14 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute> // treat #[target_feature(enable = "feat")] attributes as if they were // #[doc(cfg(target_feature = "feat"))] attributes as well for attr in hir_attr_lists(attrs, sym::target_feature) { - if attr.has_name(sym::enable) { - if attr.value_str().is_some() { - // Clone `enable = "feat"`, change to `target_feature = "feat"`. - // Unwrap is safe because `value_str` succeeded above. - let mut meta = attr.meta_item().unwrap().clone(); - meta.path = ast::Path::from_ident(Ident::with_dummy_span(sym::target_feature)); - - if let Ok(feat_cfg) = Cfg::parse(&ast::MetaItemInner::MetaItem(meta)) { - cfg &= feat_cfg; - } + if attr.has_name(sym::enable) && attr.value_str().is_some() { + // Clone `enable = "feat"`, change to `target_feature = "feat"`. + // Unwrap is safe because `value_str` succeeded above. + let mut meta = attr.meta_item().unwrap().clone(); + meta.path = ast::Path::from_ident(Ident::with_dummy_span(sym::target_feature)); + + if let Ok(feat_cfg) = Cfg::parse(&ast::MetaItemInner::MetaItem(meta)) { + cfg &= feat_cfg; } } } @@ -1160,10 +1158,10 @@ impl Attributes { continue; } - if let Some(items) = attr.meta_item_list() { - if items.iter().filter_map(|i| i.meta_item()).any(|it| it.has_name(flag)) { - return true; - } + if let Some(items) = attr.meta_item_list() + && items.iter().filter_map(|i| i.meta_item()).any(|it| it.has_name(flag)) + { + return true; } } |
