mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
VariableExtractor: do not rewrite while-loop condition if occurrences appear outside of the loop (IDEA-218704)
GitOrigin-RevId: 213471de9a6d623336673a3b968ec4724215012a
This commit is contained in:
committed by
intellij-monorepo-bot
parent
b24cd97fa1
commit
2b0a160b8a
+3
-2
@@ -279,8 +279,9 @@ class VariableExtractor {
|
||||
Set<PsiExpression> allOccurrences = StreamEx.of(occurrences).append(expr).toSet();
|
||||
PsiExpression firstOccurrence = Collections.min(allOccurrences, Comparator.comparing(e -> e.getTextRange().getStartOffset()));
|
||||
if (anchor instanceof PsiWhileStatement) {
|
||||
PsiExpression condition = ((PsiWhileStatement)anchor).getCondition();
|
||||
if (condition != null) {
|
||||
PsiWhileStatement whileStatement = (PsiWhileStatement)anchor;
|
||||
PsiExpression condition = whileStatement.getCondition();
|
||||
if (condition != null && allOccurrences.stream().allMatch(occurrence -> PsiTreeUtil.isAncestor(whileStatement, occurrence, true))) {
|
||||
if (firstOccurrence != null && PsiTreeUtil.isAncestor(condition, firstOccurrence, false)) {
|
||||
PsiPolyadicExpression polyadic = ObjectUtils.tryCast(PsiUtil.skipParenthesizedExprDown(condition), PsiPolyadicExpression.class);
|
||||
if (polyadic != null && JavaTokenType.ANDAND.equals(polyadic.getOperationTokenType())) {
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
import java.util.Arrays;
|
||||
|
||||
class Test {
|
||||
void foo(int[] a) {
|
||||
int log = 0;
|
||||
int temp = a.length;
|
||||
while (1 << log < temp) {
|
||||
log++;
|
||||
}
|
||||
int n = 1 << log;
|
||||
int[] b = new int[2 * n];
|
||||
System.arraycopy(a, 0, b, n, temp);
|
||||
Arrays.fill(b, n + temp, 2 * n, -1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import java.util.Arrays;
|
||||
|
||||
class Test {
|
||||
void foo(int[] a) {
|
||||
int log = 0;
|
||||
while (1 << log < <selection>a.length</selection>) {
|
||||
log++;
|
||||
}
|
||||
int n = 1 << log;
|
||||
int[] b = new int[2 * n];
|
||||
System.arraycopy(a, 0, b, n, a.length);
|
||||
Arrays.fill(b, n + a.length, 2 * n, -1);
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
import java.util.Arrays;
|
||||
|
||||
class Test {
|
||||
void foo(int[] a) {
|
||||
int log = 0;
|
||||
int temp = a.length;
|
||||
while (1 << log < temp) {
|
||||
log++;
|
||||
}
|
||||
int n = 1 << log;
|
||||
int[] b = new int[2 * n];
|
||||
System.arraycopy(a, 0, b, n, temp);
|
||||
Arrays.fill(b, n + temp, 2 * n, -1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import java.util.Arrays;
|
||||
|
||||
class Test {
|
||||
void foo(int[] a) {
|
||||
int log = 0;
|
||||
while (1 << log < a.length) {
|
||||
log++;
|
||||
}
|
||||
int n = 1 << log;
|
||||
int[] b = new int[2 * n];
|
||||
System.arraycopy(a, 0, b, n, <selection>a.length</selection>);
|
||||
Arrays.fill(b, n + a.length, 2 * n, -1);
|
||||
}
|
||||
}
|
||||
@@ -93,6 +93,8 @@ public class IntroduceVariableTest extends LightJavaCodeInsightTestCase {
|
||||
public void testWhileCondition2() { doTest("temp", true, false, false, "Node"); }
|
||||
public void testWhileConditionIncomplete() { doTest("temp", true, false, false, "boolean"); }
|
||||
public void testWhileConditionNoBrace() { doTest("temp", true, false, false, "int"); }
|
||||
public void testWhileConditionPlusNormal() { doTest("temp", true, false, false, "int"); }
|
||||
public void testWhileConditionPlusNormal2() { doTest("temp", true, false, false, "int"); }
|
||||
public void testField() { doTest("temp", false, false, false, CommonClassNames.JAVA_LANG_STRING); }
|
||||
public void testFieldAll() { doTest("temp", true, false, false, CommonClassNames.JAVA_LANG_STRING); }
|
||||
public void testCaseLabel() { doTest("temp", true, false, false, "int"); }
|
||||
|
||||
Reference in New Issue
Block a user