infer nullity: treat instanceof as null check (IDEA-98153)

This commit is contained in:
anna
2012-12-28 15:20:53 +01:00
parent 299e587bc4
commit dbfd42dd63
3 changed files with 33 additions and 0 deletions

View File

@@ -17,6 +17,7 @@ package com.intellij.codeInspection.inferNullity;
import com.intellij.codeInsight.NullableNotNullManager;
import com.intellij.codeInsight.intention.AddAnnotationFix;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.*;
@@ -37,6 +38,7 @@ import java.util.List;
public class NullityInferrer {
private static final int MAX_PASSES = 10;
public static final String NOTHING_FOUND_TO_INFER = "Nothing found to infer";
private int numAnnotationsAdded = 0;
private final List<SmartPsiElementPointer<? extends PsiModifierListOwner>> myNotNullSet = new ArrayList<SmartPsiElementPointer<? extends PsiModifierListOwner>>();
private final List<SmartPsiElementPointer<? extends PsiModifierListOwner>> myNullableSet = new ArrayList<SmartPsiElementPointer<? extends PsiModifierListOwner>>();
@@ -152,6 +154,9 @@ public class NullityInferrer {
public boolean nothingFoundMessage(final Project project) {
if (myNullableSet.isEmpty() && myNotNullSet.isEmpty()) {
if (ApplicationManager.getApplication().isUnitTestMode()) {
throw new RuntimeException(NOTHING_FOUND_TO_INFER);
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Messages.showInfoMessage(project, "No places found to infer @Nullable/@NotNull", "Infer Nullity Results");
@@ -584,6 +589,9 @@ public class NullityInferrer {
return true;
}
}
else if (parent instanceof PsiInstanceOfExpression) {
return true;
}
else if (parent instanceof PsiReferenceExpression) {
final PsiExpression qualifierExpression = ((PsiReferenceExpression)parent).getQualifierExpression();
if (qualifierExpression == expr) {

View File

@@ -0,0 +1,12 @@
public class C {
private int x;
public boolean equals(Object object) {
if (object instanceof C) {
if (((C)object).x == x) {
return true;
}
}
return false;
}
}

View File

@@ -18,6 +18,7 @@ package com.intellij.codeInsight;
import com.intellij.JavaTestUtil;
import com.intellij.codeInspection.inferNullity.NullityInferrer;
import com.intellij.openapi.roots.ModuleRootModificationUtil;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.vfs.JarFileSystem;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
@@ -45,6 +46,18 @@ public class NullityInferrerTest extends CodeInsightTestCase {
doTest(false);
}
public void testParameterCheckedForInstanceof() throws Exception {
try {
doTest(false);
fail("Should infer nothing");
}
catch (RuntimeException e) {
if (!Comparing.strEqual(e.getMessage(), NullityInferrer.NOTHING_FOUND_TO_INFER)) {
fail();
}
}
}
public void testParameterUsedInForeachIteratedValue() throws Exception {
doTest(false);
}