Skip to content

Commit 1a67ec4

Browse files
committed
Fix flat-switch for Go code
In flat-switch mode switches without a default case add an alternative edge from the head to the merge.
1 parent 48d071a commit 1a67ec4

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/control-flow/cfg-go.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,17 @@ export class CFGBuilder {
159159
switchHeadNode: string,
160160
) {
161161
let fallthrough: string | null = null;
162-
let previous: string | null = null;
163-
if (!this.flatSwitch && cases[0]?.conditionEntry) {
164-
this.addEdge(switchHeadNode, cases[0].conditionEntry);
165-
}
162+
let previous: string | null = switchHeadNode;
166163
cases.forEach((thisCase) => {
167164
if (this.flatSwitch) {
168165
if (thisCase.consequenceEntry) {
169166
this.addEdge(switchHeadNode, thisCase.consequenceEntry);
170167
if (fallthrough) {
171168
this.addEdge(fallthrough, thisCase.consequenceEntry);
172169
}
170+
if (thisCase.isDefault) {
171+
previous = null;
172+
}
173173
}
174174
} else {
175175
if (fallthrough && thisCase.consequenceEntry) {
@@ -203,7 +203,6 @@ export class CFGBuilder {
203203
});
204204
// Connect the last node to the merge node.
205205
// No need to handle `fallthrough` here as it is not allowed for the last case.
206-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
207206
if (previous) {
208207
this.addEdge(previous, mergeNode, "alternative");
209208
}

0 commit comments

Comments
 (0)