extract method: disable folding if array is used for writing (IDEA-156818)

This commit is contained in:
Anna Kozlova
2016-05-30 20:01:09 +03:00
parent c09e6aa438
commit c831b8cd3c
4 changed files with 33 additions and 2 deletions
@@ -208,7 +208,9 @@ public class ParametersFolder {
if (expressions == null) {
expressions = new ArrayList<PsiExpression>();
while (expression != null) {
if (isAccessedForWriting((PsiExpression)expression)) return null;
if (isAccessedForWriting((PsiExpression)expression)) {
return null;
}
for (PsiElement scopeElement : scopeElements) {
if (PsiTreeUtil.isAncestor(expression, scopeElement, true)) {
expression = null;
@@ -300,7 +302,8 @@ public class ParametersFolder {
PsiElement expression = element;
while (expression != null) {
if (PsiEquivalenceUtil.areElementsEquivalent(expression, expr)) {
return (PsiExpression)expression;
PsiExpression psiExpression = (PsiExpression)expression;
return PsiUtil.isAccessedForWriting(psiExpression) ? null : psiExpression;
}
expression = PsiTreeUtil.getParentOfType(expression, PsiExpression.class);
}
@@ -0,0 +1,10 @@
class Test {
void f(int size, int[] array) {
for (int i = 0; i < size; i++) {
int x = i;
<selection>int tmp = array[i];
array[i] = array[x];
array[x] = tmp;</selection>
}
}
}
@@ -0,0 +1,14 @@
class Test {
void f(int size, int[] array) {
for (int i = 0; i < size; i++) {
int x = i;
newMethod(array, i, x);
}
}
private void newMethod(int[] array, int i, int x) {
int tmp = array[i];
array[i] = array[x];
array[x] = tmp;
}
}
@@ -350,6 +350,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
doTest();
}
public void testStopFoldingForArrayWriteAccessInConsequentUsages() throws Exception {
doTest();
}
public void testStopFoldingPostfixInside() throws Exception {
doTest();
}