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

Commit 892a95c

Browse files
committed
Don't panic on span for empty file.
1 parent f3f1035 commit 892a95c

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ fn parse_snippet(span: &DiagnosticSpan) -> Option<Snippet> {
122122

123123
// If we get a DiagnosticSpanLine where highlight_end > text.len(), we prevent an 'out of
124124
// bounds' access by making sure the index is within the array bounds.
125-
let last_tail_index = last.highlight_end.min(last.text.len()) - 1;
125+
// `saturating_sub` is used in case of an empty file
126+
let last_tail_index = last.highlight_end.min(last.text.len()).saturating_sub(1);
126127
let last_slice = last.text.chars().collect::<Vec<char>>();
127128

128129
if span.text.len() > 1 {

tests/edge-cases/empty.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"message": "`main` function not found in crate `empty`",
3+
"code": {
4+
"code": "E0601",
5+
"explanation": "No `main` function was found in a binary crate. To fix this error, add a\n`main` function. For example:\n\n```\nfn main() {\n // Your program will start here.\n println!(\"Hello world!\");\n}\n```\n\nIf you don't know the basics of Rust, you can go look to the Rust Book to get\nstarted: https://doc.rust-lang.org/book/\n"
6+
},
7+
"level": "error",
8+
"spans": [
9+
{
10+
"file_name": "empty.rs",
11+
"byte_start": 0,
12+
"byte_end": 0,
13+
"line_start": 0,
14+
"line_end": 0,
15+
"column_start": 1,
16+
"column_end": 1,
17+
"is_primary": true,
18+
"text": [
19+
{
20+
"text": "",
21+
"highlight_start": 1,
22+
"highlight_end": 1
23+
}
24+
],
25+
"label": null,
26+
"suggested_replacement": null,
27+
"suggestion_applicability": null,
28+
"expansion": null
29+
}
30+
],
31+
"children": [
32+
{
33+
"message": "consider adding a `main` function to `empty.rs`",
34+
"code": null,
35+
"level": "note",
36+
"spans": [],
37+
"children": [],
38+
"rendered": null
39+
}
40+
],
41+
"rendered": "error[E0601]: `main` function not found in crate `empty`\n |\n = note: consider adding a `main` function to `empty.rs`\n\n"
42+
}

tests/edge-cases/empty.rs

Whitespace-only changes.

tests/edge_cases.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ macro_rules! expect_empty_json_test {
2121
expect_empty_json_test! {multiple_fix_options_yield_no_suggestions, "skip-multi-option-lints.json"}
2222
expect_empty_json_test! {out_of_bounds_test, "out_of_bounds.recorded.json"}
2323
expect_empty_json_test! {utf8_identifiers_test, "utf8_idents.recorded.json"}
24+
expect_empty_json_test! {empty, "empty.json"}

0 commit comments

Comments
 (0)