IDEA-130024 (False warning 'if statement with identical branches')

This commit is contained in:
Bas Leijdekkers
2016-06-15 18:59:33 +03:00
parent 77edf02edc
commit 3894dc0f7a
6 changed files with 96 additions and 3 deletions

View File

@@ -119,7 +119,7 @@ public class DuplicatesFinder {
}
@Nullable
public Match isDuplicate(PsiElement element, boolean ignoreParameterTypesAndPostVariableUsages) {
public Match isDuplicate(@NotNull PsiElement element, boolean ignoreParameterTypesAndPostVariableUsages) {
annotatePattern();
Match match = isDuplicateFragment(element, ignoreParameterTypesAndPostVariableUsages);
deannotatePattern();
@@ -174,7 +174,7 @@ public class DuplicatesFinder {
@Nullable
private Match isDuplicateFragment(PsiElement candidate, boolean ignoreParameterTypesAndPostVariableUsages) {
private Match isDuplicateFragment(@NotNull PsiElement candidate, boolean ignoreParameterTypesAndPostVariableUsages) {
if (isSelf(candidate)) return null;
PsiElement sibling = candidate;
ArrayList<PsiElement> candidates = new ArrayList<PsiElement>();
@@ -215,7 +215,7 @@ public class DuplicatesFinder {
return match;
}
protected boolean isSelf(PsiElement candidate) {
protected boolean isSelf(@NotNull PsiElement candidate) {
for (PsiElement pattern : myPattern) {
if (PsiTreeUtil.isAncestor(pattern, candidate, false)) {
return true;
@@ -621,6 +621,7 @@ public class DuplicatesFinder {
if (resolveResult1 instanceof PsiMethod && resolveResult2 instanceof PsiMethod) {
final PsiMethod method1 = (PsiMethod)resolveResult1;
final PsiMethod method2 = (PsiMethod)resolveResult2;
if (method1.hasModifierProperty(PsiModifier.STATIC) && !method1.equals(method2)) return false;
if (ArrayUtil.find(method1.findSuperMethods(), method2) >= 0) return true;
if (ArrayUtil.find(method2.findSuperMethods(), method1) >= 0) return true;

View File

@@ -0,0 +1,19 @@
class C {
Object foo(boolean b) {
if (b) {
<selection>return A.getInstance();</selection>
} else {
return B.getInstance();
}
}
}
class A {
static A getInstance() {
return new A();
}
}
class B extends A {
static B getInstance() {
return new B();
}
}

View File

@@ -0,0 +1,16 @@
class C {
Object foo(boolean b) {
if (b) {
<selection>return A.getInstance();</selection>
} else {
return B.getInstance();
}
}
}
class A {
static A getInstance() {
return new A();
}
}
class B extends A {
}

View File

@@ -0,0 +1,23 @@
import org.jetbrains.annotations.NotNull;
class C {
Object foo(boolean b) {
if (b) {
return newMethod();
} else {
return newMethod();
}
}
@NotNull
private Object newMethod() {
return A.getInstance();
}
}
class A {
static A getInstance() {
return new A();
}
}
class B extends A {
}

View File

@@ -0,0 +1,26 @@
import org.jetbrains.annotations.NotNull;
class C {
Object foo(boolean b) {
if (b) {
return newMethod();
} else {
return B.getInstance();
}
}
@NotNull
private Object newMethod() {
return A.getInstance();
}
}
class A {
static A getInstance() {
return new A();
}
}
class B extends A {
static B getInstance() {
return new B();
}
}

View File

@@ -245,6 +245,14 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
doDuplicatesTest();
}
public void testClassReference() throws Exception {
doDuplicatesTest();
}
public void testClassReference2() throws Exception {
doDuplicatesTest();
}
public void testCodeDuplicates() throws Exception {
doDuplicatesTest();
}