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

Commit df8401b

Browse files
committed
Prevent panic when span points past end of line.
1 parent d244f66 commit df8401b

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

src/lib.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,13 @@ fn parse_snippet(span: &DiagnosticSpan) -> Option<Snippet> {
108108
let text_slice = span.text[0].text.chars().collect::<Vec<char>>();
109109

110110
// We subtract `1` because these highlights are 1-based
111-
let start = span.text[0].highlight_start - 1;
112-
let end = span.text[0].highlight_end - 1;
111+
// Check the `min` so that it doesn't attempt to index out-of-bounds when
112+
// the span points to the "end" of the line. For example, a line of
113+
// "foo\n" with a highlight_start of 5 is intended to highlight *after*
114+
// the line. This needs to compensate since the newline has been removed
115+
// from the text slice.
116+
let start = (span.text[0].highlight_start - 1).min(text_slice.len());
117+
let end = (span.text[0].highlight_end - 1).min(text_slice.len());
113118
let lead = text_slice[indent..start].iter().collect();
114119
let mut body: String = text_slice[start..end].iter().collect();
115120

tests/edge-cases/no_main.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"message": "`main` function not found in crate `no_main`",
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": "no_main.rs",
11+
"byte_start": 26,
12+
"byte_end": 26,
13+
"line_start": 1,
14+
"line_end": 1,
15+
"column_start": 27,
16+
"column_end": 27,
17+
"is_primary": true,
18+
"text": [
19+
{
20+
"text": "// This file has no main.",
21+
"highlight_start": 27,
22+
"highlight_end": 27
23+
}
24+
],
25+
"label": "consider adding a `main` function to `no_main.rs`",
26+
"suggested_replacement": null,
27+
"suggestion_applicability": null,
28+
"expansion": null
29+
}
30+
],
31+
"children": [],
32+
"rendered": "error[E0601]: `main` function not found in crate `no_main`\n --> no_main.rs:1:27\n |\n1 | // This file has no main.\n | ^ consider adding a `main` function to `no_main.rs`\n\n"
33+
}

tests/edge-cases/no_main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// This file has no main.

tests/edge_cases.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ expect_empty_json_test! {multiple_fix_options_yield_no_suggestions, "skip-multi-
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"}
2424
expect_empty_json_test! {empty, "empty.json"}
25+
expect_empty_json_test! {no_main, "no_main.json"}

0 commit comments

Comments
 (0)