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 <alex2789@gmail.com>
This commit is contained in:
bors[bot] 2019-10-27 19:20:01 +00:00 committed by GitHub
commit 46b63c462d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -155,10 +155,17 @@ fn count_hashes(s: &str) -> usize {
}
fn find_usual_string_range(s: &str) -> Option<TextRange> {
Some(TextRange::from_to(
TextUnit::from(s.find('"')? as u32),
TextUnit::from(s.rfind('"')? as u32),
))
let left_quote = s.find('"')?;
let right_quote = s.rfind('"')?;
if left_quote == right_quote {
// `s` only contains one quote
None
} else {
Some(TextRange::from_to(
TextUnit::from(left_quote as u32),
TextUnit::from(right_quote as u32),
))
}
}
#[cfg(test)]
@ -267,6 +274,30 @@ string"###;
)
}
#[test]
fn make_raw_string_not_works_on_partial_string() {
check_assist_not_applicable(
make_raw_string,
r#"
fn f() {
let s = "foo<|>
}
"#,
)
}
#[test]
fn make_usual_string_not_works_on_partial_string() {
check_assist_not_applicable(
make_usual_string,
r#"
fn main() {
let s = r#"bar<|>
}
"#,
)
}
#[test]
fn add_hash_target() {
check_assist_target(