diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java index 1a77eb6e4f05..ccd3fafe6942 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java @@ -31,6 +31,7 @@ import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Objects; public class SimplifyBooleanExpressionFix extends LocalQuickFixOnPsiElement { private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.SimplifyBooleanExpression"); @@ -124,6 +125,16 @@ public class SimplifyBooleanExpressionFix extends LocalQuickFixOnPsiElement { if (subExpression == null) return; CommentTracker ct = new CommentTracker(); if (shouldExtractSideEffect()) { + if (!mySubExpressionValue) { + // Prevent extracting while condition to internal 'if' + PsiWhileStatement whileStatement = ObjectUtils.tryCast(PsiUtil.skipParenthesizedExprUp(subExpression.getParent()), PsiWhileStatement.class); + if (whileStatement != null && whileStatement.getCondition() != null) { + PsiStatement replacement = JavaPsiFacade.getElementFactory(project) + .createStatementFromText("if(" + whileStatement.getCondition().getText() + ");", whileStatement); + PsiIfStatement ifStatement = (PsiIfStatement)whileStatement.replace(replacement); + subExpression = Objects.requireNonNull(ifStatement.getCondition()); + } + } subExpression = RefactoringUtil.ensureCodeBlock(subExpression); if (subExpression == null) { LOG.error("ensureCodeBlock returned null", new Attachment("subExpression.txt", getSubExpression().getText())); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/afterWhileWithSideEffects.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/afterWhileWithSideEffects.java new file mode 100644 index 000000000000..d2f5bfbee743 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/afterWhileWithSideEffects.java @@ -0,0 +1,15 @@ +// "Remove 'while' statement extracting side effects" "true" +import org.jetbrains.annotations.Contract; + +class X { + @Contract("_ -> true") + boolean test(Object obj) { + return true; + } + + void doSmth(Object obj) { + if (obj != null) { + test(obj); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/afterWhileWithSideEffects2.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/afterWhileWithSideEffects2.java new file mode 100644 index 000000000000..697d8b1ed978 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/afterWhileWithSideEffects2.java @@ -0,0 +1,16 @@ +// "Simplify 'test(...) || obj == null' to true extracting side effects" "true" +import org.jetbrains.annotations.Contract; + +class X { + @Contract("_ -> true") + boolean test(Object obj) { + return true; + } + + void doSmth(Object obj) { + while(obj != null) { + test(obj); + System.out.println("aaahh"); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/beforeWhileWithSideEffects.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/beforeWhileWithSideEffects.java new file mode 100644 index 000000000000..237b66cdbee4 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/beforeWhileWithSideEffects.java @@ -0,0 +1,15 @@ +// "Remove 'while' statement extracting side effects" "true" +import org.jetbrains.annotations.Contract; + +class X { + @Contract("_ -> true") + boolean test(Object obj) { + return true; + } + + void doSmth(Object obj) { + while(obj != null && test(obj) && obj == null) { + System.out.println("aaahh"); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/beforeWhileWithSideEffects2.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/beforeWhileWithSideEffects2.java new file mode 100644 index 000000000000..7c27085bfc48 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/beforeWhileWithSideEffects2.java @@ -0,0 +1,15 @@ +// "Simplify 'test(...) || obj == null' to true extracting side effects" "true" +import org.jetbrains.annotations.Contract; + +class X { + @Contract("_ -> true") + boolean test(Object obj) { + return true; + } + + void doSmth(Object obj) { + while(obj != null && (test(obj) || obj == null)) { + System.out.println("aaahh"); + } + } +} \ No newline at end of file