diff options
author | Alexander Elís Ebenesersson <[email protected]> | 2019-10-27 18:00:28 +0000 |
---|---|---|
committer | Alexander Elís Ebenesersson <[email protected]> | 2019-10-27 18:00:28 +0000 |
commit | 17bd3e59f86a4da548c6c61dff9421dfdb31b65c (patch) | |
tree | 9c27106e8f9762afe38f6f8ad9145e687f6441c7 /crates | |
parent | 7dfbe28211910d5d7c74a593bf0007c5db3e3496 (diff) |
Fix panic on raw string assist
Strings that do not contain two quotation marks
would cause a slice indexing panic because code
was assuming `find_usual_string_range` would return
a string with two quotes, but it would incorrectly
also return text ranges containing only a single quote.
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_assists/src/assists/raw_string.rs | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/crates/ra_assists/src/assists/raw_string.rs b/crates/ra_assists/src/assists/raw_string.rs index 2df48a838..cb3a1e6e9 100644 --- a/crates/ra_assists/src/assists/raw_string.rs +++ b/crates/ra_assists/src/assists/raw_string.rs | |||
@@ -159,10 +159,17 @@ fn count_hashes(s: &str) -> usize { | |||
159 | } | 159 | } |
160 | 160 | ||
161 | fn find_usual_string_range(s: &str) -> Option<TextRange> { | 161 | fn find_usual_string_range(s: &str) -> Option<TextRange> { |
162 | Some(TextRange::from_to( | 162 | let left_quote = s.find('"')?; |
163 | TextUnit::from(s.find('"')? as u32), | 163 | let right_quote = s.rfind('"')?; |
164 | TextUnit::from(s.rfind('"')? as u32), | 164 | if left_quote == right_quote { |
165 | )) | 165 | // `s` only contains one quote |
166 | None | ||
167 | } else { | ||
168 | Some(TextRange::from_to( | ||
169 | TextUnit::from(left_quote as u32), | ||
170 | TextUnit::from(right_quote as u32), | ||
171 | )) | ||
172 | } | ||
166 | } | 173 | } |
167 | 174 | ||
168 | #[cfg(test)] | 175 | #[cfg(test)] |
@@ -272,6 +279,30 @@ string"###; | |||
272 | } | 279 | } |
273 | 280 | ||
274 | #[test] | 281 | #[test] |
282 | fn make_raw_string_not_works_on_partial_string() { | ||
283 | check_assist_not_applicable( | ||
284 | make_raw_string, | ||
285 | r#" | ||
286 | fn f() { | ||
287 | let s = "foo<|> | ||
288 | } | ||
289 | "#, | ||
290 | ) | ||
291 | } | ||
292 | |||
293 | #[test] | ||
294 | fn make_usual_string_not_works_on_partial_string() { | ||
295 | check_assist_not_applicable( | ||
296 | make_usual_string, | ||
297 | r#" | ||
298 | fn main() { | ||
299 | let s = r#"bar<|> | ||
300 | } | ||
301 | "#, | ||
302 | ) | ||
303 | } | ||
304 | |||
305 | #[test] | ||
275 | fn add_hash_target() { | 306 | fn add_hash_target() { |
276 | check_assist_target( | 307 | check_assist_target( |
277 | add_hash, | 308 | add_hash, |