replacing an infinite do-while loop with a while loop shouldn't use trickery

This commit is contained in:
Bas Leijdekkers
2013-11-16 19:23:26 +01:00
parent 80450e71f5
commit a4236bfc13
8 changed files with 60 additions and 7 deletions
@@ -16,6 +16,7 @@
package com.siyeh.ipp.whileloop;
import com.intellij.psi.*;
import com.siyeh.ig.psiutils.BoolUtils;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import org.jetbrains.annotations.NonNls;
@@ -35,8 +36,18 @@ public class ReplaceDoWhileLoopWithWhileLoopIntention extends Intention {
}
final PsiStatement body = doWhileStatement.getBody();
final PsiElement parent = doWhileStatement.getParent();
boolean noBraces = !(parent instanceof PsiCodeBlock);
final PsiExpression condition = doWhileStatement.getCondition();
@NonNls final StringBuilder replacementText = new StringBuilder();
if (BoolUtils.isTrue(condition)) {
// no trickery needed
replacementText.append("while(").append(condition.getText()).append(')');
if (body != null) {
replacementText.append(body.getText());
}
replaceStatement(replacementText.toString(), doWhileStatement);
return;
}
final boolean noBraces = !(parent instanceof PsiCodeBlock);
if (noBraces) {
final PsiElement[] parentChildren = parent.getChildren();
for (PsiElement child : parentChildren) {
@@ -60,7 +71,10 @@ public class ReplaceDoWhileLoopWithWhileLoopIntention extends Intention {
for (PsiElement declaredElement : declaredElements) {
if (declaredElement instanceof PsiVariable) {
final PsiVariable variable = (PsiVariable)declaredElement;
variable.getModifierList().setModifierProperty(PsiModifier.FINAL, false);
final PsiModifierList modifierList = variable.getModifierList();
if (modifierList != null) {
modifierList.setModifierProperty(PsiModifier.FINAL, false);
}
}
}
}
@@ -82,7 +96,6 @@ public class ReplaceDoWhileLoopWithWhileLoopIntention extends Intention {
}
}
replacementText.append("while(");
final PsiExpression condition = doWhileStatement.getCondition();
if (condition != null) {
replacementText.append(condition.getText());
}
@@ -5,6 +5,10 @@ class FinalVariable1 {
<caret>do {
final int j = 10;
System.out.println(j);
} while (true);
} while (c());
}
boolean c() {
return true;
}
}
@@ -4,9 +4,13 @@ class FinalVariable1 {
void m() {
int j = 10;
System.out.println(j);
while (true) {
while (c()) {
j = 10;
System.out.println(j);
}
}
boolean c() {
return true;
}
}
@@ -6,6 +6,10 @@ class FinalVariable2 {
if (b) do<caret> {
final int i = 10;
System.out.println(i);
} while (true);
} while (c());
}
boolean c() {
return true;
}
}
@@ -6,10 +6,14 @@ class FinalVariable2 {
if (b) {
int i = 10;
System.out.println(i);
while (true) {
while (c()) {
i = 10;
System.out.println(i);
}
}
}
boolean c() {
return true;
}
}
@@ -0,0 +1,10 @@
package com.siyeh.ipp.whileloop.replace_do_while_with_while_loop;
class InfiniteLoop {
void m() {
do<caret> {
int i = 10;
System.out.println(i);
} while ((true));
}
}
@@ -0,0 +1,10 @@
package com.siyeh.ipp.whileloop.replace_do_while_with_while_loop;
class InfiniteLoop {
void m() {
while ((true)) {
int i = 10;
System.out.println(i);
}
}
}
@@ -3,11 +3,15 @@ package com.siyeh.ipp.whileloop;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ipp.IPPTestCase;
/**
* @see com.siyeh.ipp.whileloop.ReplaceDoWhileLoopWithWhileLoopIntention
*/
public class ReplaceDoWhileWithWhileLoopIntentionTest extends IPPTestCase {
public void testWithoutBraces() { doTest(); }
public void testFinalVariable1() { doTest(); }
public void testFinalVariable2() { doTest(); }
public void testInfiniteLoop() { doTest(); }
@Override
protected String getRelativePath() {