inline: remove side effect free unused after inline return (IDEA-169056)

This commit is contained in:
Anna.Kozlova
2017-11-16 18:39:20 +01:00
parent 4a81f24c39
commit 28e0e142d6
4 changed files with 30 additions and 2 deletions
@@ -233,7 +233,8 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor {
final PsiReturnStatement[] returnStatements = PsiUtil.findReturnStatements(myMethod);
for (PsiReturnStatement statement : returnStatements) {
PsiExpression value = statement.getReturnValue();
if (value != null && !(value instanceof PsiCallExpression)) {
if (value != null && !(value instanceof PsiCallExpression) &&
RemoveUnusedVariableUtil.checkSideEffects(value, null, new ArrayList<>())) {
for (UsageInfo info : usagesIn) {
PsiReference reference = info.getReference();
if (reference != null) {
@@ -894,7 +895,8 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor {
if (returnValue == null) continue;
PsiStatement statement;
if (tailCallType == InlineUtil.TailCallType.Simple) {
if (returnValue instanceof PsiExpression && returnStatement.getNextSibling() == myMethodCopy.getBody().getLastBodyElement()) {
if (returnStatement.getNextSibling() == myMethodCopy.getBody().getLastBodyElement() &&
RemoveUnusedVariableUtil.checkSideEffects(returnValue, null, new ArrayList<>())) {
PsiExpressionStatement exprStatement = (PsiExpressionStatement) myFactory.createStatementFromText("a;", null);
exprStatement.getExpression().replace(returnValue);
returnStatement.getParent().addBefore(exprStatement, returnStatement);
@@ -0,0 +1,11 @@
class InlineMethod {
void test() {
oth<caret>er();
}
InlineMethod other() {
System.out.println("");
return this;
}
}
@@ -0,0 +1,11 @@
class InlineMethod {
void test() {
System.out.println("");
}
InlineMethod other() {
System.out.println("");
return this;
}
}
@@ -359,6 +359,10 @@ public class InlineMethodTest extends LightRefactoringTestCase {
doTestInlineThisOnly();
}
public void testRemoveReturnForTailTypeSimpleWhenNoSideEffectsPossible() {
doTestInlineThisOnly();
}
public void testDeleteOverrideAnnotations() {
doTest();
}