From 5ff481b46aba8d0bad6ad9e22d9aa0429c5ba16b Mon Sep 17 00:00:00 2001 From: Dmitry Murzin Date: Wed, 27 Jul 2022 01:53:47 +0300 Subject: [PATCH] REF: Fix rename of item used in struct literal field shorthand --- .../rust/ide/refactoring/RsRenameProcessor.kt | 10 +++---- .../org/rust/ide/refactoring/RenameTest.kt | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/org/rust/ide/refactoring/RsRenameProcessor.kt b/src/main/kotlin/org/rust/ide/refactoring/RsRenameProcessor.kt index 2f4958560eb..e7d5e2e2495 100644 --- a/src/main/kotlin/org/rust/ide/refactoring/RsRenameProcessor.kt +++ b/src/main/kotlin/org/rust/ide/refactoring/RsRenameProcessor.kt @@ -83,15 +83,15 @@ class RsRenameProcessor : RenamePsiElementProcessor() { listener: RefactoringElementListener? ) { val psiFactory = RsPsiFactory(element.project) - if (element is RsPatBinding) { - usages.forEach { - val field = it.element?.ancestorOrSelf(RsBlock::class.java) ?: return@forEach + if (element !is RsNamedFieldDecl) { + for (usage in usages) { + val field = usage.element?.ancestorOrSelf(RsBlock::class.java) ?: continue when { field.isShorthand -> { - val newPatField = psiFactory.createStructLiteralField(element.text, newName) + val newPatField = psiFactory.createStructLiteralField(field.referenceName, newName) field.replace(newPatField) } - field.referenceName == newName && field.expr is RsPathExpr -> { + field.referenceName == newName && (field.expr as? RsPathExpr)?.path == usage.element -> { field.expr?.delete() field.colon?.delete() } diff --git a/src/test/kotlin/org/rust/ide/refactoring/RenameTest.kt b/src/test/kotlin/org/rust/ide/refactoring/RenameTest.kt index ff60c333200..2d76147e886 100644 --- a/src/test/kotlin/org/rust/ide/refactoring/RenameTest.kt +++ b/src/test/kotlin/org/rust/ide/refactoring/RenameTest.kt @@ -614,6 +614,34 @@ class RenameTest : RsTestBase() { fn foo(value: u32) -> u32 { unimplemented!() } """) + fun `test can't use initialization shorthand after rename 3`() = doTest("value2", """ + struct Foo { value: u32 } + fn bar() -> Foo { + const /*caret*/value: i32 = 1; + Foo { value } + } + """, """ + struct Foo { value: u32 } + fn bar() -> Foo { + const value2: i32 = 1; + Foo { value: value2 } + } + """) + + fun `test can't use initialization shorthand after rename 4`() = doTest("value2", """ + struct Foo { value: T } + fn bar() -> Foo { + fn /*caret*/value() {} + Foo { value } + } + """, """ + struct Foo { value: T } + fn bar() -> Foo { + fn value2() {} + Foo { value: value2 } + } + """) + fun `test rename variable with variable conflict`() = doTestWithConflicts("a", """ fn test() { let a = 1;