honor array component type nullability in index expression dfa (IDEA-154874)
authorpeter <peter@jetbrains.com>
Tue, 19 Apr 2016 11:59:29 +0000 (13:59 +0200)
committerpeter <peter@jetbrains.com>
Tue, 19 Apr 2016 12:00:31 +0000 (14:00 +0200)
java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DfaPsiUtil.java
java/java-tests/testData/inspection/dataFlow/fixture/NullableArrayComponent.java

index ed5d849ae81f2e1c4c9930db8a3ec4cf2f90077b..f85cb77dd9ba3d20c9ae05bc31b43ab1460340a7 100644 (file)
@@ -66,26 +66,10 @@ public class DfaPsiUtil {
 
   @NotNull
   public static Nullness getElementNullability(@Nullable PsiType resultType, @Nullable PsiModifierListOwner owner) {
-    if (owner == null || resultType instanceof PsiPrimitiveType) {
-      return Nullness.UNKNOWN;
-    }
-
-    if (owner instanceof PsiEnumConstant || PsiUtil.isAnnotationMethod(owner)) {
-      return Nullness.NOT_NULL;
-    }
-    if (owner instanceof PsiMethod && isEnumValueOf((PsiMethod)owner)) {
-      return Nullness.NOT_NULL;
-    }
-
     if (resultType != null) {
-      NullableNotNullManager nnn = NullableNotNullManager.getInstance(owner.getProject());
       for (PsiAnnotation annotation : resultType.getAnnotations()) {
-        if (!annotation.isValid()) {
-          PsiUtilCore.ensureValid(owner);
-          PsiUtil.ensureValidType(resultType, owner + " of " + owner.getClass());
-          PsiUtilCore.ensureValid(annotation); //should fail
-        }
         String qualifiedName = annotation.getQualifiedName();
+        NullableNotNullManager nnn = NullableNotNullManager.getInstance(annotation.getProject());
         if (nnn.getNullables().contains(qualifiedName)) {
           return Nullness.NULLABLE;
         }
@@ -95,6 +79,17 @@ public class DfaPsiUtil {
       }
     }
 
+    if (owner == null || resultType instanceof PsiPrimitiveType) {
+      return Nullness.UNKNOWN;
+    }
+
+    if (owner instanceof PsiEnumConstant || PsiUtil.isAnnotationMethod(owner)) {
+      return Nullness.NOT_NULL;
+    }
+    if (owner instanceof PsiMethod && isEnumValueOf((PsiMethod)owner)) {
+      return Nullness.NOT_NULL;
+    }
+
     if (NullableNotNullManager.isNullable(owner)) {
       return Nullness.NULLABLE;
     }
index b1222bd7f654953c3a9bdb2b466df5ee836eb3c2..36f3af3e6d5da1baf5d181984e27152befedb714 100644 (file)
@@ -24,4 +24,19 @@ class TestCompilerWarnings {
     m(<warning descr="Argument 'x' might be null">x</warning>);
   }
 
+  void testIteration() {
+    @NotNull String @NotNull [] array = new String[] { "1", "2", "3" };
+    for (int i = 0; i < array.length; i++) {
+      if (<warning descr="Condition 'array[i] == null' is always 'false'">array[i] == null</warning>) {
+        System.out.println("unreachable");
+      }
+    }
+
+    for (String anArray : array) {
+      if (<warning descr="Condition 'anArray == null' is always 'false'">anArray == null</warning>) {
+        System.out.println("unreachable");
+      }
+    }
+  }
+
 }
\ No newline at end of file