Skip to content

Commit 136073e

Browse files
committed
tabs_in_doc_comments: Fix ICE due to char indexing
This is a quick-fix for an ICE in `tabs_in_doc_comments`. The problem was that we we're indexing into possibly multi-byte characters, such as '位'. More specifically `get_chunks_of_tabs` was returning indices into multi-byte characters. Those were passed on to a `Span` creation that then caused the ICE. This fix makes sure that we don't return indices that point inside a multi-byte character. *However*, we are still iterating over unicode codepoints, not grapheme clusters. So a seemingly single character like y̆ , which actually consists of two codepoints, will probably still cause incorrect spans in the output.
1 parent e315437 commit 136073e

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

clippy_lints/src/tabs_in_doc_comments.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,30 +104,30 @@ fn get_chunks_of_tabs(the_str: &str) -> Vec<(u32, u32)> {
104104
// tracker to decide if the last group of tabs is not closed by a non-tab character
105105
let mut is_active = false;
106106

107-
let chars_array: Vec<_> = the_str.chars().collect();
107+
let char_indices: Vec<_> = the_str.char_indices().collect();
108108

109-
if chars_array == vec!['\t'] {
109+
if char_indices.len() == 1 && char_indices.first().unwrap().1 == '\t' {
110110
return vec![(0, 1)];
111111
}
112112

113-
for (index, arr) in chars_array.windows(2).enumerate() {
114-
let index = u32::try_from(index).expect(line_length_way_to_long);
115-
match arr {
116-
['\t', '\t'] => {
113+
114+
for entry in char_indices.windows(2) {
115+
match entry {
116+
[(_, '\t'), (_, '\t')] => {
117117
// either string starts with double tab, then we have to set it active,
118118
// otherwise is_active is true anyway
119119
is_active = true;
120120
},
121-
[_, '\t'] => {
121+
[(_, _), (index_b, '\t')] => {
122122
// as ['\t', '\t'] is excluded, this has to be a start of a tab group,
123123
// set indices accordingly
124124
is_active = true;
125-
current_start = index + 1;
125+
current_start = *index_b as u32;
126126
},
127-
['\t', _] => {
127+
[(_, '\t'), (index_b, _)] => {
128128
// this now has to be an end of the group, hence we have to push a new tuple
129129
is_active = false;
130-
spans.push((current_start, index + 1));
130+
spans.push((current_start, *index_b as u32));
131131
},
132132
_ => {},
133133
}
@@ -137,7 +137,7 @@ fn get_chunks_of_tabs(the_str: &str) -> Vec<(u32, u32)> {
137137
if is_active {
138138
spans.push((
139139
current_start,
140-
u32::try_from(the_str.chars().count()).expect(line_length_way_to_long),
140+
u32::try_from(char_indices.last().unwrap().0 + 1).expect(line_length_way_to_long),
141141
));
142142
}
143143

@@ -148,6 +148,13 @@ fn get_chunks_of_tabs(the_str: &str) -> Vec<(u32, u32)> {
148148
mod tests_for_get_chunks_of_tabs {
149149
use super::get_chunks_of_tabs;
150150

151+
#[test]
152+
fn test_unicode_han_string() {
153+
let res = get_chunks_of_tabs(" 位\t");
154+
155+
assert_eq!(res, vec![(4, 5)]);
156+
}
157+
151158
#[test]
152159
fn test_empty_string() {
153160
let res = get_chunks_of_tabs("");

tests/ui/crashes/ice-5835.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub struct Foo {
2+
/// 位
3+
pub bar: u8,
4+
}
5+
6+
fn main() {}

tests/ui/crashes/ice-5835.stderr

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: using tabs in doc comments is not recommended
2+
--> $DIR/ice-5835.rs:2:10
3+
|
4+
LL | /// 位
5+
| ^^^^ help: consider using four spaces per tab
6+
|
7+
= note: `-D clippy::tabs-in-doc-comments` implied by `-D warnings`
8+
9+
error[E0601]: `main` function not found in crate `ice_5835`
10+
--> $DIR/ice-5835.rs:1:1
11+
|
12+
LL | / pub struct Foo {
13+
LL | | /// 位
14+
LL | | pub bar: u8,
15+
LL | | }
16+
| |_^ consider adding a `main` function to `$DIR/ice-5835.rs`
17+
18+
error: aborting due to 2 previous errors
19+
20+
For more information about this error, try `rustc --explain E0601`.

0 commit comments

Comments
 (0)