diff --git a/java/java-psi-api/src/com/intellij/psi/LambdaUtil.java b/java/java-psi-api/src/com/intellij/psi/LambdaUtil.java index f2982adfb75a..327a486d8d79 100644 --- a/java/java-psi-api/src/com/intellij/psi/LambdaUtil.java +++ b/java/java-psi-api/src/com/intellij/psi/LambdaUtil.java @@ -311,7 +311,12 @@ public class LambdaUtil { return ((PsiArrayType)psiType).getComponentType(); } } else if (parent instanceof PsiTypeCastExpression) { - final PsiType castType = ((PsiTypeCastExpression)parent).getType(); + //ensure no capture is performed to target type of cast expression, from 15.16 Cast Expressions: + //Casts can be used to explicitly "tag" a lambda expression or a method reference expression with a particular target type. + //To provide an appropriate degree of flexibility, the target type may be a list of types denoting an intersection type, + // provided the intersection induces a functional interface (ยง9.8). + final PsiTypeElement castTypeElement = ((PsiTypeCastExpression)parent).getCastType(); + final PsiType castType = castTypeElement != null ? castTypeElement.getType() : null; if (castType instanceof PsiIntersectionType) { final PsiType conjunct = extractFunctionalConjunct((PsiIntersectionType)castType); if (conjunct != null) return conjunct; diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/newLambda/EnsureNoCaptureIsPerformedOverTargetTypeOfCastExpressionWhichMarksFunctionalExpression.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/newLambda/EnsureNoCaptureIsPerformedOverTargetTypeOfCastExpressionWhichMarksFunctionalExpression.java new file mode 100644 index 000000000000..a98171eca9a9 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/newLambda/EnsureNoCaptureIsPerformedOverTargetTypeOfCastExpressionWhichMarksFunctionalExpression.java @@ -0,0 +1,7 @@ +import java.util.function.Supplier; + +class Test { + public static void casts(Runnable e) { + Object o = (Supplier) () -> e; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/NewLambdaHighlightingTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/NewLambdaHighlightingTest.java index 8e6db15c9bff..7afe53b19d15 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/NewLambdaHighlightingTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/NewLambdaHighlightingTest.java @@ -20,8 +20,6 @@ import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection; import com.intellij.openapi.projectRoots.JavaSdkVersion; import com.intellij.openapi.projectRoots.Sdk; import com.intellij.testFramework.IdeaTestUtil; -import junit.framework.Test; -import junit.framework.TestSuite; import org.jetbrains.annotations.NonNls; public class NewLambdaHighlightingTest extends LightDaemonAnalyzerTestCase { @@ -193,6 +191,10 @@ public class NewLambdaHighlightingTest extends LightDaemonAnalyzerTestCase { doTest(false); } + public void testEnsureNoCaptureIsPerformedOverTargetTypeOfCastExpressionWhichMarksFunctionalExpression() throws Exception { + doTest(); + } + private void doTest() { doTest(false); }