From 879429889357374071de96576984c4f01529be6d Mon Sep 17 00:00:00 2001 From: Owen Mansel-Chan Date: Fri, 29 May 2026 15:57:08 +0100 Subject: [PATCH] Fix empty switch statements --- .../go/controlflow/ControlFlowGraphShared.qll | 38 ++++++------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/go/ql/lib/semmle/go/controlflow/ControlFlowGraphShared.qll b/go/ql/lib/semmle/go/controlflow/ControlFlowGraphShared.qll index 72d7da2e851..992cef0ffeb 100644 --- a/go/ql/lib/semmle/go/controlflow/ControlFlowGraphShared.qll +++ b/go/ql/lib/semmle/go/controlflow/ControlFlowGraphShared.qll @@ -1231,6 +1231,14 @@ module GoCfg { exprSwitch(n1, n2) or typeSwitch(n1, n2) or caseClause(n1, n2) } + private predicate switchCasesStartOrAfter(Go::SwitchStmt sw, PreControlFlowNode n) { + n.isBefore(sw.getNonDefaultCase(0)) + or + not exists(sw.getANonDefaultCase()) and n.isBefore(sw.getDefault()) + or + not exists(sw.getACase()) and n.isAfter(sw) + } + private predicate exprSwitch(PreControlFlowNode n1, PreControlFlowNode n2) { exists(Go::ExpressionSwitchStmt sw | n1.isBefore(sw) and @@ -1241,14 +1249,7 @@ module GoCfg { ( n2.isBefore(sw.getExpr()) or - not exists(sw.getExpr()) and - ( - n2.isBefore(sw.getNonDefaultCase(0)) - or - not exists(sw.getANonDefaultCase()) and n2.isBefore(sw.getDefault()) - or - not exists(sw.getACase()) and n2.isAfter(sw) - ) + not exists(sw.getExpr()) and switchCasesStartOrAfter(sw, n2) ) ) or @@ -1256,20 +1257,10 @@ module GoCfg { ( n2.isBefore(sw.getExpr()) or - not exists(sw.getExpr()) and - ( - n2.isBefore(sw.getNonDefaultCase(0)) - or - not exists(sw.getANonDefaultCase()) and n2.isBefore(sw.getDefault()) - ) + not exists(sw.getExpr()) and switchCasesStartOrAfter(sw, n2) ) or - n1.isAfter(sw.getExpr()) and - ( - n2.isBefore(sw.getNonDefaultCase(0)) - or - not exists(sw.getANonDefaultCase()) and n2.isBefore(sw.getDefault()) - ) + n1.isAfter(sw.getExpr()) and switchCasesStartOrAfter(sw, n2) ) } @@ -1284,12 +1275,7 @@ module GoCfg { or n1.isAfter(sw.getInit()) and n2.isBefore(sw.getTest()) or - n1.isAfter(sw.getTest()) and - ( - n2.isBefore(sw.getNonDefaultCase(0)) - or - not exists(sw.getANonDefaultCase()) and n2.isBefore(sw.getDefault()) - ) + n1.isAfter(sw.getTest()) and switchCasesStartOrAfter(sw, n2) ) }