From 91092e2ef751e850119ee656f1f5cad5ab038b9b Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Wed, 17 Jun 2020 13:19:54 +0700 Subject: [PATCH] MoveIntoIfBranchesAction: disable if if branch cannot complete normally GitOrigin-RevId: 07c9d5a46c0ded0d1de27c0a0c4b372061b84917 --- .../intention/impl/MoveIntoIfBranchesAction.java | 8 +++++++- .../moveIntoIf/beforeIfCannotCompleteNormally.java | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/moveIntoIf/beforeIfCannotCompleteNormally.java diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/MoveIntoIfBranchesAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/MoveIntoIfBranchesAction.java index d60bdb9a9048..e3dccf79842a 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/MoveIntoIfBranchesAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/MoveIntoIfBranchesAction.java @@ -62,7 +62,13 @@ public class MoveIntoIfBranchesAction implements IntentionAction { afterLast.add(e); } } - List 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 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; diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/moveIntoIf/beforeIfCannotCompleteNormally.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/moveIntoIf/beforeIfCannotCompleteNormally.java new file mode 100644 index 000000000000..09810240acc4 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/moveIntoIf/beforeIfCannotCompleteNormally.java @@ -0,0 +1,7 @@ +// "Move up into 'if' statement branches" "false" +class Test { + void test(int x) { + if (x > 0) return; + System.out.println(x); + } +} \ No newline at end of file