mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 15:09:39 +07:00
IDEA-156007 'Constant conditions & exceptions' inspection should propagate @Nullable/@NotNull annotations to lambda parameters
This commit is contained in:
@@ -22,6 +22,7 @@ import com.intellij.codeInspection.dataFlow.instructions.ReturnInstruction;
|
||||
import com.intellij.codeInspection.dataFlow.value.DfaValueFactory;
|
||||
import com.intellij.lang.java.JavaLanguage;
|
||||
import com.intellij.openapi.util.Ref;
|
||||
import com.intellij.patterns.PsiJavaPatterns;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.search.LocalSearchScope;
|
||||
import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
@@ -97,6 +98,15 @@ public class DfaPsiUtil {
|
||||
return Nullness.NOT_NULL;
|
||||
}
|
||||
|
||||
if (PsiJavaPatterns.psiParameter().withParents(PsiParameterList.class, PsiLambdaExpression.class).accepts(owner)) {
|
||||
PsiLambdaExpression lambda = (PsiLambdaExpression)owner.getParent().getParent();
|
||||
int index = lambda.getParameterList().getParameterIndex((PsiParameter)owner);
|
||||
PsiMethod sam = LambdaUtil.getFunctionalInterfaceMethod(lambda.getFunctionalInterfaceType());
|
||||
if (sam != null && index < sam.getParameterList().getParametersCount()) {
|
||||
return getElementNullability(null, sam.getParameterList().getParameters()[index]);
|
||||
}
|
||||
}
|
||||
|
||||
return Nullness.UNKNOWN;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
class Example {
|
||||
interface Listener {
|
||||
void notify(@NotNull String value);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Listener l = value -> {
|
||||
if (<warning descr="Condition 'value != null' is always 'true'">value != null</warning>) {
|
||||
System.out.println(value);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,7 @@ public class DataFlowInspection8Test extends DataFlowInspectionTestCase {
|
||||
public void testNullableForeachVariable() { doTestWithCustomAnnotations(); }
|
||||
public void testOptionalOfNullable() { doTest(); }
|
||||
public void testPrimitiveInVoidLambda() { doTest(); }
|
||||
public void testNotNullLambdaParameter() { doTest(); }
|
||||
|
||||
public void testNullableArrayComponent() {
|
||||
setupCustomAnnotations();
|
||||
|
||||
Reference in New Issue
Block a user