Java: Don't cause endless recursive calls of the equals() method in the inspection... phpstorm/171.1043
authorPavel Dolgov <pavel.dolgov@jetbrains.com>
Thu, 17 Nov 2016 10:28:57 +0000 (13:28 +0300)
committerPavel Dolgov <pavel.dolgov@jetbrains.com>
Thu, 17 Nov 2016 11:00:35 +0000 (14:00 +0300)
plugins/InspectionGadgets/src/com/siyeh/ig/equality/EqualityOperatorComparesObjectsInspection.java
plugins/InspectionGadgets/test/com/siyeh/igfixes/equality/replace_equality_with_equals/CompareFieldInEqualsMethod.after.java [new file with mode: 0644]
plugins/InspectionGadgets/test/com/siyeh/igfixes/equality/replace_equality_with_equals/CompareFieldInEqualsMethod.java [new file with mode: 0644]
plugins/InspectionGadgets/test/com/siyeh/igfixes/equality/replace_equality_with_equals/CompareThisInEqualsMethod.java [new file with mode: 0644]
plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/equality/EqualityOperatorComparesObjectsInspectionTestBase.java

index 57d00fc9dcfbadd871713c1711344b3cacf647d3..39d789872eaf0d130489fb6983cce864acf56129 100644 (file)
@@ -19,6 +19,7 @@ import com.intellij.codeInspection.ProblemDescriptor;
 import com.intellij.openapi.project.Project;
 import com.intellij.psi.*;
 import com.intellij.psi.tree.IElementType;
+import com.intellij.psi.util.PsiTreeUtil;
 import com.intellij.psi.util.PsiUtil;
 import com.intellij.psi.util.TypeConversionUtil;
 import com.siyeh.InspectionGadgetsBundle;
@@ -208,6 +209,12 @@ public class EqualityOperatorComparesObjectsInspection extends BaseInspection {
       if (rhsType == null || rhsType instanceof PsiPrimitiveType || TypeConversionUtil.isEnumType(rhsType)) {
         return;
       }
+      if (lhs instanceof PsiThisExpression || rhs instanceof PsiThisExpression) {
+        final PsiMethod method = PsiTreeUtil.getParentOfType(expression, PsiMethod.class);
+        if (method != null && "equals".equals(method.getName())) {
+          return;
+        }
+      }
       final String operationText = expression.getOperationSign().getText();
       final String prefix = tokenType.equals(JavaTokenType.NE) ? "!" : "";
       registerError(expression, operationText, prefix);
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/equality/replace_equality_with_equals/CompareFieldInEqualsMethod.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/equality/replace_equality_with_equals/CompareFieldInEqualsMethod.after.java
new file mode 100644 (file)
index 0000000..c3f00c5
--- /dev/null
@@ -0,0 +1,14 @@
+class Demo {
+  private Object field;
+
+  @Override
+  public boolean equals(Object obj) {
+    if (this == obj) {
+      return true;
+    }
+    else {
+      Demo other = (Demo)obj;
+      return field.equals(other.field);
+    }
+  }
+}
\ No newline at end of file
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/equality/replace_equality_with_equals/CompareFieldInEqualsMethod.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/equality/replace_equality_with_equals/CompareFieldInEqualsMethod.java
new file mode 100644 (file)
index 0000000..28f9474
--- /dev/null
@@ -0,0 +1,14 @@
+class Demo {
+  private Object field;
+
+  @Override
+  public boolean equals(Object obj) {
+    if (this == obj) {
+      return true;
+    }
+    else {
+      Demo other = (Demo)obj;
+      return field <caret>== other.field;
+    }
+  }
+}
\ No newline at end of file
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/equality/replace_equality_with_equals/CompareThisInEqualsMethod.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/equality/replace_equality_with_equals/CompareThisInEqualsMethod.java
new file mode 100644 (file)
index 0000000..f2a9bfb
--- /dev/null
@@ -0,0 +1,14 @@
+class Demo {
+  private Object field;
+
+  @Override
+  public boolean equals(Object obj) {
+    if (this <caret>== obj) {
+      return true;
+    }
+    else {
+      Demo other = (Demo)obj;
+      return field == other.field;
+    }
+  }
+}
\ No newline at end of file
index be5f15a96be7500c61d67121765766061bc7e3ad..b49cca0b7232c567229451b17a019e0a444b07c6 100644 (file)
@@ -30,6 +30,8 @@ public abstract class EqualityOperatorComparesObjectsInspectionTestBase extends
   public void testPrimitiveComparison() { assertQuickfixNotAvailable(); }
   public void testSimpleObjectComparison() { doTest(true, false); }
   public void testNegatedObjectComparison() { doTest(false, false); }
+  public void testCompareThisInEqualsMethod() { assertQuickfixNotAvailable(); }
+  public void testCompareFieldInEqualsMethod() { doTest(true, false); }
 
   @Override
   protected void setUp() throws Exception {