Skip to content

Commit

Permalink
Rollup merge of rust-lang#114129 - GuillaumeGomez:rustdoc-cleanup, r=…
Browse files Browse the repository at this point in the history
…notriddle

Rustdoc small cleanups

Each commit does some little cleanups:

 * We had some `Res` comparisons in multiple places (and still do, but unless we use a macro, it's not possible to "merge" any further) so I moved it into a function.
 * It was weird to have some utils function used everywhere in `visit_ast` so I instead moved it into `clean/utils.rs`.
 * In HTML rendering, we had some write "issues":
   * Multiple calls that could be merged into one.
   * Some `write!` that could be `write_str`.
   * We didn't use the new `format!` args much.

r? `@notriddle`
  • Loading branch information
matthiaskrgr authored Jul 28, 2023
2 parents d694408 + 65f311d commit 3461cd5
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 109 deletions.
2 changes: 1 addition & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2818,7 +2818,7 @@ fn clean_use_statement_inner<'tcx>(
cx: &mut DocContext<'tcx>,
inlined_names: &mut FxHashSet<(ItemType, Symbol)>,
) -> Vec<Item> {
if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = path.res {
if should_ignore_res(path.res) {
return Vec::new();
}
// We need this comparison because some imports (for std types for example)
Expand Down
34 changes: 33 additions & 1 deletion src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc_ast as ast;
use rustc_ast::tokenstream::TokenTree;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
use rustc_middle::mir;
use rustc_middle::mir::interpret::ConstValue;
use rustc_middle::ty::{self, GenericArgKind, GenericArgsRef, TyCtxt};
Expand Down Expand Up @@ -629,3 +629,35 @@ pub(super) fn display_macro_source(
}
}
}

pub(crate) fn inherits_doc_hidden(
tcx: TyCtxt<'_>,
mut def_id: LocalDefId,
stop_at: Option<LocalDefId>,
) -> bool {
let hir = tcx.hir();
while let Some(id) = tcx.opt_local_parent(def_id) {
if let Some(stop_at) = stop_at && id == stop_at {
return false;
}
def_id = id;
if tcx.is_doc_hidden(def_id.to_def_id()) {
return true;
} else if let Some(node) = hir.find_by_def_id(def_id) &&
matches!(
node,
hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(_), .. }),
)
{
// `impl` blocks stand a bit on their own: unless they have `#[doc(hidden)]` directly
// on them, they don't inherit it from the parent context.
return false;
}
}
false
}

#[inline]
pub(crate) fn should_ignore_res(res: Res) -> bool {
matches!(res, Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..))
}
Loading

0 comments on commit 3461cd5

Please sign in to comment.