mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java-inspections] OverwrittenFieldAnalyzer: set initial field write anchor to prevent subtle bugs
GitOrigin-RevId: 5138540545f8553a96a4cad042ed87e71d223dd6
This commit is contained in:
committed by
intellij-monorepo-bot
parent
9a8a33c1ab
commit
9d5757374f
+8
-1
@@ -146,6 +146,8 @@ final class OverwrittenFieldAnalyzer {
|
||||
return "@" + write + (wasRead ? " (wasRead)" : "");
|
||||
}
|
||||
}
|
||||
|
||||
private record EntryPointAnchor() implements DfaAnchor {}
|
||||
|
||||
private record WriteAnchorDescriptor(@NotNull DfaVariableValue var) implements VariableDescriptor {
|
||||
@Override
|
||||
@@ -163,6 +165,11 @@ final class OverwrittenFieldAnalyzer {
|
||||
return DfWriteTopType.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull DfType getInitialDfType(@NotNull DfaVariableValue thisValue, @Nullable PsiElement context) {
|
||||
return new DfWriteAnchorType(new EntryPointAnchor(), false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "writtenAt(" + var + ")";
|
||||
@@ -220,7 +227,7 @@ final class OverwrittenFieldAnalyzer {
|
||||
varsToFlush.forEach(state::flushVariable);
|
||||
if (var.getPsiVariable() instanceof PsiField && anchor != null) {
|
||||
DfaVariableValue wnr = myFactory.getVarFactory().createVariableValue(new WriteAnchorDescriptor(var));
|
||||
state.meetDfType(wnr, new DfWriteAnchorType(anchor, false));
|
||||
state.updateDfType(wnr, old -> new DfWriteAnchorType(anchor, false));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
public class FieldNoOverwriteEmptyElse {
|
||||
boolean myField;
|
||||
|
||||
void test(int x) {
|
||||
if (x != 1) {
|
||||
myField = true;
|
||||
}
|
||||
if (x == 3) {
|
||||
myField = false;
|
||||
} else {}
|
||||
}
|
||||
}
|
||||
@@ -62,6 +62,7 @@ public class DefUseTest extends LightJavaCodeInsightFixtureTestCase {
|
||||
public void testFieldOverwrite() { doTest(); }
|
||||
public void testFieldOverwriteAfterQualifierUpdate() { doTest(); }
|
||||
public void testFieldNoOverwrite() { doTest(); }
|
||||
public void testFieldNoOverwriteEmptyElse() { doTest(); }
|
||||
public void testAssignmentInCatch() { doTest(); }
|
||||
|
||||
public void testFieldCouldBeUsedOutside() { doTest(); }
|
||||
|
||||
Reference in New Issue
Block a user