Skip to content
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

rustdoc: convert if let Some() that always matches to variable #112251

Merged
merged 1 commit into from
Jun 7, 2023
Merged
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
46 changes: 28 additions & 18 deletions src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustc_span::hygiene::MacroKind;
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::Span;

use std::{iter, mem};
use std::mem;

use crate::clean::{cfg::Cfg, reexport_chain, AttributesExt, NestedAttributesExt};
use crate::core;
Expand Down Expand Up @@ -291,27 +291,15 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
if !please_inline {
let inherits_hidden = inherits_doc_hidden(tcx, res_did, None);
// Only inline if requested or if the item would otherwise be stripped.
//
// If it's a doc hidden module, we need to keep it in case some of its inner items
// are re-exported.
if (!is_private && !inherits_hidden) || (
is_hidden &&
// If it's a doc hidden module, we need to keep it in case some of its inner items
// are re-exported.
!matches!(item, Node::Item(&hir::Item { kind: hir::ItemKind::Mod(_), .. }))
) {
return false;
} else if let Some(item_def_id) = reexport_chain(tcx, def_id, res_did).iter()
.flat_map(|reexport| reexport.id()).map(|id| id.expect_local())
.chain(iter::once(res_did)).nth(1) &&
item_def_id != def_id &&
self
.cx
.cache
.effective_visibilities
.is_directly_public(tcx, item_def_id.to_def_id()) &&
!tcx.is_doc_hidden(item_def_id) &&
!inherits_doc_hidden(tcx, item_def_id, None)
{
) ||
// The imported item is public and not `doc(hidden)` so no need to inline it.
self.reexport_public_and_not_hidden(def_id, res_did)
{
return false;
}
}
Expand Down Expand Up @@ -359,6 +347,28 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
ret
}

/// Returns `true` if the item is visible, meaning it's not `#[doc(hidden)]` or private.
///
/// This function takes into account the entire re-export `use` chain, so it needs the
/// ID of the "leaf" `use` and the ID of the "root" item.
fn reexport_public_and_not_hidden(
&self,
import_def_id: LocalDefId,
target_def_id: LocalDefId,
) -> bool {
let tcx = self.cx.tcx;
let item_def_id = reexport_chain(tcx, import_def_id, target_def_id)
.iter()
.flat_map(|reexport| reexport.id())
.map(|id| id.expect_local())
.nth(1)
.unwrap_or(target_def_id);
item_def_id != import_def_id
&& self.cx.cache.effective_visibilities.is_directly_public(tcx, item_def_id.to_def_id())
&& !tcx.is_doc_hidden(item_def_id)
&& !inherits_doc_hidden(tcx, item_def_id, None)
}

#[inline]
fn add_to_current_mod(
&mut self,
Expand Down