Skip to content

Commit 184c2b0

Browse files
Rollup merge of #138090 - yotamofek:pr/rustdoc/flatten-ifs, r=GuillaumeGomez
`librustdoc`: flatten nested ifs Some minor cosmetic improvements (IMHO) found while working on something else. But reviewed with [no whitespace](https://github.com/rust-lang/rust/pull/138090/files?diff=unified&w=1).
2 parents bc45cdb + fde3733 commit 184c2b0

14 files changed

+203
-220
lines changed

Diff for: src/librustdoc/clean/inline.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -563,11 +563,13 @@ pub(crate) fn build_impl(
563563
// Return if the trait itself or any types of the generic parameters are doc(hidden).
564564
let mut stack: Vec<&Type> = vec![&for_];
565565

566-
if let Some(did) = trait_.as_ref().map(|t| t.def_id()) {
567-
if !document_hidden && tcx.is_doc_hidden(did) {
568-
return;
569-
}
566+
if let Some(did) = trait_.as_ref().map(|t| t.def_id())
567+
&& !document_hidden
568+
&& tcx.is_doc_hidden(did)
569+
{
570+
return;
570571
}
572+
571573
if let Some(generics) = trait_.as_ref().and_then(|t| t.generics()) {
572574
stack.extend(generics);
573575
}

Diff for: src/librustdoc/clean/mod.rs

+51-61
Original file line numberDiff line numberDiff line change
@@ -828,30 +828,26 @@ fn clean_ty_generics<'tcx>(
828828
.iter()
829829
.flat_map(|(pred, _)| {
830830
let mut projection = None;
831-
let param_idx = (|| {
831+
let param_idx = {
832832
let bound_p = pred.kind();
833833
match bound_p.skip_binder() {
834-
ty::ClauseKind::Trait(pred) => {
835-
if let ty::Param(param) = pred.self_ty().kind() {
836-
return Some(param.index);
837-
}
834+
ty::ClauseKind::Trait(pred) if let ty::Param(param) = pred.self_ty().kind() => {
835+
Some(param.index)
838836
}
839-
ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(ty, _reg)) => {
840-
if let ty::Param(param) = ty.kind() {
841-
return Some(param.index);
842-
}
837+
ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(ty, _reg))
838+
if let ty::Param(param) = ty.kind() =>
839+
{
840+
Some(param.index)
843841
}
844-
ty::ClauseKind::Projection(p) => {
845-
if let ty::Param(param) = p.projection_term.self_ty().kind() {
846-
projection = Some(bound_p.rebind(p));
847-
return Some(param.index);
848-
}
842+
ty::ClauseKind::Projection(p)
843+
if let ty::Param(param) = p.projection_term.self_ty().kind() =>
844+
{
845+
projection = Some(bound_p.rebind(p));
846+
Some(param.index)
849847
}
850-
_ => (),
848+
_ => None,
851849
}
852-
853-
None
854-
})();
850+
};
855851

856852
if let Some(param_idx) = param_idx
857853
&& let Some(bounds) = impl_trait.get_mut(&param_idx)
@@ -1378,12 +1374,12 @@ pub(crate) fn clean_middle_assoc_item(assoc_item: &ty::AssocItem, cx: &mut DocCo
13781374
tcx.fn_sig(assoc_item.def_id).instantiate_identity().input(0).skip_binder();
13791375
if self_arg_ty == self_ty {
13801376
item.decl.inputs.values[0].type_ = SelfTy;
1381-
} else if let ty::Ref(_, ty, _) = *self_arg_ty.kind() {
1382-
if ty == self_ty {
1383-
match item.decl.inputs.values[0].type_ {
1384-
BorrowedRef { ref mut type_, .. } => **type_ = SelfTy,
1385-
_ => unreachable!(),
1386-
}
1377+
} else if let ty::Ref(_, ty, _) = *self_arg_ty.kind()
1378+
&& ty == self_ty
1379+
{
1380+
match item.decl.inputs.values[0].type_ {
1381+
BorrowedRef { ref mut type_, .. } => **type_ = SelfTy,
1382+
_ => unreachable!(),
13871383
}
13881384
}
13891385
}
@@ -2331,25 +2327,22 @@ fn clean_middle_opaque_bounds<'tcx>(
23312327
let bindings: ThinVec<_> = bounds
23322328
.iter()
23332329
.filter_map(|(bound, _)| {
2334-
if let ty::ClauseKind::Projection(proj) = bound.kind().skip_binder() {
2335-
if proj.projection_term.trait_ref(cx.tcx) == trait_ref.skip_binder() {
2336-
Some(AssocItemConstraint {
2337-
assoc: projection_to_path_segment(
2338-
// FIXME: This needs to be made resilient for `AliasTerm`s that
2339-
// are associated consts.
2340-
bound.kind().rebind(proj.projection_term.expect_ty(cx.tcx)),
2341-
cx,
2342-
),
2343-
kind: AssocItemConstraintKind::Equality {
2344-
term: clean_middle_term(bound.kind().rebind(proj.term), cx),
2345-
},
2346-
})
2347-
} else {
2348-
None
2349-
}
2350-
} else {
2351-
None
2330+
if let ty::ClauseKind::Projection(proj) = bound.kind().skip_binder()
2331+
&& proj.projection_term.trait_ref(cx.tcx) == trait_ref.skip_binder()
2332+
{
2333+
return Some(AssocItemConstraint {
2334+
assoc: projection_to_path_segment(
2335+
// FIXME: This needs to be made resilient for `AliasTerm`s that
2336+
// are associated consts.
2337+
bound.kind().rebind(proj.projection_term.expect_ty(cx.tcx)),
2338+
cx,
2339+
),
2340+
kind: AssocItemConstraintKind::Equality {
2341+
term: clean_middle_term(bound.kind().rebind(proj.term), cx),
2342+
},
2343+
});
23522344
}
2345+
None
23532346
})
23542347
.collect();
23552348

@@ -2743,23 +2736,20 @@ fn add_without_unwanted_attributes<'hir>(
27432736
}
27442737
let mut attr = attr.clone();
27452738
match attr {
2746-
hir::Attribute::Unparsed(ref mut normal) => {
2747-
if let [ident] = &*normal.path.segments {
2748-
let ident = ident.name;
2749-
if ident == sym::doc {
2750-
filter_doc_attr(&mut normal.args, is_inline);
2751-
attrs.push((Cow::Owned(attr), import_parent));
2752-
} else if is_inline || ident != sym::cfg {
2753-
// If it's not a `cfg()` attribute, we keep it.
2754-
attrs.push((Cow::Owned(attr), import_parent));
2755-
}
2756-
}
2757-
}
2758-
hir::Attribute::Parsed(..) => {
2759-
if is_inline {
2739+
hir::Attribute::Unparsed(ref mut normal) if let [ident] = &*normal.path.segments => {
2740+
let ident = ident.name;
2741+
if ident == sym::doc {
2742+
filter_doc_attr(&mut normal.args, is_inline);
2743+
attrs.push((Cow::Owned(attr), import_parent));
2744+
} else if is_inline || ident != sym::cfg {
2745+
// If it's not a `cfg()` attribute, we keep it.
27602746
attrs.push((Cow::Owned(attr), import_parent));
27612747
}
27622748
}
2749+
hir::Attribute::Parsed(..) if is_inline => {
2750+
attrs.push((Cow::Owned(attr), import_parent));
2751+
}
2752+
_ => {}
27632753
}
27642754
}
27652755
}
@@ -2961,16 +2951,16 @@ fn clean_extern_crate<'tcx>(
29612951
&& !cx.is_json_output();
29622952

29632953
let krate_owner_def_id = krate.owner_id.def_id;
2964-
if please_inline {
2965-
if let Some(items) = inline::try_inline(
2954+
if please_inline
2955+
&& let Some(items) = inline::try_inline(
29662956
cx,
29672957
Res::Def(DefKind::Mod, crate_def_id),
29682958
name,
29692959
Some((attrs, Some(krate_owner_def_id))),
29702960
&mut Default::default(),
2971-
) {
2972-
return items;
2973-
}
2961+
)
2962+
{
2963+
return items;
29742964
}
29752965

29762966
vec![Item::from_def_id_and_parts(

Diff for: src/librustdoc/clean/types.rs

+17-19
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,11 @@ impl ExternalCrate {
208208
.get_attrs(def_id, sym::doc)
209209
.flat_map(|attr| attr.meta_item_list().unwrap_or_default());
210210
for meta in meta_items {
211-
if meta.has_name(sym::keyword) {
212-
if let Some(v) = meta.value_str() {
213-
keyword = Some(v);
214-
break;
215-
}
211+
if meta.has_name(sym::keyword)
212+
&& let Some(v) = meta.value_str()
213+
{
214+
keyword = Some(v);
215+
break;
216216
}
217217
}
218218
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>
10711071
// treat #[target_feature(enable = "feat")] attributes as if they were
10721072
// #[doc(cfg(target_feature = "feat"))] attributes as well
10731073
for attr in hir_attr_lists(attrs, sym::target_feature) {
1074-
if attr.has_name(sym::enable) {
1075-
if attr.value_str().is_some() {
1076-
// Clone `enable = "feat"`, change to `target_feature = "feat"`.
1077-
// Unwrap is safe because `value_str` succeeded above.
1078-
let mut meta = attr.meta_item().unwrap().clone();
1079-
meta.path = ast::Path::from_ident(Ident::with_dummy_span(sym::target_feature));
1080-
1081-
if let Ok(feat_cfg) = Cfg::parse(&ast::MetaItemInner::MetaItem(meta)) {
1082-
cfg &= feat_cfg;
1083-
}
1074+
if attr.has_name(sym::enable) && attr.value_str().is_some() {
1075+
// Clone `enable = "feat"`, change to `target_feature = "feat"`.
1076+
// Unwrap is safe because `value_str` succeeded above.
1077+
let mut meta = attr.meta_item().unwrap().clone();
1078+
meta.path = ast::Path::from_ident(Ident::with_dummy_span(sym::target_feature));
1079+
1080+
if let Ok(feat_cfg) = Cfg::parse(&ast::MetaItemInner::MetaItem(meta)) {
1081+
cfg &= feat_cfg;
10841082
}
10851083
}
10861084
}
@@ -1160,10 +1158,10 @@ impl Attributes {
11601158
continue;
11611159
}
11621160

1163-
if let Some(items) = attr.meta_item_list() {
1164-
if items.iter().filter_map(|i| i.meta_item()).any(|it| it.has_name(flag)) {
1165-
return true;
1166-
}
1161+
if let Some(items) = attr.meta_item_list()
1162+
&& items.iter().filter_map(|i| i.meta_item()).any(|it| it.has_name(flag))
1163+
{
1164+
return true;
11671165
}
11681166
}
11691167

Diff for: src/librustdoc/config.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -645,10 +645,10 @@ impl Options {
645645

646646
let extension_css = matches.opt_str("e").map(|s| PathBuf::from(&s));
647647

648-
if let Some(ref p) = extension_css {
649-
if !p.is_file() {
650-
dcx.fatal("option --extend-css argument must be a file");
651-
}
648+
if let Some(ref p) = extension_css
649+
&& !p.is_file()
650+
{
651+
dcx.fatal("option --extend-css argument must be a file");
652652
}
653653

654654
let mut themes = Vec::new();
@@ -720,10 +720,10 @@ impl Options {
720720
}
721721

722722
let index_page = matches.opt_str("index-page").map(|s| PathBuf::from(&s));
723-
if let Some(ref index_page) = index_page {
724-
if !index_page.is_file() {
725-
dcx.fatal("option `--index-page` argument must be a file");
726-
}
723+
if let Some(ref index_page) = index_page
724+
&& !index_page.is_file()
725+
{
726+
dcx.fatal("option `--index-page` argument must be a file");
727727
}
728728

729729
let target = parse_target_triple(early_dcx, matches);

Diff for: src/librustdoc/doctest/rust.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,9 @@ impl HirCollector<'_> {
9898
let ast_attrs = self.tcx.hir().attrs(self.tcx.local_def_id_to_hir_id(def_id));
9999
if let Some(ref cfg) =
100100
extract_cfg_from_attrs(ast_attrs.iter(), self.tcx, &FxHashSet::default())
101+
&& !cfg.matches(&self.tcx.sess.psess, Some(self.tcx.features()))
101102
{
102-
if !cfg.matches(&self.tcx.sess.psess, Some(self.tcx.features())) {
103-
return;
104-
}
103+
return;
105104
}
106105

107106
let has_name = !name.is_empty();

Diff for: src/librustdoc/formats/cache.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,9 @@ impl DocFolder for CacheBuilder<'_, '_> {
419419
}
420420
}
421421

422-
if let Some(generics) = i.trait_.as_ref().and_then(|t| t.generics()) {
422+
if let Some(trait_) = &i.trait_
423+
&& let Some(generics) = trait_.generics()
424+
{
423425
for bound in generics {
424426
dids.extend(bound.def_id(self.cache));
425427
}

Diff for: src/librustdoc/html/highlight.rs

+41-42
Original file line numberDiff line numberDiff line change
@@ -1102,53 +1102,52 @@ fn string_without_closing_tag<T: Display>(
11021102
});
11031103
}
11041104

1105-
if let Some(href_context) = href_context {
1106-
if let Some(href) =
1107-
href_context.context.shared.span_correspondence_map.get(&def_span).and_then(|href| {
1108-
let context = href_context.context;
1109-
// FIXME: later on, it'd be nice to provide two links (if possible) for all items:
1110-
// one to the documentation page and one to the source definition.
1111-
// FIXME: currently, external items only generate a link to their documentation,
1112-
// a link to their definition can be generated using this:
1113-
// https://github.com/rust-lang/rust/blob/60f1a2fc4b535ead9c85ce085fdce49b1b097531/src/librustdoc/html/render/context.rs#L315-L338
1114-
match href {
1115-
LinkFromSrc::Local(span) => {
1116-
context.href_from_span_relative(*span, &href_context.current_href)
1117-
}
1118-
LinkFromSrc::External(def_id) => {
1119-
format::href_with_root_path(*def_id, context, Some(href_context.root_path))
1120-
.ok()
1121-
.map(|(url, _, _)| url)
1122-
}
1123-
LinkFromSrc::Primitive(prim) => format::href_with_root_path(
1124-
PrimitiveType::primitive_locations(context.tcx())[prim],
1125-
context,
1126-
Some(href_context.root_path),
1127-
)
1128-
.ok()
1129-
.map(|(url, _, _)| url),
1130-
LinkFromSrc::Doc(def_id) => {
1131-
format::href_with_root_path(*def_id, context, Some(href_context.root_path))
1132-
.ok()
1133-
.map(|(doc_link, _, _)| doc_link)
1134-
}
1105+
if let Some(href_context) = href_context
1106+
&& let Some(href) = href_context.context.shared.span_correspondence_map.get(&def_span)
1107+
&& let Some(href) = {
1108+
let context = href_context.context;
1109+
// FIXME: later on, it'd be nice to provide two links (if possible) for all items:
1110+
// one to the documentation page and one to the source definition.
1111+
// FIXME: currently, external items only generate a link to their documentation,
1112+
// a link to their definition can be generated using this:
1113+
// https://github.com/rust-lang/rust/blob/60f1a2fc4b535ead9c85ce085fdce49b1b097531/src/librustdoc/html/render/context.rs#L315-L338
1114+
match href {
1115+
LinkFromSrc::Local(span) => {
1116+
context.href_from_span_relative(*span, &href_context.current_href)
11351117
}
1136-
})
1137-
{
1138-
if !open_tag {
1139-
// We're already inside an element which has the same klass, no need to give it
1140-
// again.
1118+
LinkFromSrc::External(def_id) => {
1119+
format::href_with_root_path(*def_id, context, Some(href_context.root_path))
1120+
.ok()
1121+
.map(|(url, _, _)| url)
1122+
}
1123+
LinkFromSrc::Primitive(prim) => format::href_with_root_path(
1124+
PrimitiveType::primitive_locations(context.tcx())[prim],
1125+
context,
1126+
Some(href_context.root_path),
1127+
)
1128+
.ok()
1129+
.map(|(url, _, _)| url),
1130+
LinkFromSrc::Doc(def_id) => {
1131+
format::href_with_root_path(*def_id, context, Some(href_context.root_path))
1132+
.ok()
1133+
.map(|(doc_link, _, _)| doc_link)
1134+
}
1135+
}
1136+
}
1137+
{
1138+
if !open_tag {
1139+
// We're already inside an element which has the same klass, no need to give it
1140+
// again.
1141+
write!(out, "<a href=\"{href}\">{text_s}").unwrap();
1142+
} else {
1143+
let klass_s = klass.as_html();
1144+
if klass_s.is_empty() {
11411145
write!(out, "<a href=\"{href}\">{text_s}").unwrap();
11421146
} else {
1143-
let klass_s = klass.as_html();
1144-
if klass_s.is_empty() {
1145-
write!(out, "<a href=\"{href}\">{text_s}").unwrap();
1146-
} else {
1147-
write!(out, "<a class=\"{klass_s}\" href=\"{href}\">{text_s}").unwrap();
1148-
}
1147+
write!(out, "<a class=\"{klass_s}\" href=\"{href}\">{text_s}").unwrap();
11491148
}
1150-
return Some("</a>");
11511149
}
1150+
return Some("</a>");
11521151
}
11531152
if !open_tag {
11541153
write!(out, "{}", text_s).unwrap();

0 commit comments

Comments
 (0)