From f4a77f34da3d0d849c1e0b78b95df77485a57a3d Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 29 Nov 2020 17:56:36 +0100 Subject: Fix renaming owned self to parameter emitting ref --- crates/ide/src/references/rename.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'crates/ide') diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs index 91c64bd4a..e15b9f88e 100644 --- a/crates/ide/src/references/rename.rs +++ b/crates/ide/src/references/rename.rs @@ -280,7 +280,11 @@ fn text_edit_from_self_param( let mut replacement_text = String::from(new_name); replacement_text.push_str(": "); - replacement_text.push_str(self_param.mut_token().map_or("&", |_| "&mut ")); + match (self_param.amp_token(), self_param.mut_token()) { + (None, None) => (), + (Some(_), None) => replacement_text.push('&'), + (_, Some(_)) => replacement_text.push_str("&mut "), + }; replacement_text.push_str(type_name.as_str()); Some(TextEdit::replace(self_param.syntax().text_range(), replacement_text)) @@ -1109,6 +1113,31 @@ impl Foo { ); } + #[test] + fn test_owned_self_to_parameter() { + check( + "foo", + r#" +struct Foo { i: i32 } + +impl Foo { + fn f(<|>self) -> i32 { + self.i + } +} +"#, + r#" +struct Foo { i: i32 } + +impl Foo { + fn f(foo: Foo) -> i32 { + foo.i + } +} +"#, + ); + } + #[test] fn test_self_in_path_to_parameter() { check( -- cgit v1.2.3