Skip to content

Commit a4b6214

Browse files
committed
Add attr_span helper function
1 parent 7e11f3a commit a4b6214

File tree

6 files changed

+17
-17
lines changed

6 files changed

+17
-17
lines changed

src/librustdoc/clean/types.rs

+4
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ impl Item {
255255
if self.is_fake() { None } else { tcx.lookup_deprecation(self.def_id) }
256256
}
257257

258+
crate fn attr_span(&self, _tcx: TyCtxt<'_>) -> rustc_span::Span {
259+
crate::passes::span_of_attrs(&self.attrs).unwrap_or_else(|| self.span.inner())
260+
}
261+
258262
/// Finds the `doc` attribute as a NameValue and returns the corresponding
259263
/// value found.
260264
crate fn doc_value(&self) -> Option<String> {

src/librustdoc/passes/bare_urls.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{span_of_attrs, Pass};
1+
use super::Pass;
22
use crate::clean::*;
33
use crate::core::DocContext;
44
use crate::fold::DocFolder;
@@ -69,8 +69,7 @@ impl<'a, 'tcx> DocFolder for BareUrlsLinter<'a, 'tcx> {
6969
if !dox.is_empty() {
7070
let report_diag = |cx: &DocContext<'_>, msg: &str, url: &str, range: Range<usize>| {
7171
let sp = super::source_span_for_markdown_range(cx.tcx, &dox, &range, &item.attrs)
72-
.or_else(|| span_of_attrs(&item.attrs))
73-
.unwrap_or(item.span.inner());
72+
.unwrap_or_else(|| item.attr_span(cx.tcx));
7473
cx.tcx.struct_span_lint_hir(crate::lint::BARE_URLS, hir_id, sp, |lint| {
7574
lint.build(msg)
7675
.note("bare URLs are not automatically turned into clickable links")

src/librustdoc/passes/check_code_block_syntax.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::clean;
99
use crate::core::DocContext;
1010
use crate::fold::DocFolder;
1111
use crate::html::markdown::{self, RustCodeBlock};
12-
use crate::passes::{span_of_attrs, Pass};
12+
use crate::passes::Pass;
1313

1414
crate const CHECK_CODE_BLOCK_SYNTAX: Pass = Pass {
1515
name: "check-code-block-syntax",
@@ -86,7 +86,7 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
8686
// We couldn't calculate the span of the markdown block that had the error, so our
8787
// diagnostics are going to be a bit lacking.
8888
let mut diag = self.cx.sess().struct_span_warn(
89-
super::span_of_attrs(&item.attrs).unwrap_or(item.span.inner()),
89+
item.attr_span(self.cx.tcx),
9090
"doc comment contains an invalid Rust code block",
9191
);
9292

@@ -110,7 +110,7 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
110110
impl<'a, 'tcx> DocFolder for SyntaxChecker<'a, 'tcx> {
111111
fn fold_item(&mut self, item: clean::Item) -> Option<clean::Item> {
112112
if let Some(dox) = &item.attrs.collapsed_doc_value() {
113-
let sp = span_of_attrs(&item.attrs).unwrap_or(item.span.inner());
113+
let sp = item.attr_span(self.cx.tcx);
114114
let extra = crate::html::markdown::ExtraInfo::new_did(self.cx.tcx, item.def_id, sp);
115115
for code_block in markdown::rust_code_blocks(&dox, &extra) {
116116
self.check_rust_syntax(&item, &dox, code_block);

src/librustdoc/passes/collect_intra_doc_links.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ use crate::html::markdown::{markdown_links, MarkdownLink};
3737
use crate::lint::{BROKEN_INTRA_DOC_LINKS, PRIVATE_INTRA_DOC_LINKS};
3838
use crate::passes::Pass;
3939

40-
use super::span_of_attrs;
41-
4240
mod early;
4341
crate use early::IntraLinkCrateLoader;
4442

@@ -1242,7 +1240,7 @@ impl LinkCollector<'_, '_> {
12421240
&ori_link.range,
12431241
&item.attrs,
12441242
)
1245-
.unwrap_or_else(|| span_of_attrs(&item.attrs).unwrap_or(item.span.inner()));
1243+
.unwrap_or_else(|| item.attr_span(self.cx.tcx));
12461244

12471245
rustc_session::parse::feature_err(
12481246
&self.cx.tcx.sess.parse_sess,
@@ -1695,13 +1693,12 @@ fn report_diagnostic(
16951693
}
16961694
};
16971695

1698-
let attrs = &item.attrs;
1699-
let sp = span_of_attrs(attrs).unwrap_or(item.span.inner());
1696+
let sp = item.attr_span(tcx);
17001697

17011698
tcx.struct_span_lint_hir(lint, hir_id, sp, |lint| {
17021699
let mut diag = lint.build(msg);
17031700

1704-
let span = super::source_span_for_markdown_range(tcx, dox, link_range, attrs);
1701+
let span = super::source_span_for_markdown_range(tcx, dox, link_range, &item.attrs);
17051702

17061703
if let Some(sp) = span {
17071704
diag.set_span(sp);

src/librustdoc/passes/doc_test_lints.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! - MISSING_DOC_CODE_EXAMPLES: this lint is **UNSTABLE** and looks for public items missing doctests
44
//! - PRIVATE_DOC_TESTS: this lint is **STABLE** and looks for private items with doctests.
55
6-
use super::{span_of_attrs, Pass};
6+
use super::Pass;
77
use crate::clean;
88
use crate::clean::*;
99
use crate::core::DocContext;
@@ -97,7 +97,7 @@ crate fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) {
9797
if tests.found_tests == 0 && cx.tcx.sess.is_nightly_build() {
9898
if should_have_doc_example(cx, &item) {
9999
debug!("reporting error for {:?} (hir_id={:?})", item, hir_id);
100-
let sp = span_of_attrs(&item.attrs).unwrap_or(item.span.inner());
100+
let sp = item.attr_span(cx.tcx);
101101
cx.tcx.struct_span_lint_hir(
102102
crate::lint::MISSING_DOC_CODE_EXAMPLES,
103103
hir_id,
@@ -109,7 +109,7 @@ crate fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) {
109109
cx.tcx.struct_span_lint_hir(
110110
crate::lint::PRIVATE_DOC_TESTS,
111111
hir_id,
112-
span_of_attrs(&item.attrs).unwrap_or(item.span.inner()),
112+
item.attr_span(cx.tcx),
113113
|lint| lint.build("documentation test in private item").emit(),
114114
);
115115
}

src/librustdoc/passes/html_tags.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{span_of_attrs, Pass};
1+
use super::Pass;
22
use crate::clean::*;
33
use crate::core::DocContext;
44
use crate::fold::DocFolder;
@@ -181,7 +181,7 @@ impl<'a, 'tcx> DocFolder for InvalidHtmlTagsLinter<'a, 'tcx> {
181181
let sp = match super::source_span_for_markdown_range(tcx, &dox, range, &item.attrs)
182182
{
183183
Some(sp) => sp,
184-
None => span_of_attrs(&item.attrs).unwrap_or(item.span.inner()),
184+
None => item.attr_span(tcx),
185185
};
186186
tcx.struct_span_lint_hir(crate::lint::INVALID_HTML_TAGS, hir_id, sp, |lint| {
187187
lint.build(msg).emit()

0 commit comments

Comments
 (0)