Skip to content

Commit 24921df

Browse files
committed
Auto merge of #7039 - phansch:melt-ice, r=flip1995
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. But I don't think we handle those cases anywhere in Clippy currently? Fixes #5835 changelog: Fix ICE in `tabs_in_doc_comments`
2 parents 19740d9 + cbdebd9 commit 24921df

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

clippy_lints/src/tabs_in_doc_comments.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,30 +104,32 @@ 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+
// Note that we specifically need the char _byte_ indices here, not the positional indexes
108+
// within the char array to deal with multi-byte characters properly. `char_indices` does
109+
// exactly that. It provides an iterator over tuples of the form `(byte position, char)`.
110+
let char_indices: Vec<_> = the_str.char_indices().collect();
108111

109-
if chars_array == vec!['\t'] {
112+
if let [(_, '\t')] = char_indices.as_slice() {
110113
return vec![(0, 1)];
111114
}
112115

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'] => {
116+
for entry in char_indices.windows(2) {
117+
match entry {
118+
[(_, '\t'), (_, '\t')] => {
117119
// either string starts with double tab, then we have to set it active,
118120
// otherwise is_active is true anyway
119121
is_active = true;
120122
},
121-
[_, '\t'] => {
123+
[(_, _), (index_b, '\t')] => {
122124
// as ['\t', '\t'] is excluded, this has to be a start of a tab group,
123125
// set indices accordingly
124126
is_active = true;
125-
current_start = index + 1;
127+
current_start = u32::try_from(*index_b).unwrap();
126128
},
127-
['\t', _] => {
129+
[(_, '\t'), (index_b, _)] => {
128130
// this now has to be an end of the group, hence we have to push a new tuple
129131
is_active = false;
130-
spans.push((current_start, index + 1));
132+
spans.push((current_start, u32::try_from(*index_b).unwrap()));
131133
},
132134
_ => {},
133135
}
@@ -137,7 +139,7 @@ fn get_chunks_of_tabs(the_str: &str) -> Vec<(u32, u32)> {
137139
if is_active {
138140
spans.push((
139141
current_start,
140-
u32::try_from(the_str.chars().count()).expect(line_length_way_to_long),
142+
u32::try_from(char_indices.last().unwrap().0 + 1).expect(line_length_way_to_long),
141143
));
142144
}
143145

@@ -148,6 +150,13 @@ fn get_chunks_of_tabs(the_str: &str) -> Vec<(u32, u32)> {
148150
mod tests_for_get_chunks_of_tabs {
149151
use super::get_chunks_of_tabs;
150152

153+
#[test]
154+
fn test_unicode_han_string() {
155+
let res = get_chunks_of_tabs(" \u{4f4d}\t");
156+
157+
assert_eq!(res, vec![(4, 5)]);
158+
}
159+
151160
#[test]
152161
fn test_empty_string() {
153162
let res = get_chunks_of_tabs("");

tests/ui/crashes/ice-5835.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#[rustfmt::skip]
2+
pub struct Foo {
3+
/// 位
4+
/// ^ Do not remove this tab character.
5+
/// It was required to trigger the ICE.
6+
pub bar: u8,
7+
}
8+
9+
fn main() {}

tests/ui/crashes/ice-5835.stderr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: using tabs in doc comments is not recommended
2+
--> $DIR/ice-5835.rs:3: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: aborting due to previous error
10+

0 commit comments

Comments
 (0)