Improve code to remove duplication

This commit is contained in:
Guillaume Gomez 2023-05-05 15:59:41 +02:00
parent fb160d5d3b
commit cbc6daa559

View File

@ -119,14 +119,25 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
}); });
let kind = ModuleItem(Module { items, span }); let kind = ModuleItem(Module { items, span });
let def_id = doc.def_id.to_def_id(); generate_item_with_correct_attrs(cx, kind, doc.def_id, doc.name, doc.import_id, doc.renamed)
}
fn generate_item_with_correct_attrs(
cx: &mut DocContext<'_>,
kind: ItemKind,
local_def_id: LocalDefId,
name: Symbol,
import_id: Option<LocalDefId>,
renamed: Option<Symbol>,
) -> Item {
let def_id = local_def_id.to_def_id();
let target_attrs = inline::load_attrs(cx, def_id); let target_attrs = inline::load_attrs(cx, def_id);
let attrs = if let Some(import_id) = doc.import_id { let attrs = if let Some(import_id) = import_id {
let is_inline = inline::load_attrs(cx, import_id.to_def_id()) let is_inline = inline::load_attrs(cx, import_id.to_def_id())
.lists(sym::doc) .lists(sym::doc)
.get_word_attr(sym::inline) .get_word_attr(sym::inline)
.is_some(); .is_some();
let mut attrs = get_all_import_attributes(cx, import_id, doc.def_id, is_inline); let mut attrs = get_all_import_attributes(cx, import_id, local_def_id, is_inline);
add_without_unwanted_attributes(&mut attrs, target_attrs, is_inline, None); add_without_unwanted_attributes(&mut attrs, target_attrs, is_inline, None);
attrs attrs
} else { } else {
@ -137,9 +148,9 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
let cfg = attrs.cfg(cx.tcx, &cx.cache.hidden_cfg); let cfg = attrs.cfg(cx.tcx, &cx.cache.hidden_cfg);
let attrs = Attributes::from_ast_iter(attrs.iter().map(|(attr, did)| (&**attr, *did)), false); let attrs = Attributes::from_ast_iter(attrs.iter().map(|(attr, did)| (&**attr, *did)), false);
let name = doc.renamed.or_else(|| Some(doc.name)); let name = renamed.or(Some(name));
let mut item = Item::from_def_id_and_attrs_and_parts(def_id, name, kind, Box::new(attrs), cfg); let mut item = Item::from_def_id_and_attrs_and_parts(def_id, name, kind, Box::new(attrs), cfg);
item.inline_stmt_id = doc.import_id.map(|local| local.to_def_id()); item.inline_stmt_id = import_id.map(|local| local.to_def_id());
item item
} }
@ -2309,29 +2320,14 @@ fn clean_maybe_renamed_item<'tcx>(
_ => unreachable!("not yet converted"), _ => unreachable!("not yet converted"),
}; };
let target_attrs = inline::load_attrs(cx, def_id); vec![generate_item_with_correct_attrs(
let attrs = if let Some(import_id) = import_id { cx,
let is_inline = inline::load_attrs(cx, import_id.to_def_id()) kind,
.lists(sym::doc) item.owner_id.def_id,
.get_word_attr(sym::inline) name,
.is_some(); import_id,
let mut attrs = renamed,
get_all_import_attributes(cx, import_id, item.owner_id.def_id, is_inline); )]
add_without_unwanted_attributes(&mut attrs, target_attrs, is_inline, None);
attrs
} else {
// We only keep the item's attributes.
target_attrs.iter().map(|attr| (Cow::Borrowed(attr), None)).collect()
};
let cfg = attrs.cfg(cx.tcx, &cx.cache.hidden_cfg);
let attrs =
Attributes::from_ast_iter(attrs.iter().map(|(attr, did)| (&**attr, *did)), false);
let mut item =
Item::from_def_id_and_attrs_and_parts(def_id, Some(name), kind, Box::new(attrs), cfg);
item.inline_stmt_id = import_id.map(|local| local.to_def_id());
vec![item]
}) })
} }