diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-10-27 19:20:01 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-10-27 19:20:01 +0000 |
commit | 46b63c462d66925b59c0af7dec2eb3c48bc7be25 (patch) | |
tree | 274837f2d667e2a91e253e3bd6d705f717369fbe | |
parent | 534c8a0d78a74e28415f07954d4d948ecfbd64ed (diff) | |
parent | 17bd3e59f86a4da548c6c61dff9421dfdb31b65c (diff) |
Merge #2099
2099: Fix panic on raw string assist r=matklad a=aee11
Strings that do not contain two quotation marks would cause a slice indexing panic because `find_usual_string_range` would return a range that only contained a single quotation mark.
Panic example:
```
fn main() {
let s = "<|>
}
```
I noticed a lot of panics from the `make_raw_string` assist while working on another issue today.
Co-authored-by: Alexander Elís Ebenesersson <[email protected]>
-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 86bbaeeef..6f4b66c31 100644 --- a/crates/ra_assists/src/assists/raw_string.rs +++ b/crates/ra_assists/src/assists/raw_string.rs | |||
@@ -155,10 +155,17 @@ fn count_hashes(s: &str) -> usize { | |||
155 | } | 155 | } |
156 | 156 | ||
157 | fn find_usual_string_range(s: &str) -> Option<TextRange> { | 157 | fn find_usual_string_range(s: &str) -> Option<TextRange> { |
158 | Some(TextRange::from_to( | 158 | let left_quote = s.find('"')?; |
159 | TextUnit::from(s.find('"')? as u32), | 159 | let right_quote = s.rfind('"')?; |
160 | TextUnit::from(s.rfind('"')? as u32), | 160 | if left_quote == right_quote { |
161 | )) | 161 | // `s` only contains one quote |
162 | None | ||
163 | } else { | ||
164 | Some(TextRange::from_to( | ||
165 | TextUnit::from(left_quote as u32), | ||
166 | TextUnit::from(right_quote as u32), | ||
167 | )) | ||
168 | } | ||
162 | } | 169 | } |
163 | 170 | ||
164 | #[cfg(test)] | 171 | #[cfg(test)] |
@@ -268,6 +275,30 @@ string"###; | |||
268 | } | 275 | } |
269 | 276 | ||
270 | #[test] | 277 | #[test] |
278 | fn make_raw_string_not_works_on_partial_string() { | ||
279 | check_assist_not_applicable( | ||
280 | make_raw_string, | ||
281 | r#" | ||
282 | fn f() { | ||
283 | let s = "foo<|> | ||
284 | } | ||
285 | "#, | ||
286 | ) | ||
287 | } | ||
288 | |||
289 | #[test] | ||
290 | fn make_usual_string_not_works_on_partial_string() { | ||
291 | check_assist_not_applicable( | ||
292 | make_usual_string, | ||
293 | r#" | ||
294 | fn main() { | ||
295 | let s = r#"bar<|> | ||
296 | } | ||
297 | "#, | ||
298 | ) | ||
299 | } | ||
300 | |||
301 | #[test] | ||
271 | fn add_hash_target() { | 302 | fn add_hash_target() { |
272 | check_assist_target( | 303 | check_assist_target( |
273 | add_hash, | 304 | add_hash, |