Skip to content

Commit

Permalink
fix: Avoid ICE in doc_nested_refdefs check by checking range (#14308)
Browse files Browse the repository at this point in the history
The `looks_like_refdef` function was assuming the range was valid, this
just adds a check to ensure that is the case. It also works around a
subtraction underflow due to the same invalid range.

changelog: [`doc_nested_refdefs`]: Fix #14287 by avoiding invalid ranges
  • Loading branch information
dswij authored Feb 27, 2025
2 parents f50266a + e399e15 commit 527ab05
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
11 changes: 10 additions & 1 deletion clippy_lints/src/doc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,12 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
// backslashes aren't in the event stream...
start -= 1;
}
start - range.start

if start > range.start {
start - range.start
} else {
0
}
}
} else {
0
Expand Down Expand Up @@ -1184,6 +1189,10 @@ impl<'tcx> Visitor<'tcx> for FindPanicUnwrap<'_, 'tcx> {

#[expect(clippy::range_plus_one)] // inclusive ranges aren't the same type
fn looks_like_refdef(doc: &str, range: Range<usize>) -> Option<Range<usize>> {
if range.end < range.start {
return None;
}

let offset = range.start;
let mut iterator = doc.as_bytes()[range].iter().copied().enumerate();
let mut start = None;
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/doc/doc_nested_refdef_list_item.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,11 @@ pub struct NotEmpty;
/// - [link]\: notdef
/// inner text
pub struct NotEmptyTight;

/// ## Heading
///
/// - [x][] - Done
//~^ ERROR: link reference defined in list item
/// - [ ][] - Not Done
//~^ ERROR: link reference defined in list item
pub struct GithubCheckboxes;
8 changes: 8 additions & 0 deletions tests/ui/doc/doc_nested_refdef_list_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,11 @@ pub struct NotEmpty;
/// - [link]\: notdef
/// inner text
pub struct NotEmptyTight;

/// ## Heading
///
/// - [x] - Done
//~^ ERROR: link reference defined in list item
/// - [ ] - Not Done
//~^ ERROR: link reference defined in list item
pub struct GithubCheckboxes;
26 changes: 25 additions & 1 deletion tests/ui/doc/doc_nested_refdef_list_item.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,29 @@ help: for an intra-doc link, add `[]` between the label and the colon
LL | /// - [link][]: def "title"
| ++

error: aborting due to 12 previous errors
error: link reference defined in list item
--> tests/ui/doc/doc_nested_refdef_list_item.rs:75:7
|
LL | /// - [x] - Done
| ^^^
|
= help: link definitions are not shown in rendered documentation
help: for an intra-doc link, add `[]` between the label and the colon
|
LL | /// - [x][] - Done
| ++

error: link reference defined in list item
--> tests/ui/doc/doc_nested_refdef_list_item.rs:77:7
|
LL | /// - [ ] - Not Done
| ^^^
|
= help: link definitions are not shown in rendered documentation
help: for an intra-doc link, add `[]` between the label and the colon
|
LL | /// - [ ][] - Not Done
| ++

error: aborting due to 14 previous errors

0 comments on commit 527ab05

Please sign in to comment.