From 4a4b9c30d70b92d0106c3cbc3c0af4c8b8a5f621 Mon Sep 17 00:00:00 2001 From: Robin Neatherway Date: Wed, 2 Feb 2022 11:38:15 +0000 Subject: [PATCH] Add an example query for inexhaustive switches --- .../snippets/incompleteswitchoverenum.ql | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 ql/examples/snippets/incompleteswitchoverenum.ql diff --git a/ql/examples/snippets/incompleteswitchoverenum.ql b/ql/examples/snippets/incompleteswitchoverenum.ql new file mode 100644 index 00000000000..b201e55089e --- /dev/null +++ b/ql/examples/snippets/incompleteswitchoverenum.ql @@ -0,0 +1,17 @@ +/** + * @name Incomplete switch over enum + * @description A switch statement of enum type should explicitly reference each + * of the members of that enum. + * @kind problem + * @id go/examples/incomplete-switch + */ + +import go + +from ExpressionSwitchStmt ss, DeclaredConstant c, NamedType t +where + t.getUnderlyingType() instanceof IntegerType and + t = ss.getExpr().getType() and + c.getType() = t and + forall(CaseClause case | case = ss.getACase() | not case = c.getAReference().getParent()) +select ss, "This switch statement is not exhaustive: missing $@", c, c.getName()