Skip to content

Commit 8f6ef1f

Browse files
Improve clean_maybe_renamed_item function code a bit
1 parent 1f76d21 commit 8f6ef1f

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

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

+33-14
Original file line numberDiff line numberDiff line change
@@ -2797,10 +2797,35 @@ fn clean_maybe_renamed_item<'tcx>(
27972797
) -> Vec<Item> {
27982798
use hir::ItemKind;
27992799

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

2808+
let def_id = item.owner_id.to_def_id();
28032809
cx.with_param_env(def_id, |cx| {
2810+
// These kinds of item either don't need a `name` or accept a `None` one so we handle them
2811+
// before.
2812+
match item.kind {
2813+
ItemKind::Impl(impl_) => return clean_impl(impl_, item.owner_id.def_id, cx),
2814+
ItemKind::Use(path, kind) => {
2815+
return clean_use_statement(
2816+
item,
2817+
get_name(cx, item, renamed),
2818+
path,
2819+
kind,
2820+
cx,
2821+
&mut FxHashSet::default(),
2822+
);
2823+
}
2824+
_ => {}
2825+
}
2826+
2827+
let mut name = get_name(cx, item, renamed).unwrap();
2828+
28042829
let kind = match item.kind {
28052830
ItemKind::Static(_, ty, mutability, body_id) => StaticItem(Static {
28062831
type_: Box::new(clean_ty(ty, cx)),
@@ -2839,7 +2864,7 @@ fn clean_maybe_renamed_item<'tcx>(
28392864
item_type: Some(type_),
28402865
})),
28412866
item.owner_id.def_id.to_def_id(),
2842-
name.unwrap(),
2867+
name,
28432868
import_id,
28442869
renamed,
28452870
));
@@ -2862,17 +2887,14 @@ fn clean_maybe_renamed_item<'tcx>(
28622887
generics: clean_generics(generics, cx),
28632888
fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(),
28642889
}),
2865-
ItemKind::Impl(impl_) => return clean_impl(impl_, item.owner_id.def_id, cx),
28662890
ItemKind::Macro(_, macro_def, MacroKind::Bang) => MacroItem(Macro {
2867-
source: display_macro_source(cx, name.unwrap(), macro_def),
2891+
source: display_macro_source(cx, name, macro_def),
28682892
macro_rules: macro_def.macro_rules,
28692893
}),
2870-
ItemKind::Macro(_, _, macro_kind) => {
2871-
clean_proc_macro(item, name.as_mut().unwrap(), macro_kind, cx)
2872-
}
2894+
ItemKind::Macro(_, _, macro_kind) => clean_proc_macro(item, &mut name, macro_kind, cx),
28732895
// proc macros can have a name set by attributes
28742896
ItemKind::Fn { ref sig, generics, body: body_id, .. } => {
2875-
clean_fn_or_proc_macro(item, sig, generics, body_id, name.as_mut().unwrap(), cx)
2897+
clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx)
28762898
}
28772899
ItemKind::Trait(_, _, _, generics, bounds, item_ids) => {
28782900
let items = item_ids
@@ -2888,10 +2910,7 @@ fn clean_maybe_renamed_item<'tcx>(
28882910
}))
28892911
}
28902912
ItemKind::ExternCrate(orig_name, _) => {
2891-
return clean_extern_crate(item, name.unwrap(), orig_name, cx);
2892-
}
2893-
ItemKind::Use(path, kind) => {
2894-
return clean_use_statement(item, name, path, kind, cx, &mut FxHashSet::default());
2913+
return clean_extern_crate(item, name, orig_name, cx);
28952914
}
28962915
_ => span_bug!(item.span, "not yet converted"),
28972916
};
@@ -2900,7 +2919,7 @@ fn clean_maybe_renamed_item<'tcx>(
29002919
cx,
29012920
kind,
29022921
item.owner_id.def_id.to_def_id(),
2903-
name.unwrap(),
2922+
name,
29042923
import_id,
29052924
renamed,
29062925
)]

0 commit comments

Comments
 (0)