1
Fork 0

Account for doc comments coming from proc macros without spans

This commit is contained in:
Esteban Küber 2019-08-26 17:46:14 -07:00
parent 9b91b9c10e
commit b2b9b81c9a
3 changed files with 14 additions and 13 deletions

View file

@ -69,7 +69,7 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
// We couldn't calculate the span of the markdown block that had the error, so our // We couldn't calculate the span of the markdown block that had the error, so our
// diagnostics are going to be a bit lacking. // diagnostics are going to be a bit lacking.
let mut diag = self.cx.sess().struct_span_warn( let mut diag = self.cx.sess().struct_span_warn(
super::span_of_attrs(&item.attrs), super::span_of_attrs(&item.attrs).unwrap_or(item.source.span()),
"doc comment contains an invalid Rust code block", "doc comment contains an invalid Rust code block",
); );

View file

@ -465,7 +465,7 @@ fn resolution_failure(
} }
}; };
let attrs = &item.attrs; let attrs = &item.attrs;
let sp = span_of_attrs(attrs); let sp = span_of_attrs(attrs).unwrap_or(item.source.span());
let mut diag = cx.tcx.struct_span_lint_hir( let mut diag = cx.tcx.struct_span_lint_hir(
lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE, lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE,
@ -517,7 +517,7 @@ fn ambiguity_error(
} }
}; };
let attrs = &item.attrs; let attrs = &item.attrs;
let sp = span_of_attrs(attrs); let sp = span_of_attrs(attrs).unwrap_or(item.source.span());
let mut msg = format!("`{}` is ", path_str); let mut msg = format!("`{}` is ", path_str);

View file

@ -339,7 +339,7 @@ pub fn look_for_tests<'tcx>(
find_testable_code(&dox, &mut tests, ErrorCodes::No); find_testable_code(&dox, &mut tests, ErrorCodes::No);
if check_missing_code == true && tests.found_tests == 0 { if check_missing_code == true && tests.found_tests == 0 {
let sp = span_of_attrs(&item.attrs).substitute_dummy(item.source.span()); let sp = span_of_attrs(&item.attrs).unwrap_or(item.source.span());
let mut diag = cx.tcx.struct_span_lint_hir( let mut diag = cx.tcx.struct_span_lint_hir(
lint::builtin::MISSING_DOC_CODE_EXAMPLES, lint::builtin::MISSING_DOC_CODE_EXAMPLES,
hir_id, hir_id,
@ -352,20 +352,23 @@ pub fn look_for_tests<'tcx>(
let mut diag = cx.tcx.struct_span_lint_hir( let mut diag = cx.tcx.struct_span_lint_hir(
lint::builtin::PRIVATE_DOC_TESTS, lint::builtin::PRIVATE_DOC_TESTS,
hir_id, hir_id,
span_of_attrs(&item.attrs), span_of_attrs(&item.attrs).unwrap_or(item.source.span()),
"Documentation test in private item"); "Documentation test in private item");
diag.emit(); diag.emit();
} }
} }
/// Returns a span encompassing all the given attributes. /// Returns a span encompassing all the given attributes.
crate fn span_of_attrs(attrs: &clean::Attributes) -> Span { crate fn span_of_attrs(attrs: &clean::Attributes) -> Option<Span> {
if attrs.doc_strings.is_empty() { if attrs.doc_strings.is_empty() {
return DUMMY_SP; return None;
} }
let start = attrs.doc_strings[0].span(); let start = attrs.doc_strings[0].span();
if start == DUMMY_SP {
return None;
}
let end = attrs.doc_strings.last().expect("No doc strings provided").span(); let end = attrs.doc_strings.last().expect("No doc strings provided").span();
start.to(end) Some(start.to(end))
} }
/// Attempts to match a range of bytes from parsed markdown to a `Span` in the source code. /// Attempts to match a range of bytes from parsed markdown to a `Span` in the source code.
@ -391,7 +394,7 @@ crate fn source_span_for_markdown_range(
let snippet = cx let snippet = cx
.sess() .sess()
.source_map() .source_map()
.span_to_snippet(span_of_attrs(attrs)) .span_to_snippet(span_of_attrs(attrs)?)
.ok()?; .ok()?;
let starting_line = markdown[..md_range.start].matches('\n').count(); let starting_line = markdown[..md_range.start].matches('\n').count();
@ -441,10 +444,8 @@ crate fn source_span_for_markdown_range(
} }
} }
let sp = span_of_attrs(attrs).from_inner(InnerSpan::new( Some(span_of_attrs(attrs)?.from_inner(InnerSpan::new(
md_range.start + start_bytes, md_range.start + start_bytes,
md_range.end + start_bytes + end_bytes, md_range.end + start_bytes + end_bytes,
)); )))
Some(sp)
} }