Skip to content

Improve clean_maybe_renamed_item function code a bit #140008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 33 additions & 14 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2797,10 +2797,35 @@ fn clean_maybe_renamed_item<'tcx>(
) -> Vec<Item> {
use hir::ItemKind;

let def_id = item.owner_id.to_def_id();
let mut name = if renamed.is_some() { renamed } else { cx.tcx.hir_opt_name(item.hir_id()) };
fn get_name(
cx: &DocContext<'_>,
item: &hir::Item<'_>,
renamed: Option<Symbol>,
) -> Option<Symbol> {
renamed.or_else(|| cx.tcx.hir_opt_name(item.hir_id()))
}

let def_id = item.owner_id.to_def_id();
cx.with_param_env(def_id, |cx| {
// These kinds of item either don't need a `name` or accept a `None` one so we handle them
// before.
match item.kind {
ItemKind::Impl(impl_) => return clean_impl(impl_, item.owner_id.def_id, cx),
ItemKind::Use(path, kind) => {
return clean_use_statement(
item,
get_name(cx, item, renamed),
path,
kind,
cx,
&mut FxHashSet::default(),
);
}
_ => {}
}

let mut name = get_name(cx, item, renamed).unwrap();

let kind = match item.kind {
ItemKind::Static(_, ty, mutability, body_id) => StaticItem(Static {
type_: Box::new(clean_ty(ty, cx)),
Expand Down Expand Up @@ -2839,7 +2864,7 @@ fn clean_maybe_renamed_item<'tcx>(
item_type: Some(type_),
})),
item.owner_id.def_id.to_def_id(),
name.unwrap(),
name,
import_id,
renamed,
));
Expand All @@ -2862,17 +2887,14 @@ fn clean_maybe_renamed_item<'tcx>(
generics: clean_generics(generics, cx),
fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(),
}),
ItemKind::Impl(impl_) => return clean_impl(impl_, item.owner_id.def_id, cx),
ItemKind::Macro(_, macro_def, MacroKind::Bang) => MacroItem(Macro {
source: display_macro_source(cx, name.unwrap(), macro_def),
source: display_macro_source(cx, name, macro_def),
macro_rules: macro_def.macro_rules,
}),
ItemKind::Macro(_, _, macro_kind) => {
clean_proc_macro(item, name.as_mut().unwrap(), macro_kind, cx)
}
ItemKind::Macro(_, _, macro_kind) => clean_proc_macro(item, &mut name, macro_kind, cx),
// proc macros can have a name set by attributes
ItemKind::Fn { ref sig, generics, body: body_id, .. } => {
clean_fn_or_proc_macro(item, sig, generics, body_id, name.as_mut().unwrap(), cx)
clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx)
}
ItemKind::Trait(_, _, _, generics, bounds, item_ids) => {
let items = item_ids
Expand All @@ -2888,10 +2910,7 @@ fn clean_maybe_renamed_item<'tcx>(
}))
}
ItemKind::ExternCrate(orig_name, _) => {
return clean_extern_crate(item, name.unwrap(), orig_name, cx);
}
ItemKind::Use(path, kind) => {
return clean_use_statement(item, name, path, kind, cx, &mut FxHashSet::default());
return clean_extern_crate(item, name, orig_name, cx);
}
_ => span_bug!(item.span, "not yet converted"),
};
Expand All @@ -2900,7 +2919,7 @@ fn clean_maybe_renamed_item<'tcx>(
cx,
kind,
item.owner_id.def_id.to_def_id(),
name.unwrap(),
name,
import_id,
renamed,
)]
Expand Down
Loading