Skip to content

Commit 1573d10

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 1573d10

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

clippy_lints/src/tabs_in_doc_comments.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,30 +104,29 @@ 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+
for entry in char_indices.windows(2) {
114+
match entry {
115+
[(_, '\t'), (_, '\t')] => {
117116
// either string starts with double tab, then we have to set it active,
118117
// otherwise is_active is true anyway
119118
is_active = true;
120119
},
121-
[_, '\t'] => {
120+
[(_, _), (index_b, '\t')] => {
122121
// as ['\t', '\t'] is excluded, this has to be a start of a tab group,
123122
// set indices accordingly
124123
is_active = true;
125-
current_start = index + 1;
124+
current_start = *index_b as u32;
126125
},
127-
['\t', _] => {
126+
[(_, '\t'), (index_b, _)] => {
128127
// this now has to be an end of the group, hence we have to push a new tuple
129128
is_active = false;
130-
spans.push((current_start, index + 1));
129+
spans.push((current_start, *index_b as u32));
131130
},
132131
_ => {},
133132
}
@@ -137,7 +136,7 @@ fn get_chunks_of_tabs(the_str: &str) -> Vec<(u32, u32)> {
137136
if is_active {
138137
spans.push((
139138
current_start,
140-
u32::try_from(the_str.chars().count()).expect(line_length_way_to_long),
139+
u32::try_from(char_indices.last().unwrap().0 + 1).expect(line_length_way_to_long),
141140
));
142141
}
143142

@@ -148,6 +147,13 @@ fn get_chunks_of_tabs(the_str: &str) -> Vec<(u32, u32)> {
148147
mod tests_for_get_chunks_of_tabs {
149148
use super::get_chunks_of_tabs;
150149

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

tests/ui/crashes/ice-5835.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![rustfmt::skip]
2+
3+
pub struct Foo {
4+
/// 位
5+
pub bar: u8,
6+
}
7+
8+
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[E0658]: custom inner attributes are unstable
2+
--> $DIR/ice-5835.rs:1:4
3+
|
4+
LL | #![rustfmt::skip]
5+
| ^^^^^^^^^^^^^
6+
|
7+
= note: see issue #54726 <https://github.com/rust-lang/rust/issues/54726> for more information
8+
= help: add `#![feature(custom_inner_attributes)]` to the crate attributes to enable
9+
10+
error: using tabs in doc comments is not recommended
11+
--> $DIR/ice-5835.rs:4:10
12+
|
13+
LL | /// 位
14+
| ^^^^ help: consider using four spaces per tab
15+
|
16+
= note: `-D clippy::tabs-in-doc-comments` implied by `-D warnings`
17+
18+
error: aborting due to 2 previous errors
19+
20+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)