forbid PsiExpressionStatement as functional expression context; prefer not a statemen...
authorAnna.Kozlova <anna.kozlova@jetbrains.com>
Wed, 5 Oct 2016 08:29:22 +0000 (10:29 +0200)
committerAnna.Kozlova <anna.kozlova@jetbrains.com>
Wed, 5 Oct 2016 08:31:37 +0000 (10:31 +0200)
java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java
java/java-psi-api/src/com/intellij/psi/LambdaUtil.java

index 448c427c445c9463bad6e211d6cd15bd51959436..1a648d2a1aed2753bcbd32c829b09803c66b502c 100644 (file)
@@ -323,8 +323,10 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
   @Override
   public void visitLambdaExpression(PsiLambdaExpression expression) {
     myHolder.add(checkFeature(expression, Feature.LAMBDA_EXPRESSIONS));
+    final PsiElement parent = PsiUtil.skipParenthesizedExprUp(expression.getParent());
+    if (parent instanceof PsiExpressionStatement) return;
     if (!myHolder.hasErrorResults()) {
-      if (LambdaUtil.isValidLambdaContext(expression.getParent())) {
+      if (LambdaUtil.isValidLambdaContext(parent)) {
         final PsiType functionalInterfaceType = expression.getFunctionalInterfaceType();
         if (functionalInterfaceType != null) {
           final String notFunctionalMessage = LambdaHighlightingUtil.checkInterfaceFunctional(functionalInterfaceType);
@@ -335,7 +337,6 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
             myHolder.add(result);
           }
           else {
-            final PsiElement parent = PsiUtil.skipParenthesizedExprUp(expression.getParent());
             final PsiCallExpression callExpression = parent instanceof PsiExpressionList && parent.getParent() instanceof PsiCallExpression ?
                                                      (PsiCallExpression)parent.getParent() : null;
             final JavaResolveResult containingCallResolveResult = callExpression != null ? callExpression.resolveMethodGenerics() : null;
@@ -1294,6 +1295,8 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
   @Override
   public void visitMethodReferenceExpression(PsiMethodReferenceExpression expression) {
     myHolder.add(checkFeature(expression, Feature.METHOD_REFERENCES));
+    final PsiElement parent = PsiUtil.skipParenthesizedExprUp(expression.getParent());
+    if (parent instanceof PsiExpressionStatement) return;
 
     final JavaResolveResult result;
     final JavaResolveResult[] results;
@@ -1324,7 +1327,7 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
       myHolder.add(HighlightNamesUtil.highlightClassNameInQualifier(expression, colorsScheme));
     }
 
-    if (!LambdaUtil.isValidLambdaContext(expression.getParent())) {
+    if (!LambdaUtil.isValidLambdaContext(parent)) {
       String description = "Method reference expression is not expected here";
       myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create());
     }
index e73458927ec5f2d940945d21cebf004a5e7a4c11..8535e5756a0c3a2369ebe1b7c6e226ff9c04e676 100644 (file)
@@ -129,8 +129,7 @@ public class LambdaUtil {
     context = PsiUtil.skipParenthesizedExprUp(context);
     return isAssignmentOrInvocationContext(context) ||
            context instanceof PsiTypeCastExpression ||
-           context instanceof PsiConditionalExpression && isAssignmentOrInvocationContext(PsiUtil.skipParenthesizedExprUp(context.getParent())) ||
-           context instanceof PsiExpressionStatement;
+           context instanceof PsiConditionalExpression && isAssignmentOrInvocationContext(PsiUtil.skipParenthesizedExprUp(context.getParent()));
   }
 
   private static boolean isAssignmentOrInvocationContext(PsiElement context) {