[java-dfa] Return source value from assignment when target is not a variable

Fixes IDEA-346500 Assignment of non-null value to volatile field might be null

GitOrigin-RevId: 1105f7ed467e038df48922b036895faae40cc304
This commit is contained in:
Tagir Valeev
2024-02-19 13:30:10 +01:00
committed by intellij-monorepo-bot
parent 3c5f1db918
commit 388ce94e56
3 changed files with 13 additions and 4 deletions

View File

@@ -93,16 +93,14 @@ public class AssignInstruction extends ExpressionPushingInstruction {
dfaSource = dfaSource.getFactory().fromDfType(refType.dropLocality());
}
}
if (!(psi instanceof PsiField field) || !field.hasModifierProperty(PsiModifier.VOLATILE)) {
stateBefore.setVarValue(var, dfaSource);
}
stateBefore.setVarValue(var, dfaSource);
if (DfaNullability.fromDfType(var.getInherentType()) == DfaNullability.NULLABLE &&
DfaNullability.fromDfType(stateBefore.getDfType(var)) == DfaNullability.UNKNOWN && isVariableInitializer()) {
stateBefore.meetDfType(var, DfaNullability.NULLABLE.asDfType());
}
}
pushResult(interpreter, stateBefore, dfaDest);
pushResult(interpreter, stateBefore, dfaDest instanceof DfaVariableValue ? dfaDest : dfaSource);
return nextStates(interpreter, stateBefore);
}

View File

@@ -0,0 +1,10 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class Scratch {
private volatile @Nullable String foo;
public @NotNull String foo() {
return this.foo = "bar";
}
}

View File

@@ -744,4 +744,5 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase {
public void testNullWarningAfterInstanceofAndNullCheck() { doTest(); }
public void testInitializedViaSuperCall() { doTest(); }
public void testBoxedBooleanMethodWithCast() { doTest(); }
public void testAssignAndReturnVolatile() { doTest(); }
}