Skip to content

Remove kw::Empty uses in rustdoc #139846

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

Merged
merged 5 commits into from
Apr 17, 2025
Merged
Changes from 1 commit
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
35 changes: 19 additions & 16 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,15 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
hir::ItemKind::Use(path, kind) => {
let hir::UsePath { segments, span, .. } = *path;
let path = hir::Path { segments, res: *res, span };
clean_use_statement_inner(import, name, &path, kind, cx, &mut Default::default())
clean_use_statement_inner(import, Some(name), &path, kind, cx, &mut Default::default())
}
_ => unreachable!(),
}
}));
items.extend(doc.items.values().flat_map(|(item, renamed, _)| {
// Now we actually lower the imports, skipping everything else.
if let hir::ItemKind::Use(path, hir::UseKind::Glob) = item.kind {
let name = renamed.unwrap_or(kw::Empty); // using kw::Empty is a bit of a hack
clean_use_statement(item, name, path, hir::UseKind::Glob, cx, &mut inserted)
clean_use_statement(item, *renamed, path, hir::UseKind::Glob, cx, &mut inserted)
} else {
// skip everything else
Vec::new()
Expand Down Expand Up @@ -2792,10 +2791,11 @@ fn clean_maybe_renamed_item<'tcx>(
use hir::ItemKind;

let def_id = item.owner_id.to_def_id();
let mut name = renamed.unwrap_or_else(|| {
// FIXME: using kw::Empty is a bit of a hack
cx.tcx.hir_opt_name(item.hir_id()).unwrap_or(kw::Empty)
});
let mut name = if renamed.is_some() {
renamed
} else {
cx.tcx.hir_opt_name(item.hir_id())
};
Copy link
Member

@GuillaumeGomez GuillaumeGomez Apr 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to keep an Option around, right? If so:

Suggested change
let mut name = if renamed.is_some() {
renamed
} else {
cx.tcx.hir_opt_name(item.hir_id())
};
let mut name = renamed.unwrap_or_else(|| cx.tcx.hir_opt_name(item.hir_id()).unwrap());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't work because he hir_opt_name can return None. So it must be an Option, and then lower down we only unwrap it for certain item kinds that always have an ident.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. I have some ideas on how to improve it but for now let's move on.


cx.with_param_env(def_id, |cx| {
let kind = match item.kind {
Expand Down Expand Up @@ -2836,7 +2836,7 @@ fn clean_maybe_renamed_item<'tcx>(
item_type: Some(type_),
})),
item.owner_id.def_id.to_def_id(),
name,
name.unwrap(),
import_id,
renamed,
));
Expand All @@ -2861,13 +2861,15 @@ fn clean_maybe_renamed_item<'tcx>(
}),
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, macro_def),
source: display_macro_source(cx, name.unwrap(), macro_def),
macro_rules: macro_def.macro_rules,
}),
ItemKind::Macro(_, _, macro_kind) => clean_proc_macro(item, &mut name, macro_kind, cx),
ItemKind::Macro(_, _, macro_kind) => {
clean_proc_macro(item, name.as_mut().unwrap(), 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, &mut name, cx)
clean_fn_or_proc_macro(item, sig, generics, body_id, name.as_mut().unwrap(), cx)
}
ItemKind::Trait(_, _, _, generics, bounds, item_ids) => {
let items = item_ids
Expand All @@ -2883,7 +2885,7 @@ fn clean_maybe_renamed_item<'tcx>(
}))
}
ItemKind::ExternCrate(orig_name, _) => {
return clean_extern_crate(item, name, orig_name, cx);
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());
Expand All @@ -2895,7 +2897,7 @@ fn clean_maybe_renamed_item<'tcx>(
cx,
kind,
item.owner_id.def_id.to_def_id(),
name,
name.unwrap(),
import_id,
renamed,
)]
Expand Down Expand Up @@ -3006,7 +3008,7 @@ fn clean_extern_crate<'tcx>(

fn clean_use_statement<'tcx>(
import: &hir::Item<'tcx>,
name: Symbol,
name: Option<Symbol>,
path: &hir::UsePath<'tcx>,
kind: hir::UseKind,
cx: &mut DocContext<'tcx>,
Expand All @@ -3023,7 +3025,7 @@ fn clean_use_statement<'tcx>(

fn clean_use_statement_inner<'tcx>(
import: &hir::Item<'tcx>,
name: Symbol,
name: Option<Symbol>,
path: &hir::Path<'tcx>,
kind: hir::UseKind,
cx: &mut DocContext<'tcx>,
Expand All @@ -3042,7 +3044,7 @@ fn clean_use_statement_inner<'tcx>(
let visibility = cx.tcx.visibility(import.owner_id);
let attrs = cx.tcx.hir_attrs(import.hir_id());
let inline_attr = hir_attr_lists(attrs, sym::doc).get_word_attr(sym::inline);
let pub_underscore = visibility.is_public() && name == kw::Underscore;
let pub_underscore = visibility.is_public() && name == Some(kw::Underscore);
let current_mod = cx.tcx.parent_module_from_def_id(import.owner_id.def_id);
let import_def_id = import.owner_id.def_id;

Expand Down Expand Up @@ -3108,6 +3110,7 @@ fn clean_use_statement_inner<'tcx>(
}
Import::new_glob(resolve_use_source(cx, path), true)
} else {
let name = name.unwrap();
if inline_attr.is_none()
&& let Res::Def(DefKind::Mod, did) = path.res
&& !did.is_local()
Expand Down