mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
lambda: void-compatibility check for expression's lambda refined
(cherry picked from commit c59e6beb8c42c48a46550b39725587d3aa8f560a)
This commit is contained in:
@@ -18,6 +18,7 @@ package com.intellij.psi;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.psi.util.MethodSignature;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -51,10 +52,15 @@ public class LambdaHighlightingUtil {
|
||||
final PsiElement body = lambdaExpression.getBody();
|
||||
if (body instanceof PsiCodeBlock) {
|
||||
if (!LambdaUtil.getReturnExpressions(lambdaExpression).isEmpty()) return "Unexpected return value";
|
||||
} else if (body instanceof PsiReferenceExpression || body instanceof PsiLiteralExpression) {
|
||||
} else if (body instanceof PsiExpression) {
|
||||
final PsiType type = ((PsiExpression)body).getType();
|
||||
if (type != PsiType.VOID) {
|
||||
return "Incompatible return type " + (type == PsiType.NULL || type == null ? "<null>" : type.getPresentableText()) +" in lambda expression";
|
||||
try {
|
||||
if (!PsiUtil.isStatement(JavaPsiFacade.getElementFactory(body.getProject()).createStatementFromText(body.getText(), body))) {
|
||||
return "Incompatible return type " + (type == PsiType.NULL || type == null ? "<null>" : type.getPresentableText()) + " in lambda expression";
|
||||
}
|
||||
}
|
||||
catch (IncorrectOperationException ignore) {}
|
||||
}
|
||||
}
|
||||
} else if (functionalInterfaceReturnType != null) {
|
||||
|
||||
+3
@@ -7,6 +7,9 @@ public class Main {
|
||||
String s = "";
|
||||
I<Object> arr1 = <error descr="Incompatible return type String in lambda expression">(t) -> s</error>;
|
||||
I<Object> arr2 = (t) -> s.toString();
|
||||
|
||||
I<Integer> i1 = <error descr="Incompatible return type int in lambda expression">i -> i * 2</error>;
|
||||
I<Integer> i2 = <error descr="Incompatible return type int in lambda expression">i -> 2 * i</error>;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user