[java-dfa] IDEA-332978 Nullability warning is not displayed in instanceof with pattern operand

GitOrigin-RevId: e0ea0bb2fae6eb9119310c8f7be56a05298733f8
This commit is contained in:
Tagir Valeev
2023-09-27 16:12:24 +00:00
committed by intellij-monorepo-bot
parent 2586b12f70
commit d79850455f
2 changed files with 15 additions and 13 deletions
@@ -1864,7 +1864,6 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
PsiType operandType = operand.getType();
PsiTypeElement checkType = InstanceOfUtils.findCheckTypeElement(expression);
CFGBuilder builder = new CFGBuilder(this);
DfaVariableValue expressionValue;
if (pattern == null) {
if (checkType == null) {
pushUnknown();
@@ -1874,18 +1873,10 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
}
}
else if (operandType != null) {
DfaValue expr = JavaDfaValueFactory.getExpressionDfaValue(getFactory(), operand);
if (expr instanceof DfaVariableValue) {
expressionValue = (DfaVariableValue)expr;
builder.push(expressionValue);
}
else {
expressionValue = createTempVariable(operand.getType());
builder
.pushForWrite(expressionValue)
.pushExpression(operand)
.assign();
}
builder
.pushForWrite(createTempVariable(operand.getType()))
.pushExpression(operand)
.assign();
processPatternInInstanceof(pattern, expression, operandType);
}
else {
@@ -2,6 +2,15 @@ import java.util.function.*;
import org.jetbrains.annotations.Nullable;
public class InstanceOfPattern {
void test2(@Nullable Foo foo) {
if (Math.random() > 0.5 && foo.<warning descr="Method invocation 'bar' may produce 'NullPointerException'">bar</warning>() instanceof String s) {
System.out.println(s.length());
}
if (Math.random() > 0.5 && foo.<warning descr="Method invocation 'getBar' may produce 'NullPointerException'">getBar</warning>() instanceof String s) {
System.out.println(s.length());
}
}
void test(Foo foo) {
if (foo.bar() instanceof String s) {
System.out.println(s.length());
@@ -27,6 +36,8 @@ public class InstanceOfPattern {
}
interface Foo {
@Nullable Object getBar();
@Nullable Object bar();
}
}