Skip to content

Commit 6163d89

Browse files
Improve code
1 parent 4a3746e commit 6163d89

File tree

3 files changed

+58
-13
lines changed

3 files changed

+58
-13
lines changed

src/librustdoc/passes/html_tags.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,20 @@ fn drop_tag(
4545
range: Range<usize>,
4646
f: &impl Fn(&str, &Range<usize>),
4747
) {
48-
if let Some(pos) = tags.iter().position(|(t, _)| *t == tag_name) {
49-
for _ in pos + 1..tags.len() {
50-
if ALLOWED_UNCLOSED.iter().find(|&at| at == &tags[pos + 1].0).is_some() {
48+
if let Some(pos) = tags.iter().rev().position(|(t, _)| *t == tag_name) {
49+
// Because this is from a `rev` iterator, the position is reversed as well!
50+
let pos = tags.len() - 1 - pos;
51+
for (last_tag_name, last_tag_span) in tags.drain(pos + 1..) {
52+
if ALLOWED_UNCLOSED.iter().any(|&at| at == &last_tag_name) {
5153
continue;
5254
}
5355
// `tags` is used as a queue, meaning that everything after `pos` is included inside it.
5456
// So `<h2><h3></h2>` will look like `["h2", "h3"]`. So when closing `h2`, we will still
5557
// have `h3`, meaning the tag wasn't closed as it should have.
56-
f(&format!("unclosed HTML tag `{}`", tags[pos + 1].0), &tags[pos + 1].1);
57-
tags.remove(pos + 1);
58+
f(&format!("unclosed HTML tag `{}`", last_tag_name), &last_tag_span);
5859
}
59-
tags.remove(pos);
60+
// Remove the `tag_name` that was originally closed
61+
tags.pop();
6062
} else {
6163
// It can happen for example in this case: `<h2></script></h2>` (the `h2` tag isn't required
6264
// but it helps for the visualization).
@@ -84,7 +86,13 @@ fn extract_tag(
8486
tag_name.push(*c);
8587
} else {
8688
if !tag_name.is_empty() {
87-
let r = Range { start: range.start + start_pos, end: range.start + pos };
89+
let mut r =
90+
Range { start: range.start + start_pos, end: range.start + pos };
91+
if *c == '>' {
92+
// In case we have a tag without attribute, we can consider the span to
93+
// refer to it fully.
94+
r.end += 1;
95+
}
8896
if is_closing {
8997
drop_tag(tags, tag_name, r, f);
9098
} else {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,22 @@ pub fn foo() {}
2020
/// </hello>
2121
//~^ ERROR unopened HTML tag `hello`
2222
pub fn f() {}
23+
24+
/// <div>
25+
/// <br/> <p>
26+
//~^ ERROR unclosed HTML tag `p`
27+
/// </div>
28+
pub fn a() {}
29+
30+
/// <div>
31+
/// <p>
32+
/// <div></div>
33+
/// </p>
34+
/// </div>
35+
pub fn b() {}
36+
37+
/// <div style="hello">
38+
//~^ ERROR unclosed HTML tag `div`
39+
/// <h3>
40+
//~^ ERROR unclosed HTML tag `h3`
41+
pub fn c() {}

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: unclosed HTML tag `unknown`
22
--> $DIR/invalid-html-tags.rs:7:5
33
|
44
LL | /// <unknown>
5-
| ^^^^^^^^
5+
| ^^^^^^^^^
66
|
77
note: the lint level is defined here
88
--> $DIR/invalid-html-tags.rs:1:9
@@ -14,25 +14,43 @@ error: unclosed HTML tag `script`
1414
--> $DIR/invalid-html-tags.rs:10:5
1515
|
1616
LL | /// <script>
17-
| ^^^^^^^
17+
| ^^^^^^^^
1818

1919
error: unclosed HTML tag `h2`
2020
--> $DIR/invalid-html-tags.rs:15:7
2121
|
2222
LL | /// <h2>
23-
| ^^^
23+
| ^^^^
2424

2525
error: unclosed HTML tag `h3`
2626
--> $DIR/invalid-html-tags.rs:17:9
2727
|
2828
LL | /// <h3>
29-
| ^^^
29+
| ^^^^
3030

3131
error: unopened HTML tag `hello`
3232
--> $DIR/invalid-html-tags.rs:20:5
3333
|
3434
LL | /// </hello>
35-
| ^^^^^^^
35+
| ^^^^^^^^
36+
37+
error: unclosed HTML tag `p`
38+
--> $DIR/invalid-html-tags.rs:25:14
39+
|
40+
LL | /// <br/> <p>
41+
| ^^^
42+
43+
error: unclosed HTML tag `div`
44+
--> $DIR/invalid-html-tags.rs:37:5
45+
|
46+
LL | /// <div style="hello">
47+
| ^^^^
48+
49+
error: unclosed HTML tag `h3`
50+
--> $DIR/invalid-html-tags.rs:39:7
51+
|
52+
LL | /// <h3>
53+
| ^^^^
3654

37-
error: aborting due to 5 previous errors
55+
error: aborting due to 8 previous errors
3856

0 commit comments

Comments
 (0)