MoveIntoIfBranchesAction: disable if if branch cannot complete normally

GitOrigin-RevId: 07c9d5a46c0ded0d1de27c0a0c4b372061b84917
This commit is contained in:
Tagir Valeev
2020-06-17 09:24:13 +03:00
committed by intellij-monorepo-bot
parent 1829c3119a
commit 91092e2ef7
2 changed files with 14 additions and 1 deletions
@@ -62,7 +62,13 @@ public class MoveIntoIfBranchesAction implements IntentionAction {
afterLast.add(e);
}
}
List<String> declaredInIf = StreamEx.of(ifStatement.getThenBranch(), ifStatement.getElseBranch()).flatArray(ControlFlowUtils::unwrapBlock)
PsiStatement thenBranch = ifStatement.getThenBranch();
PsiStatement elseBranch = ifStatement.getElseBranch();
if (!ControlFlowUtils.statementMayCompleteNormally(thenBranch) ||
!ControlFlowUtils.statementMayCompleteNormally(elseBranch)) {
return true;
}
List<String> declaredInIf = StreamEx.of(thenBranch, elseBranch).flatArray(ControlFlowUtils::unwrapBlock)
.select(PsiDeclarationStatement.class).flatArray(PsiDeclarationStatement::getDeclaredElements)
.select(PsiNamedElement.class).map(PsiNamedElement::getName).nonNull().toList();
if (afterLast.isEmpty() && declaredInIf.isEmpty()) return false;
@@ -0,0 +1,7 @@
// "Move up into 'if' statement branches" "false"
class Test {
void test(int x) {
if (x > 0) return;
<caret>System.out.println(x);
}
}