mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
don't open capture if reference expression is returned from lambda expression (IDEA-139089)
This commit is contained in:
@@ -424,6 +424,12 @@ public class LambdaUtil {
|
||||
return typeByExpression instanceof PsiMethodReferenceType || typeByExpression instanceof PsiLambdaExpressionType || typeByExpression instanceof PsiLambdaParameterType;
|
||||
}
|
||||
|
||||
public static boolean isLambdaReturnExpression(PsiElement element) {
|
||||
final PsiElement parent = element.getParent();
|
||||
return parent instanceof PsiLambdaExpression ||
|
||||
parent instanceof PsiReturnStatement && PsiTreeUtil.getParentOfType(parent, PsiLambdaExpression.class, true, PsiMethod.class) != null;
|
||||
}
|
||||
|
||||
public static PsiReturnStatement[] getReturnStatements(PsiLambdaExpression lambdaExpression) {
|
||||
final PsiElement body = lambdaExpression.getBody();
|
||||
return body instanceof PsiCodeBlock ? PsiUtil.findReturnStatements((PsiCodeBlock)body) : PsiReturnStatement.EMPTY_ARRAY;
|
||||
|
||||
@@ -415,6 +415,9 @@ public class PsiImplUtil {
|
||||
private static PsiType doNormalizeWildcardByPosition(final PsiType type, @NotNull PsiExpression expression, final PsiExpression toplevel) {
|
||||
if (type instanceof PsiCapturedWildcardType) {
|
||||
final PsiWildcardType wildcardType = ((PsiCapturedWildcardType)type).getWildcard();
|
||||
if (expression instanceof PsiReferenceExpression && LambdaUtil.isLambdaReturnExpression(expression)) {
|
||||
return type;
|
||||
}
|
||||
|
||||
if (PsiUtil.isAccessedForWriting(toplevel)) {
|
||||
return wildcardType.isSuper() ? wildcardType.getBound() : PsiCapturedWildcardType.create(wildcardType, expression);
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
import java.util.function.Function;
|
||||
|
||||
interface I<T>{
|
||||
void foo(Function<T, T> f);
|
||||
}
|
||||
|
||||
class C {
|
||||
void bar(I<?> x) {
|
||||
x.foo(a -> {
|
||||
x.foo(y -> <error descr="Bad return type in lambda expression: capture of ? cannot be converted to capture of ?">a</error>);
|
||||
return a;
|
||||
});
|
||||
}
|
||||
}
|
||||
+4
@@ -195,6 +195,10 @@ public class NewLambdaHighlightingTest extends LightDaemonAnalyzerTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testCaptureInReturnStatementOfLambdaExpression() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
private void doTest() {
|
||||
doTest(false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user