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

Commit 66ef037

Browse files
authored
Merge pull request #139 from alexcrichton/fix-bug
Handle invalid snippets instead of panicking
2 parents 8921742 + c9d3e09 commit 66ef037

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/lib.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub struct Replacement {
9292
pub replacement: String,
9393
}
9494

95-
fn parse_snippet(span: &DiagnosticSpan) -> Snippet {
95+
fn parse_snippet(span: &DiagnosticSpan) -> Option<Snippet> {
9696
// unindent the snippet
9797
let indent = span.text
9898
.iter()
@@ -103,8 +103,7 @@ fn parse_snippet(span: &DiagnosticSpan) -> Snippet {
103103
.count();
104104
std::cmp::min(indent, line.highlight_start)
105105
})
106-
.min()
107-
.expect("text to replace is empty");
106+
.min()?;
108107
let start = span.text[0].highlight_start - 1;
109108
let end = span.text[0].highlight_end - 1;
110109
let lead = span.text[0].text[indent..start].to_string();
@@ -120,7 +119,7 @@ fn parse_snippet(span: &DiagnosticSpan) -> Snippet {
120119
body.push_str(&last.text[indent..last.highlight_end - 1]);
121120
}
122121
tail.push_str(&last.text[last.highlight_end - 1..]);
123-
Snippet {
122+
Some(Snippet {
124123
file_name: span.file_name.clone(),
125124
line_range: LineRange {
126125
start: LinePosition {
@@ -134,16 +133,13 @@ fn parse_snippet(span: &DiagnosticSpan) -> Snippet {
134133
},
135134
range: (span.byte_start as usize)..(span.byte_end as usize),
136135
text: (lead, body, tail),
137-
}
136+
})
138137
}
139138

140139
fn collect_span(span: &DiagnosticSpan) -> Option<Replacement> {
141-
span.suggested_replacement
142-
.clone()
143-
.map(|replacement| Replacement {
144-
snippet: parse_snippet(span),
145-
replacement,
146-
})
140+
let snippet = parse_snippet(span)?;
141+
let replacement = span.suggested_replacement.clone()?;
142+
Some(Replacement { snippet, replacement })
147143
}
148144

149145
pub fn collect_suggestions<S: ::std::hash::BuildHasher>(
@@ -166,7 +162,7 @@ pub fn collect_suggestions<S: ::std::hash::BuildHasher>(
166162
let snippets = diagnostic
167163
.spans
168164
.iter()
169-
.map(|span| parse_snippet(span))
165+
.filter_map(|span| parse_snippet(span))
170166
.collect();
171167

172168
let solutions: Vec<_> = diagnostic

0 commit comments

Comments
 (0)