From 17bd3e59f86a4da548c6c61dff9421dfdb31b65c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20El=C3=ADs=20Ebenesersson?= Date: Mon, 28 Oct 2019 03:00:28 +0900 Subject: 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. --- crates/ra_assists/src/assists/raw_string.rs | 39 ++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 4 deletions(-) (limited to 'crates') 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 { } fn find_usual_string_range(s: &str) -> Option { - 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)] @@ -271,6 +278,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( -- cgit v1.2.3