[groovy] properly check new expressions equivalency (IDEA-152085)
authorDaniil Ovchinnikov <daniil.ovchinnikov@jetbrains.com>
Mon, 31 Oct 2016 16:29:05 +0000 (19:29 +0300)
committerDaniil Ovchinnikov <daniil.ovchinnikov@jetbrains.com>
Mon, 31 Oct 2016 16:30:11 +0000 (19:30 +0300)
plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/utils/EquivalenceChecker.java
plugins/groovy/test/org/jetbrains/plugins/groovy/codeInspection/control/GroovyIdenticalBranchesInspectionTest.groovy [new file with mode: 0644]
plugins/groovy/testdata/inspections/identicalBranches/TwoNewExpressions.groovy [new file with mode: 0644]

index 03deb3411eb95f6642f2d55f6f2fa8abcb60d7f1..9522217269e29c327b46d077a9b5215e11fb7e9f 100644 (file)
@@ -658,7 +658,7 @@ public class EquivalenceChecker {
                                                      @NotNull GrNewExpression newExp2) {
     final PsiMethod constructor1 = newExp1.resolveMethod();
     final PsiMethod constructor2 = newExp2.resolveMethod();
-    if (constructor1 == null || constructor2 == null || constructor1.equals(constructor2)) {
+    if (constructor1 == null || constructor2 == null || !constructor1.equals(constructor2)) {
       return false;
     }
     return argumentListsAreEquivalent(newExp1.getArgumentList(), newExp2.getArgumentList());
diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/codeInspection/control/GroovyIdenticalBranchesInspectionTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/codeInspection/control/GroovyIdenticalBranchesInspectionTest.groovy
new file mode 100644 (file)
index 0000000..004c33a
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2000-2016 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jetbrains.plugins.groovy.codeInspection.control
+
+import com.intellij.testFramework.LightProjectDescriptor
+import groovy.transform.CompileStatic
+import org.jetbrains.plugins.groovy.GroovyLightProjectDescriptor
+import org.jetbrains.plugins.groovy.lang.highlighting.GrHighlightingTestBase
+import org.jetbrains.plugins.groovy.util.TestUtils
+
+@CompileStatic
+class GroovyIdenticalBranchesInspectionTest extends GrHighlightingTestBase {
+
+  LightProjectDescriptor projectDescriptor = GroovyLightProjectDescriptor.GROOVY_LATEST
+  String basePath = TestUtils.testDataPath + "inspections/identicalBranches/"
+
+  @Override
+  void setUp() throws Exception {
+    super.setUp()
+    fixture.enableInspections(GroovyConditionalWithIdenticalBranchesInspection, GroovyIfStatementWithIdenticalBranchesInspection)
+  }
+
+  void 'test two new expressions'() { doTest() }
+
+  @Override
+  protected String getTestName(boolean lowercaseFirstLetter) {
+    def name = getName()
+    if (name.contains(" ")) {
+      name = name.split(" ").collect { String it -> it == "test" ? it : it.capitalize() }.join('')
+    }
+    return getTestName(name, lowercaseFirstLetter)
+  }
+}
diff --git a/plugins/groovy/testdata/inspections/identicalBranches/TwoNewExpressions.groovy b/plugins/groovy/testdata/inspections/identicalBranches/TwoNewExpressions.groovy
new file mode 100644 (file)
index 0000000..5cd0b0c
--- /dev/null
@@ -0,0 +1,20 @@
+class IfStatementWithIdenticalBranchesTest {
+  void test(boolean b) {
+    b ? new Foo() : new Bar()
+
+    if (b) {
+      new Foo()
+    }
+    else {
+      new Bar()
+    }
+  }
+}
+
+class Foo {
+  Foo() {}
+}
+
+class Bar {
+  Bar() {}
+}