Skip to content

Commit 527ab05

Browse files
authored
fix: Avoid ICE in doc_nested_refdefs check by checking range (rust-lang#14308)
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 rust-lang#14287 by avoiding invalid ranges
2 parents f50266a + e399e15 commit 527ab05

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

clippy_lints/src/doc/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,12 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
10041004
// backslashes aren't in the event stream...
10051005
start -= 1;
10061006
}
1007-
start - range.start
1007+
1008+
if start > range.start {
1009+
start - range.start
1010+
} else {
1011+
0
1012+
}
10081013
}
10091014
} else {
10101015
0
@@ -1184,6 +1189,10 @@ impl<'tcx> Visitor<'tcx> for FindPanicUnwrap<'_, 'tcx> {
11841189

11851190
#[expect(clippy::range_plus_one)] // inclusive ranges aren't the same type
11861191
fn looks_like_refdef(doc: &str, range: Range<usize>) -> Option<Range<usize>> {
1192+
if range.end < range.start {
1193+
return None;
1194+
}
1195+
11871196
let offset = range.start;
11881197
let mut iterator = doc.as_bytes()[range].iter().copied().enumerate();
11891198
let mut start = None;

tests/ui/doc/doc_nested_refdef_list_item.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,11 @@ pub struct NotEmpty;
6969
/// - [link]\: notdef
7070
/// inner text
7171
pub struct NotEmptyTight;
72+
73+
/// ## Heading
74+
///
75+
/// - [x][] - Done
76+
//~^ ERROR: link reference defined in list item
77+
/// - [ ][] - Not Done
78+
//~^ ERROR: link reference defined in list item
79+
pub struct GithubCheckboxes;

tests/ui/doc/doc_nested_refdef_list_item.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,11 @@ pub struct NotEmpty;
6969
/// - [link]\: notdef
7070
/// inner text
7171
pub struct NotEmptyTight;
72+
73+
/// ## Heading
74+
///
75+
/// - [x] - Done
76+
//~^ ERROR: link reference defined in list item
77+
/// - [ ] - Not Done
78+
//~^ ERROR: link reference defined in list item
79+
pub struct GithubCheckboxes;

tests/ui/doc/doc_nested_refdef_list_item.stderr

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,29 @@ help: for an intra-doc link, add `[]` between the label and the colon
144144
LL | /// - [link][]: def "title"
145145
| ++
146146

147-
error: aborting due to 12 previous errors
147+
error: link reference defined in list item
148+
--> tests/ui/doc/doc_nested_refdef_list_item.rs:75:7
149+
|
150+
LL | /// - [x] - Done
151+
| ^^^
152+
|
153+
= help: link definitions are not shown in rendered documentation
154+
help: for an intra-doc link, add `[]` between the label and the colon
155+
|
156+
LL | /// - [x][] - Done
157+
| ++
158+
159+
error: link reference defined in list item
160+
--> tests/ui/doc/doc_nested_refdef_list_item.rs:77:7
161+
|
162+
LL | /// - [ ] - Not Done
163+
| ^^^
164+
|
165+
= help: link definitions are not shown in rendered documentation
166+
help: for an intra-doc link, add `[]` between the label and the colon
167+
|
168+
LL | /// - [ ][] - Not Done
169+
| ++
170+
171+
error: aborting due to 14 previous errors
148172

0 commit comments

Comments
 (0)