Skip to content

Commit a9a435b

Browse files
committed
handle more edge cases
1 parent fc86385 commit a9a435b

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

compiler/rustc_resolve/src/rustdoc.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,11 +538,20 @@ pub fn source_span_for_markdown_range(
538538
// doc fragments, but the target range does not span across multiple
539539
// fragments.
540540
let mut match_data = None;
541+
let pat = &markdown[md_range.clone()];
542+
// this heirustic doesn't make sense with a zero-sized range.
543+
if pat.len() == 0 {
544+
return None;
545+
}
541546
for (i, fragment) in fragments.iter().enumerate() {
542547
if let Ok(snippet) = span_to_snippet(fragment.span)
543-
&& let Some(match_start) = snippet.find(&markdown[md_range.clone()])
548+
&& let Some(match_start) = snippet.find(pat)
544549
{
545-
if match_data.is_none() {
550+
// if there is either a match in a previous fragment,
551+
// or multiple matches in this fragment,
552+
// there is ambiguity.
553+
if match_data.is_none() &&
554+
!snippet[match_start+1..].contains(pat) {
546555
match_data = Some((i, match_start));
547556
} else {
548557
// heirustic produced ambiguity, return nothing.

src/librustdoc/passes/lint/unescaped_backticks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub(crate) fn visit_item(cx: &DocContext<'_>, item: &Item, hir_id: HirId, dox: &
4848
&(backtick_index..backtick_index + 1),
4949
&item.attrs.doc_strings,
5050
)
51-
.unwrap_or_else(|| item.attr_span(tcx));
51+
.unwrap_or_else(|| item.attr_span(tcx));
5252

5353
tcx.node_span_lint(crate::lint::UNESCAPED_BACKTICKS, hir_id, span, |lint| {
5454
lint.primary_message("unescaped backtick");

tests/rustdoc-ui/unescaped_backticks.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -628,21 +628,21 @@ LL | /// or even to add a number `n` to 42 (`add(42, n)\`)!
628628
| +
629629

630630
error: unescaped backtick
631-
--> $DIR/unescaped_backticks.rs:108:9
631+
--> $DIR/unescaped_backticks.rs:108:10
632632
|
633633
LL | #[doc = "`"]
634-
| ^^^
634+
| ^
635635
|
636636
= help: the opening or closing backtick of an inline code may be missing
637637
= help: if you meant to use a literal backtick, escape it
638638
change: `
639639
to this: \`
640640

641641
error: unescaped backtick
642-
--> $DIR/unescaped_backticks.rs:115:9
642+
--> $DIR/unescaped_backticks.rs:115:26
643643
|
644644
LL | #[doc = concat!("\\", "`")]
645-
| ^^^^^^^^^^^^^^^^^^^^
645+
| ^
646646
|
647647
= help: the opening backtick of an inline code may be missing
648648
change: \`

0 commit comments

Comments
 (0)