From fb8f978e490c199615b5961f7cdf803589fc7707 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Tue, 17 Dec 2019 08:39:05 +0700 Subject: [PATCH] inlineMethodCall: encodeContextInfo for prepared block Sometimes during the transformations we generate statements/expressions from text, thus context info is not preserved. Luckily it could be encoded after the preparation. Fixes IDEA-228512 Method inlining may behave incorrectly when return type is ignored GitOrigin-RevId: 58cc2013cd6286be88e9d85b2b7ec69e08d1815f --- .../refactoring/inline/InlineMethodProcessor.java | 3 +-- .../refactoring/inlineMethod/IgnoreReturnValue.java | 13 +++++++++++++ .../inlineMethod/IgnoreReturnValue.java.after | 10 ++++++++++ .../java/refactoring/inline/InlineMethodTest.java | 4 ++++ 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 java/java-tests/testData/refactoring/inlineMethod/IgnoreReturnValue.java create mode 100644 java/java-tests/testData/refactoring/inlineMethod/IgnoreReturnValue.java.after diff --git a/java/java-impl/src/com/intellij/refactoring/inline/InlineMethodProcessor.java b/java/java-impl/src/com/intellij/refactoring/inline/InlineMethodProcessor.java index b52054fec765..7ae3ee868241 100644 --- a/java/java-impl/src/com/intellij/refactoring/inline/InlineMethodProcessor.java +++ b/java/java-impl/src/com/intellij/refactoring/inline/InlineMethodProcessor.java @@ -604,14 +604,13 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor { } public void inlineMethodCall(PsiReferenceExpression ref) throws IncorrectOperationException { - ChangeContextUtil.encodeContextInfo(myMethod, false); myMethodCopy = (PsiMethod)myMethod.copy(); - ChangeContextUtil.clearContextInfo(myMethod); PsiMethodCallExpression methodCall = (PsiMethodCallExpression)ref.getParent(); InlineMethodHelper helper = new InlineMethodHelper(myProject, myMethod, myMethodCopy, methodCall); BlockData blockData = prepareBlock(ref, helper); + ChangeContextUtil.encodeContextInfo(blockData.block, false); InlineUtil.solveVariableNameConflicts(blockData.block, ref, myMethodCopy.getBody()); helper.initializeParameters(blockData.parmVars); addThisInitializer(methodCall, blockData.thisVar); diff --git a/java/java-tests/testData/refactoring/inlineMethod/IgnoreReturnValue.java b/java/java-tests/testData/refactoring/inlineMethod/IgnoreReturnValue.java new file mode 100644 index 000000000000..33e6f8e9bcf9 --- /dev/null +++ b/java/java-tests/testData/refactoring/inlineMethod/IgnoreReturnValue.java @@ -0,0 +1,13 @@ +class Foo { + void method() { + new Bar().nonVoidMethod(); + } +} +class Bar { + Bar nonVoidMethod() { + return this.innerMethod(); + } + Bar innerMethod() { + return this; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inlineMethod/IgnoreReturnValue.java.after b/java/java-tests/testData/refactoring/inlineMethod/IgnoreReturnValue.java.after new file mode 100644 index 000000000000..6ef112d39ec0 --- /dev/null +++ b/java/java-tests/testData/refactoring/inlineMethod/IgnoreReturnValue.java.after @@ -0,0 +1,10 @@ +class Foo { + void method() { + new Bar().innerMethod(); + } +} +class Bar { + Bar innerMethod() { + return this; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/refactoring/inline/InlineMethodTest.java b/java/java-tests/testSrc/com/intellij/java/refactoring/inline/InlineMethodTest.java index 4c037786564a..9a234e41744b 100644 --- a/java/java-tests/testSrc/com/intellij/java/refactoring/inline/InlineMethodTest.java +++ b/java/java-tests/testSrc/com/intellij/java/refactoring/inline/InlineMethodTest.java @@ -546,6 +546,10 @@ public class InlineMethodTest extends LightRefactoringTestCase { public void testTypeParameterMethodRefArgument() { doTest(); } + + public void testIgnoreReturnValue() { + doTest(); + } @Override protected Sdk getProjectJDK() {