From ff555bd832e72d69746169435d721b8728b181a5 Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Tue, 17 May 2016 16:16:33 +0200 Subject: [PATCH] disable remove redundant else if then branch can throw exception (IDEA-156142) --- .../impl/quickfix/RemoveRedundantElseAction.java | 3 ++- .../beforeCanThrowException.java | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeRedundantElse/beforeCanThrowException.java diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/RemoveRedundantElseAction.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/RemoveRedundantElseAction.java index 0428f865b309..28db235e1e1b 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/RemoveRedundantElseAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/RemoveRedundantElseAction.java @@ -24,6 +24,7 @@ import com.intellij.openapi.project.Project; import com.intellij.psi.*; import com.intellij.psi.controlFlow.*; import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.util.BitUtil; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -82,7 +83,7 @@ public class RemoveRedundantElseAction extends PsiElementBaseIntentionAction { ControlFlow controlFlow = ControlFlowFactory.getInstance(thenBranch.getProject()).getControlFlow(block, LocalsOrMyInstanceFieldsControlFlowPolicy.getInstance()); int startOffset = controlFlow.getStartOffset(thenBranch); int endOffset = controlFlow.getEndOffset(thenBranch); - return startOffset != -1 && endOffset != -1 && !ControlFlowUtil.canCompleteNormally(controlFlow, startOffset, endOffset); + return startOffset != -1 && endOffset != -1 && !BitUtil.isSet(ControlFlowUtil.getCompletionReasons(controlFlow, startOffset, endOffset), ControlFlowUtil.NORMAL_COMPLETION_REASON); } catch (AnalysisCanceledException e) { return false; diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeRedundantElse/beforeCanThrowException.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeRedundantElse/beforeCanThrowException.java new file mode 100644 index 000000000000..8fd59ef094f3 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeRedundantElse/beforeCanThrowException.java @@ -0,0 +1,15 @@ +// "Remove redundant 'else'" "false" +import java.io.IOException; +class a { + void foo(boolean condition) throws IOException{ + if (condition) { + tMethod(); + } + else { + System.out.println("else"); + } + } + + void tMethod() throws IOException {} +} +