From 390caa70ad6d095702a553378105c8b72276c37e Mon Sep 17 00:00:00 2001 From: peter Date: Fri, 7 Sep 2018 08:59:09 +0200 Subject: [PATCH] disable "swap if statements" intention where it would fail --- .../intention/impl/SwapIfStatementsIntentionAction.java | 7 ++++++- .../quickFix/swapIfStatements/beforeMalformed.java | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/swapIfStatements/beforeMalformed.java diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/SwapIfStatementsIntentionAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/SwapIfStatementsIntentionAction.java index a4f6741b4647..e9648b50d46f 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/SwapIfStatementsIntentionAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/SwapIfStatementsIntentionAction.java @@ -21,6 +21,7 @@ import com.intellij.openapi.project.Project; import com.intellij.psi.*; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * @author Dmitry Batkovich @@ -58,7 +59,11 @@ public class SwapIfStatementsIntentionAction extends PsiElementBaseIntentionActi return false; } final PsiElement parent = element.getParent(); - return parent instanceof PsiIfStatement && ((PsiIfStatement)parent).getElseBranch() instanceof PsiIfStatement; + return isWellFormedIf(parent) && isWellFormedIf(((PsiIfStatement)parent).getElseBranch()); + } + + private static boolean isWellFormedIf(@Nullable PsiElement e) { + return e instanceof PsiIfStatement && ((PsiIfStatement)e).getCondition() != null && ((PsiIfStatement)e).getThenBranch() != null; } @NotNull diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/swapIfStatements/beforeMalformed.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/swapIfStatements/beforeMalformed.java new file mode 100644 index 000000000000..32ffeffe1a09 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/swapIfStatements/beforeMalformed.java @@ -0,0 +1,9 @@ +// "Swap 'if' statements" "false" +class Test { + public static void main(String[] args) { + if (true) { + System.out.println(); + } + else if (true) + } +} \ No newline at end of file