IDEA-185965 Do not suggest to extract condition to internal 'if' for 'while(true)' loop

This commit is contained in:
Tagir Valeev
2018-02-01 11:27:02 +07:00
parent 07d4da3f8a
commit e4ba0f4e1e
3 changed files with 13 additions and 4 deletions
@@ -20,6 +20,7 @@ import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.siyeh.ig.psiutils.BlockUtils;
import com.siyeh.ig.psiutils.BoolUtils;
import com.siyeh.ig.psiutils.ExpressionUtils;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import org.jetbrains.annotations.NotNull;
@@ -29,7 +30,9 @@ public class ExtractWhileLoopConditionToIfStatementIntention extends Intention {
@Override
@NotNull
protected PsiElementPredicate getElementPredicate() {
return new WhileLoopPredicate();
WhileLoopPredicate predicate = new WhileLoopPredicate();
return e -> predicate.satisfiedBy(e) &&
!(ExpressionUtils.computeConstantExpression(((PsiWhileStatement)e.getParent()).getCondition()) instanceof Boolean);
}
@Override
@@ -0,0 +1,7 @@
class TrueCondition {
private volatile boolean flag;
void m() {
<caret>while (true) System.out.println();
}
}
@@ -1,6 +1,4 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.siyeh.ipp.whileloop;
import com.siyeh.IntentionPowerPackBundle;
@@ -14,6 +12,7 @@ public class ExtractWhileLoopConditionToIfStatementIntentionTest extends IPPTest
public void testSimple() { doTest(); }
public void testNoBody() { doTest(); }
public void testTrue() { assertIntentionNotAvailable(); }
@Override
protected String getRelativePath() {