Java: report more "variable might have been assigned to" problems

GitOrigin-RevId: 030936b065bb00b504a69bc08ad39ae8a28cad6f
This commit is contained in:
Bas Leijdekkers
2025-08-25 17:43:01 +00:00
committed by intellij-monorepo-bot
parent 319b584d15
commit 97de4512b7
2 changed files with 12 additions and 5 deletions
@@ -2624,16 +2624,14 @@ public final class ControlFlowUtil {
}
public static @NotNull Collection<VariableInfo> getInitializedTwice(@NotNull ControlFlow flow, int startOffset, int endOffset) {
Collection<VariableInfo> result = new HashSet<>();
while (startOffset < endOffset) {
InitializedTwiceClientVisitor visitor = new InitializedTwiceClientVisitor(flow, startOffset);
depthFirstSearch(flow, visitor, startOffset, endOffset);
Collection<VariableInfo> result = visitor.getResult();
if (!result.isEmpty()) {
return result;
}
result.addAll(visitor.getResult());
startOffset = findUnprocessed(startOffset, endOffset, visitor);
}
return Collections.emptyList();
return result;
}
private static class InitializedTwiceClientVisitor extends InstructionClientVisitor<Collection<VariableInfo>> {
@@ -160,6 +160,15 @@ class ForLoop {
break;
}
}
void x() {
final int a, b;
<error descr="Variable 'a' might already have been assigned to">a</error> = a = 0;
for (;; b = 0) {
<error descr="Variable 'b' might already have been assigned to">b</error> = 1;
break;
}
}
}
// IDEA-186305
class Asserts {