IDEA-135846 Dfa should warn about qualifiers in method references
authorpeter <peter@jetbrains.com>
Mon, 2 Feb 2015 11:38:40 +0000 (12:38 +0100)
committerpeter <peter@jetbrains.com>
Mon, 2 Feb 2015 11:42:56 +0000 (12:42 +0100)
java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ControlFlowAnalyzer.java
java/java-tests/testData/inspection/dataFlow/fixture/MethodReferenceOnNullable.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspection8Test.java

index 2274ac1221d5e74696aa8a4fdffb566cdfdce3e9..38d4857c7514479a05ecfbbf13eaf77bbd8a24b5 100644 (file)
@@ -690,6 +690,21 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
     finishElement(switchStmt);
   }
 
+  @Override
+  public void visitMethodReferenceExpression(PsiMethodReferenceExpression expression) {
+    startElement(expression);
+
+    PsiExpression qualifier = expression.getQualifierExpression();
+    if (qualifier != null) {
+      qualifier.accept(this);
+      addInstruction(new FieldReferenceInstruction(qualifier, "Method reference qualifier"));
+    }
+
+    addInstruction(new PushInstruction(myFactory.createTypeValue(expression.getFunctionalInterfaceType(), Nullness.NOT_NULL), expression));
+
+    finishElement(expression);
+  }
+
   @Override public void visitSynchronizedStatement(PsiSynchronizedStatement statement) {
     startElement(statement);
 
diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/MethodReferenceOnNullable.java b/java/java-tests/testData/inspection/dataFlow/fixture/MethodReferenceOnNullable.java
new file mode 100644 (file)
index 0000000..7b27c2e
--- /dev/null
@@ -0,0 +1,18 @@
+import org.jetbrains.annotations.Nullable;
+
+class BrokenAlignment {
+
+  {
+    Runnable t = <warning descr="Dereference of 'getString()' may produce 'java.lang.NullPointerException'">getString()</warning>::length;
+    Runnable t2 = BrokenAlignment::getStringStatic;
+  }
+
+  @Nullable
+  private String getString() {
+    return null;
+  }
+  @Nullable
+  private static String getStringStatic() {
+    return null;
+  }
+}
\ No newline at end of file
index 9581ab124eccba2ce8bf78b05bee4b371a0604a2..34b1c015678f3370f3e696ebf56f3303cb66c2b3 100644 (file)
@@ -76,6 +76,8 @@ public class DataFlowInspection8Test extends LightCodeInsightFixtureTestCase {
     });
   }
 
+  public void testMethodReferenceOnNullable() { doTest(); }
+
   public void testNullableForeachVariable() {
     setupCustomAnnotations();
     doTest();