Skip to content

Commit 0009cba

Browse files
Add check for HTML comments
1 parent 861b892 commit 0009cba

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

src/librustdoc/passes/html_tags.rs

+29-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ use crate::core::DocContext;
44
use crate::fold::DocFolder;
55
use crate::html::markdown::opts;
66
use core::ops::Range;
7-
use std::iter::Peekable;
8-
use std::str::CharIndices;
97
use pulldown_cmark::{Event, Parser};
108
use rustc_feature::UnstableFeatures;
119
use rustc_session::lint;
10+
use std::iter::Peekable;
11+
use std::str::CharIndices;
1212

1313
pub const CHECK_INVALID_HTML_TAGS: Pass = Pass {
1414
name: "check-invalid-html-tags",
@@ -104,8 +104,7 @@ fn extract_html_tag(
104104
tag_name.push(c);
105105
} else {
106106
if !tag_name.is_empty() {
107-
let mut r =
108-
Range { start: range.start + start_pos, end: range.start + pos };
107+
let mut r = Range { start: range.start + start_pos, end: range.start + pos };
109108
if c == '>' {
110109
// In case we have a tag without attribute, we can consider the span to
111110
// refer to it fully.
@@ -143,6 +142,27 @@ fn extract_html_tag(
143142
}
144143
}
145144

145+
fn extract_html_comment(
146+
text: &str,
147+
range: &Range<usize>,
148+
start_pos: usize,
149+
iter: &mut Peekable<CharIndices<'_>>,
150+
f: &impl Fn(&str, &Range<usize>),
151+
) {
152+
// We first skip the "!--" part.
153+
let mut iter = iter.skip(3);
154+
while let Some((pos, c)) = iter.next() {
155+
if c == '-' && text[pos..].starts_with("-->") {
156+
// All good, we can leave!
157+
return;
158+
}
159+
}
160+
f(
161+
"Unclosed HTML comment",
162+
&Range { start: range.start + start_pos, end: range.start + start_pos + 3 },
163+
);
164+
}
165+
146166
fn extract_tags(
147167
tags: &mut Vec<(String, Range<usize>)>,
148168
text: &str,
@@ -153,7 +173,11 @@ fn extract_tags(
153173

154174
while let Some((start_pos, c)) = iter.next() {
155175
if c == '<' {
156-
extract_html_tag(tags, text, &range, start_pos, &mut iter, f);
176+
if text[start_pos..].starts_with("<!--") {
177+
extract_html_comment(text, &range, start_pos, &mut iter, f);
178+
} else {
179+
extract_html_tag(tags, text, &range, start_pos, &mut iter, f);
180+
}
157181
}
158182
}
159183
}

src/test/rustdoc-ui/invalid-html-tags.rs

+10
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,13 @@ pub fn e() {}
7373
/// <div></div
7474
//~^ ERROR unclosed HTML tag `div`
7575
pub fn f() {}
76+
77+
/// <!---->
78+
/// <!-- -->
79+
/// <!-- <div> -->
80+
/// <!-- <!-- -->
81+
pub fn g() {}
82+
83+
/// <!--
84+
//~^ ERROR Unclosed HTML comment
85+
pub fn h() {}

src/test/rustdoc-ui/invalid-html-tags.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,11 @@ error: unclosed HTML tag `div`
7676
LL | /// <div></div
7777
| ^^^^^
7878

79-
error: aborting due to 12 previous errors
79+
error: Unclosed HTML comment
80+
--> $DIR/invalid-html-tags.rs:83:5
81+
|
82+
LL | /// <!--
83+
| ^^^
84+
85+
error: aborting due to 13 previous errors
8086

0 commit comments

Comments
 (0)