inline: warn if assignment with operation (IDEA-80348)
authorAnna Kozlova <anna.kozlova@jetbrains.com>
Fri, 27 Jan 2012 17:46:36 +0000 (21:46 +0400)
committerAnna Kozlova <anna.kozlova@jetbrains.com>
Fri, 27 Jan 2012 17:47:55 +0000 (21:47 +0400)
java/java-impl/src/com/intellij/refactoring/inline/InlineLocalHandler.java
java/java-tests/testData/refactoring/inlineLocal/NonEqAssignment.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/refactoring/inline/InlineLocalTest.java

index 27f6694cdf218f8dc88bb82719a766214fe01800..2046e152a3d4db67723dc88e8f640625d29bf9fb 100644 (file)
@@ -308,7 +308,9 @@ public class InlineLocalHandler extends JavaInlineActionHandler {
       }
 
       if (def instanceof PsiReferenceExpression && def.getParent() instanceof PsiAssignmentExpression) {
-        final PsiExpression rExpr = ((PsiAssignmentExpression)def.getParent()).getRExpression();
+        final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression)def.getParent();
+        if (assignmentExpression.getOperationTokenType() != JavaTokenType.EQ) return null;
+        final PsiExpression rExpr = assignmentExpression.getRExpression();
         if (rExpr != null) return rExpr;
       }
     }
diff --git a/java/java-tests/testData/refactoring/inlineLocal/NonEqAssignment.java b/java/java-tests/testData/refactoring/inlineLocal/NonEqAssignment.java
new file mode 100644 (file)
index 0000000..c69ec61
--- /dev/null
@@ -0,0 +1,7 @@
+class Test {
+  void foo() {
+    int x = 1;
+    x += 1;
+    int y = <caret>x;
+  }
+}
\ No newline at end of file
index e4bde4df16c0c4ac6dea4399d6c5fe41b4003442..472d877af6ad32d0f63576abfdcad0f0a0c59781 100644 (file)
@@ -132,6 +132,10 @@ public class InlineLocalTest extends LightCodeInsightTestCase {
     doTest(true);
   }
 
+  public void testNonEqAssignment() throws Exception {
+    doTest(false, "Cannot perform refactoring.\n" +
+                  "Variable 'x' is accessed for writing.");
+  }
 
   private void doTest(final boolean inlineDef, String conflictMessage) throws Exception {
     try {