mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Java: Fixed removing unused variable occurrences from 'for' loop's update and init clauses (IDEA-180217)
This commit is contained in:
+20
-2
@@ -59,6 +59,12 @@ public class RemoveUnusedVariableUtil {
|
||||
elementToReplace = element.getParent();
|
||||
expressionToReplaceWith =
|
||||
factory.createStatementFromText((expression == null ? "" : expression.getText()) + ";", null);
|
||||
if (isForLoopUpdate(elementToReplace)) {
|
||||
PsiElement lastChild = expressionToReplaceWith.getLastChild();
|
||||
if (PsiUtil.isJavaToken(lastChild, JavaTokenType.SEMICOLON)) {
|
||||
lastChild.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (element.getParent() instanceof PsiDeclarationStatement) {
|
||||
expressionToReplaceWith =
|
||||
@@ -83,7 +89,7 @@ public class RemoveUnusedVariableUtil {
|
||||
// just delete it altogether
|
||||
if (element.getParent() instanceof PsiExpressionStatement) {
|
||||
PsiExpressionStatement parent = (PsiExpressionStatement)element.getParent();
|
||||
if (parent.getParent() instanceof PsiCodeBlock) {
|
||||
if (parent.getParent() instanceof PsiCodeBlock || isForLoopUpdate(parent)) {
|
||||
parent.delete();
|
||||
}
|
||||
else {
|
||||
@@ -134,7 +140,7 @@ public class RemoveUnusedVariableUtil {
|
||||
if (rExpression == null) return true;
|
||||
// replace assignment with expression and resimplify
|
||||
boolean sideEffectFound = checkSideEffects(rExpression, variable, sideEffects);
|
||||
if (!(element.getParent() instanceof PsiExpressionStatement) || PsiUtil.isStatement(rExpression)) {
|
||||
if (!isStatementExpression(expression) || PsiUtil.isStatement(rExpression)) {
|
||||
if (deleteMode == RemoveMode.MAKE_STATEMENT ||
|
||||
deleteMode == RemoveMode.DELETE_ALL && !(element.getParent() instanceof PsiExpressionStatement)) {
|
||||
element = replaceElementWithExpression(rExpression, factory, element);
|
||||
@@ -201,4 +207,16 @@ public class RemoveUnusedVariableUtil {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean isForLoopUpdate(PsiElement element) {
|
||||
PsiElement parent = element.getParent();
|
||||
return parent instanceof PsiForStatement &&
|
||||
((PsiForStatement)parent).getUpdate() == element;
|
||||
}
|
||||
|
||||
private static boolean isStatementExpression(PsiExpression expression) {
|
||||
PsiElement parent = expression.getParent();
|
||||
return parent instanceof PsiExpressionStatement ||
|
||||
parent instanceof PsiExpressionList && parent.getParent() instanceof PsiExpressionListStatement;
|
||||
}
|
||||
}
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Remove variable 'problematic'" "true"
|
||||
class C {
|
||||
Object foo = null;
|
||||
|
||||
void case01() {
|
||||
int i = 10;
|
||||
for(; (--i) > 0; ) {
|
||||
System.out.println("index = " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Remove variable 'problematic'" "true"
|
||||
class C {
|
||||
Object foo = null;
|
||||
|
||||
void case01() {
|
||||
int i;
|
||||
for(i = 10; (--i) > 0; ) {
|
||||
System.out.println("index = " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Remove variable 'problematic'" "true"
|
||||
class C {
|
||||
Object foo() {return null;}
|
||||
|
||||
void case01() {
|
||||
int i = 10;
|
||||
for(foo(); (--i) > 0; ) {
|
||||
System.out.println("index = " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Remove variable 'problematic'" "true"
|
||||
class C {
|
||||
Object foo() {return null;}
|
||||
|
||||
void case01() {
|
||||
int i;
|
||||
for(i = 10, foo(); (--i) > 0; ) {
|
||||
System.out.println("index = " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Remove variable 'problematic'" "true"
|
||||
class C {
|
||||
Object foo = null;
|
||||
|
||||
void case01() {
|
||||
for(int i = 10; (--i) > 0; ) {
|
||||
System.out.println("index = " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Remove variable 'problematic'" "true"
|
||||
class C {
|
||||
Object foo = null;
|
||||
|
||||
void case02() {
|
||||
for(int i = 10; i > 0; i--) {
|
||||
System.out.println("index = " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Remove variable 'problematic'" "true"
|
||||
class C {
|
||||
Object foo() {return null;}
|
||||
|
||||
void case01() {
|
||||
for(int i = 10; (--i) > 0; foo()) {
|
||||
System.out.println("index = " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Remove variable 'problematic'" "true"
|
||||
class C {
|
||||
Object foo() {return null;}
|
||||
|
||||
void case01() {
|
||||
for(int i = 10; i > 0; --i, foo()) {
|
||||
System.out.println("index = " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Remove variable 'problematic'" "true"
|
||||
class C {
|
||||
Object foo = null;
|
||||
|
||||
void case01() {
|
||||
Object <caret>problematic;
|
||||
int i = 10;
|
||||
for(problematic = foo; (--i) > 0; ) {
|
||||
System.out.println("index = " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Remove variable 'problematic'" "true"
|
||||
class C {
|
||||
Object foo = null;
|
||||
|
||||
void case01() {
|
||||
Object <caret>problematic;
|
||||
int i;
|
||||
for(i = 10, problematic = foo; (--i) > 0; ) {
|
||||
System.out.println("index = " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Remove variable 'problematic'" "true"
|
||||
class C {
|
||||
Object foo() {return null;}
|
||||
|
||||
void case01() {
|
||||
Object <caret>problematic;
|
||||
int i = 10;
|
||||
for(problematic = foo(); (--i) > 0; ) {
|
||||
System.out.println("index = " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Remove variable 'problematic'" "true"
|
||||
class C {
|
||||
Object foo() {return null;}
|
||||
|
||||
void case01() {
|
||||
Object <caret>problematic;
|
||||
int i;
|
||||
for(i = 10, problematic = foo(); (--i) > 0; ) {
|
||||
System.out.println("index = " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Remove variable 'problematic'" "true"
|
||||
class C {
|
||||
Object foo = null;
|
||||
|
||||
void case01() {
|
||||
Object <caret>problematic;
|
||||
for(int i = 10; (--i) > 0; problematic = foo) {
|
||||
System.out.println("index = " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Remove variable 'problematic'" "true"
|
||||
class C {
|
||||
Object foo = null;
|
||||
|
||||
void case02() {
|
||||
Object <caret>problematic;
|
||||
for(int i = 10; i > 0; i--, problematic = foo) {
|
||||
System.out.println("index = " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Remove variable 'problematic'" "true"
|
||||
class C {
|
||||
Object foo() {return null;}
|
||||
|
||||
void case01() {
|
||||
Object <caret>problematic;
|
||||
for(int i = 10; (--i) > 0; problematic = foo()) {
|
||||
System.out.println("index = " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Remove variable 'problematic'" "true"
|
||||
class C {
|
||||
Object foo() {return null;}
|
||||
|
||||
void case01() {
|
||||
Object <caret>problematic;
|
||||
for(int i = 10; i > 0; --i, problematic = foo()) {
|
||||
System.out.println("index = " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user