From fa1e4113224c119258670538f8c3ca6c8ea8ad1e Mon Sep 17 00:00:00 2001 From: David Lattimore Date: Wed, 29 Jul 2020 21:23:39 +1000 Subject: SSR: Wrap placeholder expansions in parenthesis when necessary e.g. `foo($a) ==> $a.to_string()` should produce `(1 + 2).to_string()` not `1 + 2.to_string()` We don't yet try to determine if the whole replacement needs to be wrapped in parenthesis. That's harder and I think perhaps less often an issue. --- crates/ra_ssr/src/tests.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'crates/ra_ssr/src/tests.rs') diff --git a/crates/ra_ssr/src/tests.rs b/crates/ra_ssr/src/tests.rs index f5ffff7cc..a4fa2cb44 100644 --- a/crates/ra_ssr/src/tests.rs +++ b/crates/ra_ssr/src/tests.rs @@ -664,7 +664,7 @@ fn replace_binary_op() { assert_ssr_transform( "$a + $b ==>> $b + $a", "fn f() {1 + 2 + 3 + 4}", - expect![["fn f() {4 + 3 + 2 + 1}"]], + expect![[r#"fn f() {4 + (3 + (2 + 1))}"#]], ); } @@ -773,11 +773,32 @@ fn preserves_whitespace_within_macro_expansion() { macro_rules! macro1 { ($a:expr) => {$a} } - fn f() {macro1!(4 - 3 - 1 * 2} + fn f() {macro1!(4 - (3 - 1 * 2)} "#]], ) } +#[test] +fn add_parenthesis_when_necessary() { + assert_ssr_transform( + "foo($a) ==>> $a.to_string()", + r#" + fn foo(_: i32) {} + fn bar3(v: i32) { + foo(1 + 2); + foo(-v); + } + "#, + expect![[r#" + fn foo(_: i32) {} + fn bar3(v: i32) { + (1 + 2).to_string(); + (-v).to_string(); + } + "#]], + ) +} + #[test] fn match_failure_reasons() { let code = r#" -- cgit v1.2.3