mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-10-01 19:02:13 +07:00
replacing an infinite do-while loop with a while loop shouldn't use trickery
This commit is contained in:
+16
-3
@@ -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
-1
@@ -5,6 +5,10 @@ class FinalVariable1 {
|
||||
<caret>do {
|
||||
final int j = 10;
|
||||
System.out.println(j);
|
||||
} while (true);
|
||||
} while (c());
|
||||
}
|
||||
|
||||
boolean c() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -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;
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -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;
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -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;
|
||||
}
|
||||
}
|
||||
+10
@@ -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));
|
||||
}
|
||||
}
|
||||
+10
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user