[java-dfa] Do not flush the pure method result facts

Otherwise, we lose locality

GitOrigin-RevId: aed2cd27ea1c412f16deaaea346791484e02f390
This commit is contained in:
Tagir Valeev
2024-07-05 16:23:22 +02:00
committed by intellij-monorepo-bot
parent 31b989d0cd
commit c2e018c7e3
4 changed files with 29 additions and 1 deletions

View File

@@ -79,6 +79,7 @@ public final class DfaCallArguments {
// We assume that even pure call may modify private fields (e.g., to cache something)
state.flushVariables(v -> v.getQualifier() == qualifier &&
v.getPsiVariable() instanceof PsiMember member &&
member != method &&
member.hasModifierProperty(PsiModifier.PRIVATE));
}
return;

View File

@@ -1,6 +1,6 @@
package org.assertj.core.api;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.*;
import java.util.Optional;
import java.util.stream.*;
@@ -37,6 +37,10 @@ class Sample {
Assertions.assertThat(stream).isEmpty();
}
// All methods inside the org.assertj.core.api package are hardcoded to be pure by default
// See com.intellij.codeInsight.DefaultInferredAnnotationProvider.getHardcodedContractAnnotation
// We override this, otherwise it spoils the tests, as it's assumed that the same value is always returned
@Contract(pure = false)
private native Optional<String> getOptional();
public void testAs() {

View File

@@ -0,0 +1,22 @@
import org.jetbrains.annotations.*;
public class FieldLocalNoAliasing {
static class X {
int a = 1;
}
void noAliasingPossible(X b) {
X x = getX();
x.a = 1;
b.a = 211;
if (<warning descr="Condition 'x.a == 1' is always 'true'">x.a == 1</warning>) {
System.out.println("1");
}
}
@Contract(value="->new", pure=true)
private X getX(){
X x = new X();
return x;
}
}

View File

@@ -762,5 +762,6 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase {
public void testAssignAndReturnVolatile() { doTest(); }
public void testQualifiedValueFromConstant() { doTest();}
public void testFieldAliasing() { doTest();}
public void testFieldLocalNoAliasing() { doTest();}
public void testIoContracts() { doTest(); }
}