[java-dfa] Avoid flushing constants before closures

LiveVariableAnalyzer should know that the constant is used in closure. Otherwise, it may flush it.
Fixes IDEA-262325 Nullability issue when constant checked outside of lambda is used inside

GitOrigin-RevId: 660beecec912efbaceec77e71bfcfbca43fd97ad
This commit is contained in:
Tagir Valeev
2021-02-19 10:15:02 +07:00
committed by intellij-monorepo-bot
parent 1b615aa69c
commit 1c366664e9
3 changed files with 30 additions and 4 deletions

View File

@@ -776,10 +776,13 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
if (PsiUtil.isJvmLocalVariable(target)) {
variables.add((PsiVariable)target);
}
if (target instanceof PsiMember && !((PsiMember)target).hasModifierProperty(PsiModifier.STATIC)) {
DfaValue qualifier = getFactory().getExpressionFactory().getQualifierOrThisValue(expression);
if (qualifier instanceof DfaVariableValue) {
escapedVars.add((DfaVariableValue)qualifier);
if (target instanceof PsiMember) {
DfaValue escapedVar = getFactory().getExpressionFactory().getQualifierOrThisValue(expression);
if (escapedVar == null) {
escapedVar = getFactory().createValue(expression);
}
if (escapedVar instanceof DfaVariableValue) {
escapedVars.add((DfaVariableValue)escapedVar);
}
}
}

View File

@@ -0,0 +1,22 @@
import org.jetbrains.annotations.*;
public class ConstantInClosure {
static final Object CONST = init();
private void test() {
Obj obj = new Obj();
if (CONST != null) {
privateMethod();
Runnable runnable = () -> consume(CONST);
}
}
private void privateMethod() { }
native void consume(@NotNull Object obj);
@Nullable
static native Object init();
private static class Obj { }
}

View File

@@ -329,4 +329,5 @@ public class DataFlowInspection8Test extends DataFlowInspectionTestCase {
public void testModifyListInLambda() {
doTest();
}
public void testConstantInClosure() { doTest(); }
}