inline method: ensure required braces are not removed (IDEA-201741)

This commit is contained in:
Anna.Kozlova
2018-11-09 16:13:52 +01:00
parent abae2f744d
commit 6ff61941a5
4 changed files with 53 additions and 9 deletions

View File

@@ -1497,22 +1497,26 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor {
PsiStatement[] statements = codeBlock.getStatements();
if (statements.length == 1) {
final PsiElement codeBlockParent = codeBlock.getParent();
PsiStatement statement = statements[0];
if (codeBlockParent instanceof PsiLambdaExpression) {
if (statements[0] instanceof PsiReturnStatement) {
final PsiExpression returnValue = ((PsiReturnStatement)statements[0]).getReturnValue();
if (statement instanceof PsiReturnStatement) {
final PsiExpression returnValue = ((PsiReturnStatement)statement).getReturnValue();
if (returnValue != null) {
codeBlock.replace(returnValue);
}
} else if (statements[0] instanceof PsiExpressionStatement){
codeBlock.replace(((PsiExpressionStatement)statements[0]).getExpression());
} else if (statement instanceof PsiExpressionStatement){
codeBlock.replace(((PsiExpressionStatement)statement).getExpression());
}
}
else if (codeBlockParent instanceof PsiBlockStatement) {
if (!(ifStatementWithAppendableElseBranch(statement) &&
codeBlockParent.getParent() instanceof PsiIfStatement &&
((PsiIfStatement)codeBlockParent.getParent()).getElseBranch() != null)) {
codeBlockParent.replace(statement);
}
}
else {
if (codeBlockParent instanceof PsiBlockStatement) {
codeBlockParent.replace(statements[0]);
} else {
codeBlock.replace(statements[0]);
}
codeBlock.replace(statement);
}
}
}
@@ -1532,6 +1536,14 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor {
}
}
private static boolean ifStatementWithAppendableElseBranch(PsiStatement statement) {
if (statement instanceof PsiIfStatement) {
PsiStatement elseBranch = ((PsiIfStatement)statement).getElseBranch();
return elseBranch == null || elseBranch instanceof PsiIfStatement;
}
return false;
}
@Nullable
private PsiExpression getSimpleFieldInitializer(PsiField field, PsiClassInitializer initializer) {
final PsiStatement[] statements = initializer.getBody().getStatements();

View File

@@ -0,0 +1,13 @@
class Temp {
public static void main(String... args) {
if (args.length > 0)
check<caret>Args(args);
else
System.out.println("help");
}
private static void checkArgs(String[] args) {
if (args[0].equals("foo"))
System.out.println("bar");
}
}

View File

@@ -0,0 +1,15 @@
class Temp {
public static void main(String... args) {
if (args.length > 0) {
if (args[0].equals("foo"))
System.out.println("bar");
}
else
System.out.println("help");
}
private static void checkArgs(String[] args) {
if (args[0].equals("foo"))
System.out.println("bar");
}
}

View File

@@ -312,6 +312,10 @@ public class InlineMethodTest extends LightRefactoringTestCase {
doTestInlineThisOnly();
}
public void testIfElseIfWithSingleStatement() {
doTestInlineThisOnly();
}
public void testUnresolvedArgPassedToSameNameParameter() {
doTestInlineThisOnly();
}