[java-dfa] Ignore some contracts if method return value is not used

GitOrigin-RevId: 2834d00f1385759947351f8b93d8fc6a18a45106
This commit is contained in:
Tagir Valeev
2021-03-24 23:54:51 +00:00
committed by intellij-monorepo-bot
parent cbdf7bb2f9
commit a3e6b55f19
3 changed files with 32 additions and 0 deletions
@@ -1710,6 +1710,11 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
anchor = reference;
}
else {
if (ExpressionUtils.isVoidContext(expression) && ContainerUtil.all(contracts, c ->
c.getReturnValue() != ContractReturnValue.fail() && !(c.getReturnValue() instanceof ContractReturnValue.ParameterReturnValue))) {
// Do not track contracts if return value is not used
contracts = Collections.emptyList();
}
addInstruction(new MethodCallInstruction(expression, myFactory.createValue(expression), contracts));
anchor = expression;
}
@@ -0,0 +1,26 @@
import org.jetbrains.annotations.*;
class Inspection {
@Nullable
private Object o;
public void test1() {
check(o);
foo(<warning descr="Argument 'o' might be null">o</warning>);
}
public static Object check(@Nullable final Object o) {
if (o != null && is()) {
}
return <warning descr="'null' is returned by the method which is not declared as @Nullable">null</warning>;
}
private static boolean is() {
System.out.println();
return true;
}
private static void foo(@NotNull final Object obj) {
}
}
@@ -693,4 +693,5 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase {
public void testFinalStaticFields() { doTest(); }
public void testReassignInConstructor() { doTest(); }
public void testCollectionViewsSize() { doTest(); }
public void testFlushedNullableOnUnknownCall() { doTest(); }
}