remove unused assignment: do not move the transformed statement up as it could change the semantic and is not evident (IDEA-123841)

This commit is contained in:
Anna Kozlova
2014-04-14 20:21:52 +02:00
parent 36771d7d8e
commit b708f2006e
3 changed files with 37 additions and 2 deletions

View File

@@ -118,8 +118,13 @@ public class DefUseInspection extends DefUseInspectionBase {
else if (res == RemoveUnusedVariableUtil.MAKE_STATEMENT) {
final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
final PsiStatement statementFromText = factory.createStatementFromText(psiInitializer.getText() + ";", null);
declaration.getParent().addAfter(statementFromText, declaration);
elementToDelete.delete();
final PsiElement parent = elementToDelete.getParent();
if (parent instanceof PsiExpressionStatement) {
parent.replace(statementFromText);
} else {
declaration.getParent().addAfter(statementFromText, declaration);
elementToDelete.delete();
}
}
}
catch (IncorrectOperationException e) {

View File

@@ -0,0 +1,15 @@
// "Remove redundant assignment" "true"
class A {
A a = null;
String m(String str) {
return str;
}
{
String ss = "";
System.out.println();
a.m(ss);
}
}

View File

@@ -0,0 +1,15 @@
// "Remove redundant assignment" "true"
class A {
A a = null;
String m(String str) {
return str;
}
{
String ss = "";
System.out.println();
s<caret>s = a.m(ss);
}
}