enable contract checking when a notnull method is called (IDEA-CR-28918)

This commit is contained in:
peter
2018-02-07 00:00:32 +01:00
parent 04015dbafd
commit 16ae75a78a
3 changed files with 24 additions and 1 deletions
@@ -15,6 +15,7 @@
*/
package com.intellij.codeInspection.dataFlow;
import com.intellij.codeInsight.NullableNotNullManager;
import com.intellij.codeInspection.dataFlow.instructions.CheckReturnValueInstruction;
import com.intellij.codeInspection.dataFlow.instructions.Instruction;
import com.intellij.codeInspection.dataFlow.instructions.MethodCallInstruction;
@@ -115,7 +116,7 @@ class ContractChecker extends DataFlowRunner {
private static boolean weCannotInferAnythingAboutMethodReturnValue(MethodCallInstruction instruction) {
PsiMethod target = instruction.getTargetMethod();
return instruction.getContracts().isEmpty() && target != null && !target.isConstructor();
return instruction.getContracts().isEmpty() && target != null && !target.isConstructor() && !NullableNotNullManager.isNotNull(target);
}
@NotNull
@@ -0,0 +1,21 @@
import org.jetbrains.annotations.*;
class Test {
@Contract("!null -> null")
static String test(String s) {
if(s != null) {
return <warning descr="Contract clause '!null -> null' is violated">getValue(s)</warning>;
}
return getDefaultValue();
}
@NotNull
static String getValue(String s) {
return s.trim();
}
static String getDefaultValue() {
return "foo";
}
}
@@ -63,4 +63,5 @@ public class ContractCheckTest extends LightCodeInsightFixtureTestCase {
public void testPassingVarargsToDelegate() { doTest(); }
public void testUnknownIfCondition() { doTest(); }
public void testCallingNotNullMethod() { doTest(); }
}