Avoid unsafe unwrap of line_numbers, gracefully handle non-existing matches

Fixes #1044.
This commit is contained in:
KtorZ
2024-10-25 11:36:05 +02:00
parent 93d0191489
commit 2b7ca0e4a1
2 changed files with 11 additions and 3 deletions

View File

@@ -60,9 +60,16 @@ impl SourceLinker {
pub fn url(&self, span: Span) -> String {
match &self.url_pattern {
Some((base, line_sep)) => {
let start_line = self.line_numbers.line_number(span.start).unwrap();
let end_line = self.line_numbers.line_number(span.end).unwrap();
format!("{base}{start_line}{line_sep}{end_line}")
match (
self.line_numbers.line_number(span.start),
self.line_numbers.line_number(span.end),
) {
(Some(start_line), Some(end_line)) => {
format!("{base}{start_line}{line_sep}{end_line}")
}
(Some(start_line), None) => format!("{base}{start_line}"),
_ => base.to_string(),
}
}
None => "".into(),
}