IDEA-218391 Transform to single return: remove excessive braces in if-else chains.

GitOrigin-RevId: b13d36218843ae5ff7fbfbcadc898113ad4c9bc1
This commit is contained in:
Tagir Valeev
2019-07-16 15:01:49 +03:00
committed by intellij-monorepo-bot
parent 6a06cd749a
commit 50ff44aebd
5 changed files with 40 additions and 29 deletions
@@ -289,34 +289,53 @@ class ReturnReplacementContext {
}
PsiCodeBlock block = tryCast(myReturnStatement.getParent(), PsiCodeBlock.class);
new CommentTracker().deleteAndRestoreComments(myReturnStatement);
cleanUpEmptyBlocks(block);
PsiElement place = cleanUpEmptyBlocks(block);
stripUnnecessaryBlocks(place);
}
private static void cleanUpEmptyBlocks(PsiCodeBlock block) {
if (block == null || !block.isEmpty()) return;
private void stripUnnecessaryBlocks(PsiElement place) {
while (place != null && place != myBlock) {
if (place instanceof PsiBlockStatement) {
PsiIfStatement parentIf = tryCast(place.getParent(), PsiIfStatement.class);
if (parentIf != null && parentIf.getElseBranch() == place) {
PsiIfStatement childIf = tryCast(ControlFlowUtils.stripBraces((PsiStatement)place), PsiIfStatement.class);
if (childIf != null) {
place = place.replace(childIf);
}
}
}
place = place.getParent();
}
}
private static PsiElement cleanUpEmptyBlocks(PsiCodeBlock block) {
if (block == null || !block.isEmpty()) return block;
PsiBlockStatement blockStatement = tryCast(block.getParent(), PsiBlockStatement.class);
if (blockStatement == null) return;
if (blockStatement == null) return block;
PsiIfStatement parent = tryCast(blockStatement.getParent(), PsiIfStatement.class);
if (parent == null) return;
if (parent == null) return block;
PsiExpression condition = parent.getCondition();
if (condition == null) return;
if (condition == null) return block;
if (blockStatement == parent.getElseBranch()) {
new CommentTracker().deleteAndRestoreComments(blockStatement);
return parent;
}
else if (blockStatement == parent.getThenBranch()) {
if (blockStatement == parent.getThenBranch()) {
if (parent.getElseBranch() != null) {
new CommentTracker().replaceAndRestoreComments(blockStatement, parent.getElseBranch());
parent.getElseBranch().delete();
CommentTracker ct = new CommentTracker();
String negatedCondition = BoolUtils.getNegatedExpressionText(condition, ct);
ct.replaceAndRestoreComments(condition, negatedCondition);
return parent;
}
else if (!SideEffectChecker.mayHaveSideEffects(condition)) {
if (!SideEffectChecker.mayHaveSideEffects(condition)) {
PsiCodeBlock parentBlock = tryCast(parent.getParent(), PsiCodeBlock.class);
new CommentTracker().deleteAndRestoreComments(parent);
cleanUpEmptyBlocks(parentBlock);
return cleanUpEmptyBlocks(parentBlock);
}
}
return block;
}
static void replaceSingleReturn(@NotNull Project project,
@@ -12,12 +12,10 @@ class Test {
s = arr[1];
if (s == null) {
result = false;
} else {
if (arr.length > 3) {
s = arr[2];
if (s != null && s.isEmpty()) {
result = false;
}
} else if (arr.length > 3) {
s = arr[2];
if (s != null && s.isEmpty()) {
result = false;
}
}
}
@@ -8,12 +8,10 @@ class Test {
} else {
result = 4;
}
} else if (s.isEmpty()) {
result = 3;
} else {
if (s.isEmpty()) {
result = 3;
} else {
System.out.println(s);
}
System.out.println(s);
}
return result;
}
@@ -5,12 +5,10 @@ class Test {
synchronized (this) {
if (x == 0) {
result = "foo";
} else if (x == 1) {
result = "bar";
} else {
if (x == 1) {
result = "bar";
} else {
result = "baz";
}
result = "baz";
}
}
return result;
@@ -5,10 +5,8 @@ class Test {
synchronized (this) {
if (x == 0) {
result = "foo";
} else {
if (x == 1) {
result = "bar";
}
} else if (x == 1) {
result = "bar";
}
}
if (result == null) {