mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
enable contract checking when a notnull method is called (IDEA-CR-28918)
This commit is contained in:
+2
-1
@@ -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(); }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user