[java-analysis] ProjectBytecodeAnalysis: do not rewrite null->x contract with _->x if notnull parameter was inferred

Fixes IDEA-354381 Unreachable code inspection fails when using Streamable.of()
Unfortunately, the test does not differentiate the bug. I failed to write a good test in a reasonable amount of time. Nevertheless, the original issue is fixed.

GitOrigin-RevId: 55b0f169c93d1cef9453c3eb88959ea4832a917a
This commit is contained in:
Tagir Valeev
2024-06-17 18:59:38 +02:00
committed by intellij-monorepo-bot
parent 8e90196a7f
commit 191e65634e
4 changed files with 16 additions and 2 deletions

View File

@@ -606,9 +606,17 @@ public class ProjectBytecodeAnalysis {
if (alwaysNotNullParameters.cardinality() != 0) {
allContracts.replaceAll(smc -> {
ValueConstraint[] constraints = smc.getConstraints().toArray(new ValueConstraint[0]);
alwaysNotNullParameters.stream().forEach(idx -> constraints[idx] = ValueConstraint.ANY_VALUE);
for (int i = 0; i < constraints.length; i++) {
if (alwaysNotNullParameters.get(i)) {
if (constraints[i] == ValueConstraint.NULL_VALUE) {
return null;
}
constraints[i] = ValueConstraint.ANY_VALUE;
}
}
return new StandardMethodContract(constraints, smc.getReturnValue());
});
allContracts.removeIf(Objects::isNull);
}
}

View File

@@ -722,7 +722,7 @@
</item>
<item name='java.net.URI java.lang.String normalize(java.lang.String)'>
<annotation name='org.jetbrains.annotations.Contract'>
<val val="&quot;_-&gt;!null;_-&gt;null&quot;"/>
<val val="&quot;_-&gt;!null&quot;"/>
</annotation>
</item>
<item name='java.net.URI java.lang.String normalize(java.lang.String) 0'>

View File

@@ -8,6 +8,7 @@ import java.io.IOException;
import java.lang.reflect.Array;
import java.nio.file.Files;
import java.util.List;
import java.util.Objects;
import java.util.ArrayList;
@SuppressWarnings({"unused", "IOResourceOpenedButNotSafelyClosed"})
@@ -45,6 +46,11 @@ public class Test01 {
}
}
static @ExpectNotNull Runnable doubleCheck(Object obj) {
checkNotNullVoid(obj, "obj");
return checkNotNull(obj, "obj")::hashCode;
}
native static String createMessage(String s1, String s2);
@ExpectNotNull