Skip to content

Commit 46b63c4

Browse files
bors[bot]aee11
andauthored
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]>
2 parents 534c8a0 + 17bd3e5 commit 46b63c4

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

crates/ra_assists/src/assists/raw_string.rs

+35-4
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,17 @@ fn count_hashes(s: &str) -> usize {
155155
}
156156

157157
fn find_usual_string_range(s: &str) -> Option<TextRange> {
158-
Some(TextRange::from_to(
159-
TextUnit::from(s.find('"')? as u32),
160-
TextUnit::from(s.rfind('"')? as u32),
161-
))
158+
let left_quote = s.find('"')?;
159+
let right_quote = s.rfind('"')?;
160+
if left_quote == right_quote {
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+
}
162169
}
163170

164171
#[cfg(test)]
@@ -267,6 +274,30 @@ string"###;
267274
)
268275
}
269276

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+
270301
#[test]
271302
fn add_hash_target() {
272303
check_assist_target(

0 commit comments

Comments
 (0)