Skip to content

Commit 26cef0c

Browse files
Rollup merge of #129318 - GuillaumeGomez:rm-unneeded-defid-conversion, r=notriddle
Remove unneeded conversion to `DefId` for `ExtraInfo` I'm working on adding support for "unit test doctests" and this first cleanup came up so just sending it ahead of the rest. r? `@notriddle`
2 parents d8d1ad9 + 83fce47 commit 26cef0c

File tree

4 files changed

+31
-28
lines changed

4 files changed

+31
-28
lines changed

src/librustdoc/clean/types.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_attr::{ConstStability, Deprecation, Stability, StabilityLevel, StableS
1212
use rustc_const_eval::const_eval::is_unstable_const_fn;
1313
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1414
use rustc_hir::def::{CtorKind, DefKind, Res};
15-
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
15+
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
1616
use rustc_hir::lang_items::LangItem;
1717
use rustc_hir::{BodyId, Mutability};
1818
use rustc_hir_analysis::check::intrinsic::intrinsic_operation_unsafety;
@@ -88,6 +88,11 @@ impl ItemId {
8888
}
8989
}
9090

91+
#[inline]
92+
pub(crate) fn as_local_def_id(self) -> Option<LocalDefId> {
93+
self.as_def_id().and_then(|id| id.as_local())
94+
}
95+
9196
#[inline]
9297
pub(crate) fn krate(self) -> CrateNum {
9398
match self {

src/librustdoc/doctest/rust.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl<'a, 'tcx> HirCollector<'a, 'tcx> {
136136
self.enable_per_target_ignores,
137137
Some(&crate::html::markdown::ExtraInfo::new(
138138
self.tcx,
139-
def_id.to_def_id(),
139+
def_id,
140140
span_of_fragments(&attrs.doc_strings).unwrap_or(sp),
141141
)),
142142
);

src/librustdoc/html/markdown.rs

+20-24
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use pulldown_cmark::{
4040
};
4141
use rustc_data_structures::fx::FxHashMap;
4242
use rustc_errors::{Diag, DiagMessage};
43-
use rustc_hir::def_id::DefId;
43+
use rustc_hir::def_id::LocalDefId;
4444
use rustc_middle::ty::TyCtxt;
4545
pub(crate) use rustc_resolve::rustdoc::main_body_opts;
4646
use rustc_resolve::rustdoc::may_be_doc_link;
@@ -818,45 +818,41 @@ pub(crate) fn find_codes<T: doctest::DocTestVisitor>(
818818
}
819819

820820
pub(crate) struct ExtraInfo<'tcx> {
821-
def_id: DefId,
821+
def_id: LocalDefId,
822822
sp: Span,
823823
tcx: TyCtxt<'tcx>,
824824
}
825825

826826
impl<'tcx> ExtraInfo<'tcx> {
827-
pub(crate) fn new(tcx: TyCtxt<'tcx>, def_id: DefId, sp: Span) -> ExtraInfo<'tcx> {
827+
pub(crate) fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId, sp: Span) -> ExtraInfo<'tcx> {
828828
ExtraInfo { def_id, sp, tcx }
829829
}
830830

831831
fn error_invalid_codeblock_attr(&self, msg: impl Into<DiagMessage>) {
832-
if let Some(def_id) = self.def_id.as_local() {
833-
self.tcx.node_span_lint(
834-
crate::lint::INVALID_CODEBLOCK_ATTRIBUTES,
835-
self.tcx.local_def_id_to_hir_id(def_id),
836-
self.sp,
837-
|lint| {
838-
lint.primary_message(msg);
839-
},
840-
);
841-
}
832+
self.tcx.node_span_lint(
833+
crate::lint::INVALID_CODEBLOCK_ATTRIBUTES,
834+
self.tcx.local_def_id_to_hir_id(self.def_id),
835+
self.sp,
836+
|lint| {
837+
lint.primary_message(msg);
838+
},
839+
);
842840
}
843841

844842
fn error_invalid_codeblock_attr_with_help(
845843
&self,
846844
msg: impl Into<DiagMessage>,
847845
f: impl for<'a, 'b> FnOnce(&'b mut Diag<'a, ()>),
848846
) {
849-
if let Some(def_id) = self.def_id.as_local() {
850-
self.tcx.node_span_lint(
851-
crate::lint::INVALID_CODEBLOCK_ATTRIBUTES,
852-
self.tcx.local_def_id_to_hir_id(def_id),
853-
self.sp,
854-
|lint| {
855-
lint.primary_message(msg);
856-
f(lint);
857-
},
858-
);
859-
}
847+
self.tcx.node_span_lint(
848+
crate::lint::INVALID_CODEBLOCK_ATTRIBUTES,
849+
self.tcx.local_def_id_to_hir_id(self.def_id),
850+
self.sp,
851+
|lint| {
852+
lint.primary_message(msg);
853+
f(lint);
854+
},
855+
);
860856
}
861857
}
862858

src/librustdoc/passes/lint/check_code_block_syntax.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ use crate::core::DocContext;
1616
use crate::html::markdown::{self, RustCodeBlock};
1717

1818
pub(crate) fn visit_item(cx: &DocContext<'_>, item: &clean::Item) {
19-
if let Some(dox) = &item.opt_doc_value() {
19+
if let Some(def_id) = item.item_id.as_local_def_id()
20+
&& let Some(dox) = &item.opt_doc_value()
21+
{
2022
let sp = item.attr_span(cx.tcx);
21-
let extra = crate::html::markdown::ExtraInfo::new(cx.tcx, item.item_id.expect_def_id(), sp);
23+
let extra = crate::html::markdown::ExtraInfo::new(cx.tcx, def_id, sp);
2224
for code_block in markdown::rust_code_blocks(dox, &extra) {
2325
check_rust_syntax(cx, item, dox, code_block);
2426
}

0 commit comments

Comments
 (0)