IDEA-231238 False-positive nullability warning when using patterns

GitOrigin-RevId: 7c01148fde86284c0e1df215a39e1ef0808a1115
This commit is contained in:
Tagir Valeev
2020-01-23 16:52:16 +07:00
committed by intellij-monorepo-bot
parent f9ba0af66e
commit 762c20b6f5
4 changed files with 44 additions and 15 deletions

View File

@@ -299,6 +299,22 @@ public class CFGBuilder {
return add(new InstanceofInstruction(anchor));
}
/**
* Generate instructions to perform an instanceof operation
* <p>
* Stack before: ... object cast_type
* <p>
* Stack after: ... result
*
* @param anchor element to bind this instruction to
* @param operand operand expression (pushed before)
* @param castType cast type (pushed before)
* @return this builder
*/
public CFGBuilder isInstance(PsiExpression anchor, @Nullable PsiExpression operand, @NotNull PsiType castType) {
return add(new InstanceofInstruction(anchor, operand, castType));
}
/**
* Generate instructions to compare two values on top of stack with given relation operation (e.g. {@link JavaTokenType#GT}).
* <p>

View File

@@ -1521,24 +1521,26 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
PsiPattern pattern = expression.getPattern();
if (pattern instanceof PsiTypeTestPattern) {
PsiTypeElement checkType = ((PsiTypeTestPattern)pattern).getCheckType();
operand.accept(this);
PsiType type = checkType.getType();
CFGBuilder builder = new CFGBuilder(this);
PsiPatternVariable variable = ((PsiTypeTestPattern)pattern).getPatternVariable();
if (variable != null) {
addInstruction(new DupInstruction());
}
addInstruction(new PushValueInstruction(DfTypes.typedObject(type, Nullability.NOT_NULL)));
addInstruction(new InstanceofInstruction(expression, operand, type));
if (variable != null) {
new CFGBuilder(this)
.ifConditionIs(true)
.assignTo(variable)
.pop()
.push(DfTypes.TRUE)
.elseBranch()
.pop()
.push(DfTypes.FALSE)
DfaVariableValue dfaVar = getFactory().getVarFactory().createVariableValue(variable);
builder
.pushForWrite(dfaVar)
.pushExpression(operand)
.assign()
.push(DfTypes.typedObject(type, Nullability.NOT_NULL))
.isInstance(expression, operand, type)
.dup()
.ifConditionIs(false)
.flush(dfaVar)
.end();
} else {
builder
.pushExpression(operand)
.push(DfTypes.typedObject(type, Nullability.NOT_NULL))
.isInstance(expression, operand, type);
}
}
else {