IDEA-116947 Unqualified call NPE false positive with contracts

This commit is contained in:
peter
2013-12-18 17:25:49 +01:00
parent ea2b802d15
commit 746901e8c5
2 changed files with 21 additions and 3 deletions
@@ -1414,14 +1414,14 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
if (contracts.size() > 1) {
addInstruction(new DupInstruction(args.length, contracts.size() - 1));
}
for (MethodContract contract : contracts) {
handleContract(expression, contract);
for (int i = 0; i < contracts.size(); i++) {
handleContract(expression, contracts.get(i), contracts.size() - 1 - i);
}
pushUnknownReturnValue(expression); // goto here if all contracts are false
return true;
}
private void handleContract(PsiMethodCallExpression expression, MethodContract contract) {
private void handleContract(PsiMethodCallExpression expression, MethodContract contract, int remainingContracts) {
PsiExpression[] args = expression.getArgumentList().getExpressions();
final ControlFlow.ControlFlowOffset exitPoint = getEndOffset(expression);
@@ -1448,6 +1448,10 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
continueCheckingContract.setOffset(myCurrentFlow.getInstructionCount());
}
for (int j = 0; j < remainingContracts * args.length; j++) {
addInstruction(new PopInstruction());
}
// if contract is true
switch (contract.returnValue) {
case ANY_VALUE:
@@ -1,4 +1,5 @@
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
public class Foo {
@@ -6,8 +7,21 @@ public class Foo {
return escapeStr(s, escaper);
}
String foo2(Object escaper, @Nullable String s) {
return <warning descr="Expression 'escapeStr(s, escaper)' might evaluate to null but is returned by the method which is not declared as @Nullable">escapeStr(s, escaper)</warning>;
}
void foo3(@Nullable String s) {
foo2(this, escapeStr(s));
}
@Contract("null,_->null;!null,_->!null")
String escapeStr(String s, Object o) {
return s;
}
@Contract("null->null;!null->!null")
String escapeStr(String s) {
return s;
}
}