From 83fce478f43c91dd94b140a6fc4430d68ddc87a9 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 20 Aug 2024 18:09:58 +0200 Subject: [PATCH] Remove unneeded conversion to `DefId` for `ExtraInfo` --- src/librustdoc/clean/types.rs | 7 ++- src/librustdoc/doctest/rust.rs | 2 +- src/librustdoc/html/markdown.rs | 44 +++++++++---------- .../passes/lint/check_code_block_syntax.rs | 6 ++- 4 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index ff5c16f2b3e..63d71a73cdf 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -12,7 +12,7 @@ use rustc_const_eval::const_eval::is_unstable_const_fn; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_hir::def::{CtorKind, DefKind, Res}; -use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE}; +use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE}; use rustc_hir::lang_items::LangItem; use rustc_hir::{BodyId, Mutability}; use rustc_hir_analysis::check::intrinsic::intrinsic_operation_unsafety; @@ -88,6 +88,11 @@ pub(crate) fn as_def_id(self) -> Option { } } + #[inline] + pub(crate) fn as_local_def_id(self) -> Option { + self.as_def_id().and_then(|id| id.as_local()) + } + #[inline] pub(crate) fn krate(self) -> CrateNum { match self { diff --git a/src/librustdoc/doctest/rust.rs b/src/librustdoc/doctest/rust.rs index abd66f15dc0..5c0898f28fc 100644 --- a/src/librustdoc/doctest/rust.rs +++ b/src/librustdoc/doctest/rust.rs @@ -136,7 +136,7 @@ fn visit_testable( self.enable_per_target_ignores, Some(&crate::html::markdown::ExtraInfo::new( self.tcx, - def_id.to_def_id(), + def_id, span_of_fragments(&attrs.doc_strings).unwrap_or(sp), )), ); diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 7bfe5d87d39..364d4e077b1 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -40,7 +40,7 @@ }; use rustc_data_structures::fx::FxHashMap; use rustc_errors::{Diag, DiagMessage}; -use rustc_hir::def_id::DefId; +use rustc_hir::def_id::LocalDefId; use rustc_middle::ty::TyCtxt; pub(crate) use rustc_resolve::rustdoc::main_body_opts; use rustc_resolve::rustdoc::may_be_doc_link; @@ -818,27 +818,25 @@ pub(crate) fn find_codes( } pub(crate) struct ExtraInfo<'tcx> { - def_id: DefId, + def_id: LocalDefId, sp: Span, tcx: TyCtxt<'tcx>, } impl<'tcx> ExtraInfo<'tcx> { - pub(crate) fn new(tcx: TyCtxt<'tcx>, def_id: DefId, sp: Span) -> ExtraInfo<'tcx> { + pub(crate) fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId, sp: Span) -> ExtraInfo<'tcx> { ExtraInfo { def_id, sp, tcx } } fn error_invalid_codeblock_attr(&self, msg: impl Into) { - if let Some(def_id) = self.def_id.as_local() { - self.tcx.node_span_lint( - crate::lint::INVALID_CODEBLOCK_ATTRIBUTES, - self.tcx.local_def_id_to_hir_id(def_id), - self.sp, - |lint| { - lint.primary_message(msg); - }, - ); - } + self.tcx.node_span_lint( + crate::lint::INVALID_CODEBLOCK_ATTRIBUTES, + self.tcx.local_def_id_to_hir_id(self.def_id), + self.sp, + |lint| { + lint.primary_message(msg); + }, + ); } fn error_invalid_codeblock_attr_with_help( @@ -846,17 +844,15 @@ fn error_invalid_codeblock_attr_with_help( msg: impl Into, f: impl for<'a, 'b> FnOnce(&'b mut Diag<'a, ()>), ) { - if let Some(def_id) = self.def_id.as_local() { - self.tcx.node_span_lint( - crate::lint::INVALID_CODEBLOCK_ATTRIBUTES, - self.tcx.local_def_id_to_hir_id(def_id), - self.sp, - |lint| { - lint.primary_message(msg); - f(lint); - }, - ); - } + self.tcx.node_span_lint( + crate::lint::INVALID_CODEBLOCK_ATTRIBUTES, + self.tcx.local_def_id_to_hir_id(self.def_id), + self.sp, + |lint| { + lint.primary_message(msg); + f(lint); + }, + ); } } diff --git a/src/librustdoc/passes/lint/check_code_block_syntax.rs b/src/librustdoc/passes/lint/check_code_block_syntax.rs index ef05befdddc..977c0953336 100644 --- a/src/librustdoc/passes/lint/check_code_block_syntax.rs +++ b/src/librustdoc/passes/lint/check_code_block_syntax.rs @@ -16,9 +16,11 @@ use crate::html::markdown::{self, RustCodeBlock}; pub(crate) fn visit_item(cx: &DocContext<'_>, item: &clean::Item) { - if let Some(dox) = &item.opt_doc_value() { + if let Some(def_id) = item.item_id.as_local_def_id() + && let Some(dox) = &item.opt_doc_value() + { let sp = item.attr_span(cx.tcx); - let extra = crate::html::markdown::ExtraInfo::new(cx.tcx, item.item_id.expect_def_id(), sp); + let extra = crate::html::markdown::ExtraInfo::new(cx.tcx, def_id, sp); for code_block in markdown::rust_code_blocks(dox, &extra) { check_rust_syntax(cx, item, dox, code_block); }