conditional expression with similar branches doesn't distinguish different constructors in PsiNewExpression (IDEA-149822)

This commit is contained in:
Dmitry Batkovich
2015-12-29 14:42:09 +03:00
parent 053ac359db
commit f78dbdba4c
3 changed files with 28 additions and 0 deletions
@@ -15,6 +15,7 @@
*/
package com.siyeh.ig.psiutils;
import com.intellij.openapi.util.Comparing;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
@@ -775,6 +776,11 @@ public class EquivalenceChecker {
if (!expressionsAreEquivalentDecision(arrayInitializer1, arrayInitializer2).getExactlyMatches()) {
return EXACTLY_UN_MATCHES;
}
final PsiMethod constructor1 = newExpression1.resolveConstructor();
final PsiMethod constructor2 = newExpression2.resolveConstructor();
if (!Comparing.equal(constructor1, constructor2)) {
return EXACTLY_UN_MATCHES;
}
final PsiExpression qualifier1 = newExpression1.getQualifier();
final PsiExpression qualifier2 = newExpression2.getQualifier();
if (!expressionsAreEquivalentDecision(qualifier1, qualifier2).getExactlyMatches()) {
@@ -47,4 +47,19 @@ class ConditionalExpressionWithIdenticalBranches {
int someMethod(String s, String s2) {
return s.length();
}
class Item {
Item(String name) {
}
Item(int value) {
}
void v() {
int i = 1;
Item item = (i == 1 ? new Item("1") : new Item(i)); // warning here
Item item1 = (i == 1 ? new Item("1") : new Item("2")); // warning here
}
}
}
@@ -55,4 +55,11 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Conditional expression with identical or similar branches</problem_class>
<description>Conditional expression &lt;code&gt;new Random().nextBoolean() ? &quot;2&quot; + &quot;q&quot; + &quot;1&quot; : &quot;2&quot; + &quot;qwe&quot; + &quot;1&quot;&lt;/code&gt; with identical or similar branches #loc</description>
</problem>
<problem>
<file>ConditionalExpressionWithIdenticalBranches.java</file>
<line>62</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Conditional expression with identical or similar branches</problem_class>
<description>Conditional expression &lt;code&gt;i == 1 ? new Item(&quot;1&quot;) : new Item(&quot;2&quot;)&lt;/code&gt; with identical or similar branches #loc</description>
</problem>
</problems>