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

Commit

Permalink
Prevent panic when span points past end of line.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Apr 2, 2020
1 parent d244f66 commit df8401b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,13 @@ fn parse_snippet(span: &DiagnosticSpan) -> Option<Snippet> {
let text_slice = span.text[0].text.chars().collect::<Vec<char>>();

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

Expand Down
33 changes: 33 additions & 0 deletions tests/edge-cases/no_main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"message": "`main` function not found in crate `no_main`",
"code": {
"code": "E0601",
"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"
},
"level": "error",
"spans": [
{
"file_name": "no_main.rs",
"byte_start": 26,
"byte_end": 26,
"line_start": 1,
"line_end": 1,
"column_start": 27,
"column_end": 27,
"is_primary": true,
"text": [
{
"text": "// This file has no main.",
"highlight_start": 27,
"highlight_end": 27
}
],
"label": "consider adding a `main` function to `no_main.rs`",
"suggested_replacement": null,
"suggestion_applicability": null,
"expansion": null
}
],
"children": [],
"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"
}
1 change: 1 addition & 0 deletions tests/edge-cases/no_main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// This file has no main.
1 change: 1 addition & 0 deletions tests/edge_cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ expect_empty_json_test! {multiple_fix_options_yield_no_suggestions, "skip-multi-
expect_empty_json_test! {out_of_bounds_test, "out_of_bounds.recorded.json"}
expect_empty_json_test! {utf8_identifiers_test, "utf8_idents.recorded.json"}
expect_empty_json_test! {empty, "empty.json"}
expect_empty_json_test! {no_main, "no_main.json"}

0 comments on commit df8401b

Please sign in to comment.