Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tabs_in_doc_comments: Fix ICE due to char indexing #7039

Merged
merged 5 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions clippy_lints/src/tabs_in_doc_comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,30 +104,29 @@ fn get_chunks_of_tabs(the_str: &str) -> Vec<(u32, u32)> {
// tracker to decide if the last group of tabs is not closed by a non-tab character
let mut is_active = false;

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

if chars_array == vec!['\t'] {
if let [(_, '\t')] = char_indices.as_slice() {
return vec![(0, 1)];
}

for (index, arr) in chars_array.windows(2).enumerate() {
let index = u32::try_from(index).expect(line_length_way_to_long);
match arr {
['\t', '\t'] => {
for entry in char_indices.windows(2) {
match entry {
[(_, '\t'), (_, '\t')] => {
// either string starts with double tab, then we have to set it active,
// otherwise is_active is true anyway
is_active = true;
},
[_, '\t'] => {
[(_, _), (index_b, '\t')] => {
// as ['\t', '\t'] is excluded, this has to be a start of a tab group,
// set indices accordingly
is_active = true;
current_start = index + 1;
current_start = u32::try_from(*index_b).unwrap();
},
['\t', _] => {
[(_, '\t'), (index_b, _)] => {
// this now has to be an end of the group, hence we have to push a new tuple
is_active = false;
spans.push((current_start, index + 1));
spans.push((current_start, u32::try_from(*index_b).unwrap()));
},
_ => {},
}
Expand All @@ -137,7 +136,7 @@ fn get_chunks_of_tabs(the_str: &str) -> Vec<(u32, u32)> {
if is_active {
spans.push((
current_start,
u32::try_from(the_str.chars().count()).expect(line_length_way_to_long),
u32::try_from(char_indices.last().unwrap().0 + 1).expect(line_length_way_to_long),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this just

Suggested change
u32::try_from(char_indices.last().unwrap().0 + 1).expect(line_length_way_to_long),
u32::try_from(char_indices.len()).expect(line_length_way_to_long),

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. char_indicies is an iterator over tuples with (byte position, char) and using the length of the iterator would produce the same result as the previous code. The suggestion would only work if all characters are 1-byte characters.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah can you add a comment to that line than, that the first part is the byte position and not the character index? Otherwise r=me

));
}

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

#[test]
fn test_unicode_han_string() {
let res = get_chunks_of_tabs(" \u{4f4d}\t");

assert_eq!(res, vec![(4, 5)]);
}

#[test]
fn test_empty_string() {
let res = get_chunks_of_tabs("");
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/crashes/ice-5835.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[rustfmt::skip]
pub struct Foo {
/// 位
phansch marked this conversation as resolved.
Show resolved Hide resolved
/// ^ Do not remove this tab character.
/// It was required to trigger the ICE.
pub bar: u8,
}

fn main() {}
10 changes: 10 additions & 0 deletions tests/ui/crashes/ice-5835.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: using tabs in doc comments is not recommended
--> $DIR/ice-5835.rs:3:10
|
LL | /// 位
| ^^^^ help: consider using four spaces per tab
|
= note: `-D clippy::tabs-in-doc-comments` implied by `-D warnings`

error: aborting due to previous error