Skip to content

Commit cbc6daa

Browse files
Improve code to remove duplication
1 parent fb160d5 commit cbc6daa

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

src/librustdoc/clean/mod.rs

+24-28
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,25 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
119119
});
120120

121121
let kind = ModuleItem(Module { items, span });
122-
let def_id = doc.def_id.to_def_id();
122+
generate_item_with_correct_attrs(cx, kind, doc.def_id, doc.name, doc.import_id, doc.renamed)
123+
}
124+
125+
fn generate_item_with_correct_attrs(
126+
cx: &mut DocContext<'_>,
127+
kind: ItemKind,
128+
local_def_id: LocalDefId,
129+
name: Symbol,
130+
import_id: Option<LocalDefId>,
131+
renamed: Option<Symbol>,
132+
) -> Item {
133+
let def_id = local_def_id.to_def_id();
123134
let target_attrs = inline::load_attrs(cx, def_id);
124-
let attrs = if let Some(import_id) = doc.import_id {
135+
let attrs = if let Some(import_id) = import_id {
125136
let is_inline = inline::load_attrs(cx, import_id.to_def_id())
126137
.lists(sym::doc)
127138
.get_word_attr(sym::inline)
128139
.is_some();
129-
let mut attrs = get_all_import_attributes(cx, import_id, doc.def_id, is_inline);
140+
let mut attrs = get_all_import_attributes(cx, import_id, local_def_id, is_inline);
130141
add_without_unwanted_attributes(&mut attrs, target_attrs, is_inline, None);
131142
attrs
132143
} else {
@@ -137,9 +148,9 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
137148
let cfg = attrs.cfg(cx.tcx, &cx.cache.hidden_cfg);
138149
let attrs = Attributes::from_ast_iter(attrs.iter().map(|(attr, did)| (&**attr, *did)), false);
139150

140-
let name = doc.renamed.or_else(|| Some(doc.name));
151+
let name = renamed.or(Some(name));
141152
let mut item = Item::from_def_id_and_attrs_and_parts(def_id, name, kind, Box::new(attrs), cfg);
142-
item.inline_stmt_id = doc.import_id.map(|local| local.to_def_id());
153+
item.inline_stmt_id = import_id.map(|local| local.to_def_id());
143154
item
144155
}
145156

@@ -2309,29 +2320,14 @@ fn clean_maybe_renamed_item<'tcx>(
23092320
_ => unreachable!("not yet converted"),
23102321
};
23112322

2312-
let target_attrs = inline::load_attrs(cx, def_id);
2313-
let attrs = if let Some(import_id) = import_id {
2314-
let is_inline = inline::load_attrs(cx, import_id.to_def_id())
2315-
.lists(sym::doc)
2316-
.get_word_attr(sym::inline)
2317-
.is_some();
2318-
let mut attrs =
2319-
get_all_import_attributes(cx, import_id, item.owner_id.def_id, is_inline);
2320-
add_without_unwanted_attributes(&mut attrs, target_attrs, is_inline, None);
2321-
attrs
2322-
} else {
2323-
// We only keep the item's attributes.
2324-
target_attrs.iter().map(|attr| (Cow::Borrowed(attr), None)).collect()
2325-
};
2326-
2327-
let cfg = attrs.cfg(cx.tcx, &cx.cache.hidden_cfg);
2328-
let attrs =
2329-
Attributes::from_ast_iter(attrs.iter().map(|(attr, did)| (&**attr, *did)), false);
2330-
2331-
let mut item =
2332-
Item::from_def_id_and_attrs_and_parts(def_id, Some(name), kind, Box::new(attrs), cfg);
2333-
item.inline_stmt_id = import_id.map(|local| local.to_def_id());
2334-
vec![item]
2323+
vec![generate_item_with_correct_attrs(
2324+
cx,
2325+
kind,
2326+
item.owner_id.def_id,
2327+
name,
2328+
import_id,
2329+
renamed,
2330+
)]
23352331
})
23362332
}
23372333

0 commit comments

Comments
 (0)