Skip to content
This repository was archived by the owner on Nov 24, 2023. It is now read-only.

Commit 836108f

Browse files
authored
Merge pull request #167 from phansch/fix_string_indexing
Fix string indexing
2 parents e292079 + 0cc670d commit 836108f

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,15 @@ fn parse_snippet(span: &DiagnosticSpan) -> Option<Snippet> {
104104
std::cmp::min(indent, line.highlight_start)
105105
})
106106
.min()?;
107+
108+
let text_slice = span.text[0].text.chars().collect::<Vec<char>>();
109+
110+
// We subtract `1` because these highlights are 1-based
107111
let start = span.text[0].highlight_start - 1;
108112
let end = span.text[0].highlight_end - 1;
109-
let lead = span.text[0].text[indent..start].to_string();
110-
let mut body = span.text[0].text[start..end].to_string();
113+
let lead = text_slice[indent..start].iter().collect();
114+
let mut body: String = text_slice[start..end].iter().collect();
115+
111116
for line in span.text.iter().take(span.text.len() - 1).skip(1) {
112117
body.push('\n');
113118
body.push_str(&line.text[indent..]);
@@ -118,12 +123,13 @@ fn parse_snippet(span: &DiagnosticSpan) -> Option<Snippet> {
118123
// If we get a DiagnosticSpanLine where highlight_end > text.len(), we prevent an 'out of
119124
// bounds' access by making sure the index is within the array bounds.
120125
let last_tail_index = last.highlight_end.min(last.text.len()) - 1;
126+
let last_slice = last.text.chars().collect::<Vec<char>>();
121127

122128
if span.text.len() > 1 {
123129
body.push('\n');
124-
body.push_str(&last.text[indent..last_tail_index]);
130+
body.push_str(&last_slice[indent..last_tail_index].iter().collect::<String>());
125131
}
126-
tail.push_str(&last.text[last_tail_index..]);
132+
tail.push_str(&last_slice[last_tail_index..].iter().collect::<String>());
127133
Some(Snippet {
128134
file_name: span.file_name.clone(),
129135
line_range: LineRange {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"message": "expected one of `,`, `:`, `=`, or `>`, found `'β`",
3+
"code": null,
4+
"level": "error",
5+
"spans": [
6+
{
7+
"file_name": "./tests/everything/utf8_idents.rs",
8+
"byte_start": 14,
9+
"byte_end": 14,
10+
"line_start": 2,
11+
"line_end": 2,
12+
"column_start": 6,
13+
"column_end": 6,
14+
"is_primary": false,
15+
"text": [
16+
{
17+
"text": " γ //~ ERROR non-ascii idents are not fully supported",
18+
"highlight_start": 6,
19+
"highlight_end": 6
20+
}
21+
],
22+
"label": "expected one of `,`, `:`, `=`, or `>` here",
23+
"suggested_replacement": null,
24+
"suggestion_applicability": null,
25+
"expansion": null
26+
},
27+
{
28+
"file_name": "./tests/everything/utf8_idents.rs",
29+
"byte_start": 145,
30+
"byte_end": 148,
31+
"line_start": 4,
32+
"line_end": 4,
33+
"column_start": 5,
34+
"column_end": 7,
35+
"is_primary": true,
36+
"text": [
37+
{
38+
"text": " 'β, //~ ERROR non-ascii idents are not fully supported",
39+
"highlight_start": 5,
40+
"highlight_end": 7
41+
}
42+
],
43+
"label": "unexpected token",
44+
"suggested_replacement": null,
45+
"suggestion_applicability": null,
46+
"expansion": null
47+
}
48+
],
49+
"children": [],
50+
"rendered": "error: expected one of `,`, `:`, `=`, or `>`, found `'β`\n --> ./tests/everything/utf8_idents.rs:4:5\n |\n2 | γ //~ ERROR non-ascii idents are not fully supported\n | - expected one of `,`, `:`, `=`, or `>` here\n3 | //~^ WARN type parameter `γ` should have an upper camel case name\n4 | 'β, //~ ERROR non-ascii idents are not fully supported\n | ^^ unexpected token\n\n"
51+
}
52+
{
53+
"message": "aborting due to previous error",
54+
"code": null,
55+
"level": "error",
56+
"spans": [],
57+
"children": [],
58+
"rendered": "error: aborting due to previous error\n\n"
59+
}

tests/edge_cases.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,12 @@ fn out_of_bounds_test() {
1919
.unwrap();
2020
assert!(expected_suggestions.is_empty());
2121
}
22+
23+
#[test]
24+
fn utf8_identifiers_test() {
25+
let json = fs::read_to_string("./tests/edge-cases/utf8_idents.recorded.json").unwrap();
26+
let expected_suggestions =
27+
rustfix::get_suggestions_from_json(&json, &HashSet::new(), rustfix::Filter::Everything)
28+
.unwrap();
29+
assert!(expected_suggestions.is_empty());
30+
}

0 commit comments

Comments
 (0)