IDEA-175018 Unwrap if produces red code

This commit is contained in:
peter
2017-07-07 17:53:00 +02:00
parent a7ce2856cf
commit 365d521519
3 changed files with 38 additions and 1 deletions
@@ -189,9 +189,16 @@ public class SimplifyBooleanExpressionFix extends LocalQuickFixOnPsiElement {
PsiElement parent = orig.getParent();
if (parent == null) return;
PsiElement grandParent = parent.getParent();
if (parent instanceof PsiCodeBlock && blockAlwaysReturns(statement)) {
removeFollowingStatements(orig, (PsiCodeBlock)parent);
}
else if (grandParent instanceof PsiCodeBlock && parent instanceof PsiIfStatement) {
PsiIfStatement ifStmt = (PsiIfStatement)parent;
if (ifStmt.getElseBranch() == orig && blockAlwaysReturns(ifStmt.getThenBranch()) && blockAlwaysReturns(statement)) {
removeFollowingStatements(ifStmt, (PsiCodeBlock)grandParent);
}
}
if (parent instanceof PsiCodeBlock) {
if (statement instanceof PsiBlockStatement &&
@@ -246,7 +253,8 @@ public class SimplifyBooleanExpressionFix extends LocalQuickFixOnPsiElement {
orig.delete();
}
private static boolean blockAlwaysReturns(@NotNull PsiStatement statement) {
private static boolean blockAlwaysReturns(@Nullable PsiStatement statement) {
if (statement == null) return false;
try {
return ControlFlowUtil.returnPresent(HighlightControlFlowUtil.getControlFlowNoConstantEvaluate(statement));
}
@@ -0,0 +1,14 @@
// "Unwrap 'if' statement" "true"
class X {
boolean f(int[] a) {
if (a.length == 0) return false;
if (a.length == 1) {
System.out.println();
return true;
}
else {
System.out.println("2");
return true;
}
}
}
@@ -0,0 +1,15 @@
// "Unwrap 'if' statement" "true"
class X {
boolean f(int[] a) {
if (a.length == 0) return false;
if (a.length == 1) {
System.out.println();
return true;
}
else if (a.<caret>length > 1) {
System.out.println("2");
return true;
}
return false;
}
}