Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit f70ce00

Browse files
authored
Merge pull request rust-lang#3479 from sinkuu/issue_2995
Fix indexing panic on unicode whitespaces
2 parents b4b80b6 + c0ff894 commit f70ce00

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

src/lists.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ pub fn get_comment_end(
651651
if let Some(i) = block_open_index {
652652
match post_snippet.find('/') {
653653
Some(j) if j < i => block_open_index = None,
654-
_ if i > 0 && &post_snippet[i - 1..i] == "/" => block_open_index = None,
654+
_ if post_snippet[..i].chars().last() == Some('/') => block_open_index = None,
655655
_ => (),
656656
}
657657
}
@@ -692,8 +692,13 @@ pub fn has_extra_newline(post_snippet: &str, comment_end: usize) -> bool {
692692
return false;
693693
}
694694

695+
let len_last = post_snippet[..comment_end]
696+
.chars()
697+
.last()
698+
.unwrap()
699+
.len_utf8();
695700
// Everything from the separator to the next item.
696-
let test_snippet = &post_snippet[comment_end - 1..];
701+
let test_snippet = &post_snippet[comment_end - len_last..];
697702
let first_newline = test_snippet
698703
.find('\n')
699704
.unwrap_or_else(|| test_snippet.len());

tests/source/issue-2995.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn issue_2995() {
2+
// '\u{2028}' is inserted in the code below.
3+
4+
[0,1];
5+
[0, 
/* */ 1];
6+
[0,1];
7+
}

tests/target/issue-2995.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn issue_2995() {
2+
// '\u{2028}' is inserted in the code below.
3+
4+
[0, 1];
5+
[0, /* */ 1];
6+
[0, 1];
7+
}

0 commit comments

Comments
 (0)