Skip to content

Commit ca199b1

Browse files
Use char_indices() instead of chars() to prevent more than one-byte characters issue
1 parent 5bc1489 commit ca199b1

File tree

5 files changed

+43
-48
lines changed

5 files changed

+43
-48
lines changed

src/librustdoc/passes/html_tags.rs

+5-16
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ fn drop_tag(
4646
f: &impl Fn(&str, &Range<usize>),
4747
) {
4848
let tag_name_low = tag_name.to_lowercase();
49-
if let Some(pos) = tags.iter().rev().position(|(t, _)| t.to_lowercase() == tag_name_low) {
50-
// Because this is from a `rev` iterator, the position is reversed as well!
51-
let pos = tags.len() - 1 - pos;
49+
if let Some(pos) = tags.iter().rposition(|(t, _)| t.to_lowercase() == tag_name_low) {
5250
// If the tag is nested inside a "<script>" or a "<style>" tag, no warning should
5351
// be emitted.
5452
let should_not_warn = tags.iter().take(pos + 1).any(|(at, _)| {
@@ -83,9 +81,9 @@ fn extract_tag(
8381
range: Range<usize>,
8482
f: &impl Fn(&str, &Range<usize>),
8583
) {
86-
let mut iter = text.chars().enumerate().peekable();
84+
let mut iter = text.char_indices().peekable();
8785

88-
'top: while let Some((start_pos, c)) = iter.next() {
86+
while let Some((start_pos, c)) = iter.next() {
8987
if c == '<' {
9088
let mut tag_name = String::new();
9189
let mut is_closing = false;
@@ -118,20 +116,11 @@ fn extract_tag(
118116
tags.push((tag_name, r));
119117
}
120118
}
121-
continue 'top;
122-
}
123-
// Some chars like 💩 are longer than 1 character, so we need to skip the other
124-
// bytes as well to prevent stopping "in the middle" of a char.
125-
for _ in 0..c.len_utf8() {
126-
iter.next();
119+
break;
127120
}
121+
iter.next();
128122
}
129123
}
130-
// Some chars like 💩 are longer than 1 character, so we need to skip the other
131-
// bytes as well to prevent stopping "in the middle" of a char.
132-
for _ in 0..c.len_utf8() - 1 {
133-
iter.next();
134-
}
135124
}
136125
}
137126

src/test/rustdoc-ui/intra-link-errors.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![allow(invalid_html_tags)]
21
#![deny(broken_intra_doc_links)]
32
//~^ NOTE lint level is defined
43

src/test/rustdoc-ui/intra-link-errors.stderr

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,95 @@
11
error: unresolved link to `path::to::nonexistent::module`
2-
--> $DIR/intra-link-errors.rs:8:6
2+
--> $DIR/intra-link-errors.rs:7:6
33
|
44
LL | /// [path::to::nonexistent::module]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the module `intra_link_errors` contains no item named `path`
66
|
77
note: the lint level is defined here
8-
--> $DIR/intra-link-errors.rs:2:9
8+
--> $DIR/intra-link-errors.rs:1:9
99
|
1010
LL | #![deny(broken_intra_doc_links)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^
1212

1313
error: unresolved link to `path::to::nonexistent::macro`
14-
--> $DIR/intra-link-errors.rs:12:6
14+
--> $DIR/intra-link-errors.rs:11:6
1515
|
1616
LL | /// [path::to::nonexistent::macro!]
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the module `intra_link_errors` contains no item named `path`
1818

1919
error: unresolved link to `path::to::nonexistent::type`
20-
--> $DIR/intra-link-errors.rs:16:6
20+
--> $DIR/intra-link-errors.rs:15:6
2121
|
2222
LL | /// [type@path::to::nonexistent::type]
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the module `intra_link_errors` contains no item named `path`
2424

2525
error: unresolved link to `std::io::not::here`
26-
--> $DIR/intra-link-errors.rs:20:6
26+
--> $DIR/intra-link-errors.rs:19:6
2727
|
2828
LL | /// [std::io::not::here]
2929
| ^^^^^^^^^^^^^^^^^^ the module `io` contains no item named `not`
3030

3131
error: unresolved link to `std::io::not::here`
32-
--> $DIR/intra-link-errors.rs:24:6
32+
--> $DIR/intra-link-errors.rs:23:6
3333
|
3434
LL | /// [type@std::io::not::here]
3535
| ^^^^^^^^^^^^^^^^^^^^^^^ the module `io` contains no item named `not`
3636

3737
error: unresolved link to `std::io::Error::x`
38-
--> $DIR/intra-link-errors.rs:28:6
38+
--> $DIR/intra-link-errors.rs:27:6
3939
|
4040
LL | /// [std::io::Error::x]
4141
| ^^^^^^^^^^^^^^^^^ the struct `Error` has no field or associated item named `x`
4242

4343
error: unresolved link to `std::io::ErrorKind::x`
44-
--> $DIR/intra-link-errors.rs:32:6
44+
--> $DIR/intra-link-errors.rs:31:6
4545
|
4646
LL | /// [std::io::ErrorKind::x]
4747
| ^^^^^^^^^^^^^^^^^^^^^ the enum `ErrorKind` has no variant or associated item named `x`
4848

4949
error: unresolved link to `f::A`
50-
--> $DIR/intra-link-errors.rs:36:6
50+
--> $DIR/intra-link-errors.rs:35:6
5151
|
5252
LL | /// [f::A]
5353
| ^^^^ `f` is a function, not a module or type, and cannot have associated items
5454

5555
error: unresolved link to `f::A`
56-
--> $DIR/intra-link-errors.rs:40:6
56+
--> $DIR/intra-link-errors.rs:39:6
5757
|
5858
LL | /// [f::A!]
5959
| ^^^^^ `f` is a function, not a module or type, and cannot have associated items
6060

6161
error: unresolved link to `S::A`
62-
--> $DIR/intra-link-errors.rs:44:6
62+
--> $DIR/intra-link-errors.rs:43:6
6363
|
6464
LL | /// [S::A]
6565
| ^^^^ the struct `S` has no field or associated item named `A`
6666

6767
error: unresolved link to `S::fmt`
68-
--> $DIR/intra-link-errors.rs:48:6
68+
--> $DIR/intra-link-errors.rs:47:6
6969
|
7070
LL | /// [S::fmt]
7171
| ^^^^^^ the struct `S` has no field or associated item named `fmt`
7272

7373
error: unresolved link to `E::D`
74-
--> $DIR/intra-link-errors.rs:52:6
74+
--> $DIR/intra-link-errors.rs:51:6
7575
|
7676
LL | /// [E::D]
7777
| ^^^^ the enum `E` has no variant or associated item named `D`
7878

7979
error: unresolved link to `u8::not_found`
80-
--> $DIR/intra-link-errors.rs:56:6
80+
--> $DIR/intra-link-errors.rs:55:6
8181
|
8282
LL | /// [u8::not_found]
8383
| ^^^^^^^^^^^^^ the builtin type `u8` has no associated item named `not_found`
8484

8585
error: unresolved link to `std::primitive::u8::not_found`
86-
--> $DIR/intra-link-errors.rs:60:6
86+
--> $DIR/intra-link-errors.rs:59:6
8787
|
8888
LL | /// [std::primitive::u8::not_found]
8989
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the builtin type `u8` has no associated item named `not_found`
9090

9191
error: unresolved link to `Vec::into_iter`
92-
--> $DIR/intra-link-errors.rs:64:6
92+
--> $DIR/intra-link-errors.rs:63:6
9393
|
9494
LL | /// [type@Vec::into_iter]
9595
| ^^^^^^^^^^^^^^^^^^^
@@ -98,7 +98,7 @@ LL | /// [type@Vec::into_iter]
9898
| help: to link to the associated function, add parentheses: `Vec::into_iter()`
9999

100100
error: unresolved link to `S`
101-
--> $DIR/intra-link-errors.rs:69:6
101+
--> $DIR/intra-link-errors.rs:68:6
102102
|
103103
LL | /// [S!]
104104
| ^^
@@ -107,7 +107,7 @@ LL | /// [S!]
107107
| help: to link to the struct, prefix with `struct@`: `struct@S`
108108

109109
error: unresolved link to `T::g`
110-
--> $DIR/intra-link-errors.rs:87:6
110+
--> $DIR/intra-link-errors.rs:86:6
111111
|
112112
LL | /// [type@T::g]
113113
| ^^^^^^^^^
@@ -116,13 +116,13 @@ LL | /// [type@T::g]
116116
| help: to link to the associated function, add parentheses: `T::g()`
117117

118118
error: unresolved link to `T::h`
119-
--> $DIR/intra-link-errors.rs:92:6
119+
--> $DIR/intra-link-errors.rs:91:6
120120
|
121121
LL | /// [T::h!]
122122
| ^^^^^ the trait `T` has no macro named `h`
123123

124124
error: unresolved link to `S::h`
125-
--> $DIR/intra-link-errors.rs:79:6
125+
--> $DIR/intra-link-errors.rs:78:6
126126
|
127127
LL | /// [type@S::h]
128128
| ^^^^^^^^^
@@ -131,7 +131,7 @@ LL | /// [type@S::h]
131131
| help: to link to the associated function, add parentheses: `S::h()`
132132

133133
error: unresolved link to `m`
134-
--> $DIR/intra-link-errors.rs:99:6
134+
--> $DIR/intra-link-errors.rs:98:6
135135
|
136136
LL | /// [m()]
137137
| ^^^

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

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
//! <p>💩<p>
44
//~^ ERROR unclosed HTML tag `p`
5+
//~^^ ERROR unclosed HTML tag `p`
56

67
/// <img><input>
78
/// <script>

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

+16-10
Original file line numberDiff line numberDiff line change
@@ -10,59 +10,65 @@ note: the lint level is defined here
1010
LL | #![deny(invalid_html_tags)]
1111
| ^^^^^^^^^^^^^^^^^
1212

13+
error: unclosed HTML tag `p`
14+
--> $DIR/invalid-html-tags.rs:3:9
15+
|
16+
LL | //! <p>💩<p>
17+
| ^^^
18+
1319
error: unclosed HTML tag `unknown`
14-
--> $DIR/invalid-html-tags.rs:10:5
20+
--> $DIR/invalid-html-tags.rs:11:5
1521
|
1622
LL | /// <unknown>
1723
| ^^^^^^^^^
1824

1925
error: unclosed HTML tag `script`
20-
--> $DIR/invalid-html-tags.rs:13:5
26+
--> $DIR/invalid-html-tags.rs:14:5
2127
|
2228
LL | /// <script>
2329
| ^^^^^^^^
2430

2531
error: unclosed HTML tag `h2`
26-
--> $DIR/invalid-html-tags.rs:18:7
32+
--> $DIR/invalid-html-tags.rs:19:7
2733
|
2834
LL | /// <h2>
2935
| ^^^^
3036

3137
error: unclosed HTML tag `h3`
32-
--> $DIR/invalid-html-tags.rs:20:9
38+
--> $DIR/invalid-html-tags.rs:21:9
3339
|
3440
LL | /// <h3>
3541
| ^^^^
3642

3743
error: unopened HTML tag `hello`
38-
--> $DIR/invalid-html-tags.rs:23:5
44+
--> $DIR/invalid-html-tags.rs:24:5
3945
|
4046
LL | /// </hello>
4147
| ^^^^^^^^
4248

4349
error: unclosed HTML tag `p`
44-
--> $DIR/invalid-html-tags.rs:28:14
50+
--> $DIR/invalid-html-tags.rs:29:14
4551
|
4652
LL | /// <br/> <p>
4753
| ^^^
4854

4955
error: unclosed HTML tag `div`
50-
--> $DIR/invalid-html-tags.rs:40:5
56+
--> $DIR/invalid-html-tags.rs:41:5
5157
|
5258
LL | /// <div style="hello">
5359
| ^^^^
5460

5561
error: unclosed HTML tag `h3`
56-
--> $DIR/invalid-html-tags.rs:42:7
62+
--> $DIR/invalid-html-tags.rs:43:7
5763
|
5864
LL | /// <h3>
5965
| ^^^^
6066

6167
error: unclosed HTML tag `script`
62-
--> $DIR/invalid-html-tags.rs:44:5
68+
--> $DIR/invalid-html-tags.rs:45:5
6369
|
6470
LL | /// <script
6571
| ^^^^^^
6672

67-
error: aborting due to 10 previous errors
73+
error: aborting due to 11 previous errors
6874

0 commit comments

Comments
 (0)