Skip to content

Commit b2ea700

Browse files
committed
Go: support guards for tagged switch cases
1 parent 0b40def commit b2ea700

2 files changed

Lines changed: 31 additions & 7 deletions

File tree

‎go/ql/lib/semmle/go/controlflow/Guards.qll‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,22 @@ private module GuardsInput implements
9696
}
9797

9898
/**
99-
* A case expression in a tagless `switch` statement.
99+
* A case expression in an expression `switch` statement.
100100
*/
101101
class Case extends Expr {
102-
Case() {
103-
this =
104-
any(G::ExpressionSwitchStmt switch | not exists(switch.getExpr())).getACase().getAnExpr()
105-
}
102+
G::ExpressionSwitchStmt switch;
103+
104+
Case() { this = switch.getACase().getAnExpr() }
106105

107-
Expr getSwitchExpr() { result = this }
106+
Expr getSwitchExpr() {
107+
result = switch.getExpr()
108+
or
109+
not exists(switch.getExpr()) and result = this
110+
}
108111

109112
predicate isDefaultCase() { none() }
110113

111-
ConstantExpr asConstantCase() { none() }
114+
ConstantExpr asConstantCase() { exists(switch.getExpr()) and result = this }
112115

113116
predicate matchEdge(CfgImpl::Cfg::BasicBlock bb1, CfgImpl::Cfg::BasicBlock bb2) {
114117
bb1.getLastNode() = this.getControlFlowNode() and

‎go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.expected‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ controlsResult
44
| guards.go:7:7:7:15 | ...<... | zero | false |
55
| guards.go:9:7:9:16 | ...==... | positive | false |
66
| guards.go:9:7:9:16 | ...==... | zero | true |
7+
| guards.go:18:7:18:7 | 0 | tagged one or two | false |
8+
| guards.go:18:7:18:7 | 0 | tagged other | false |
9+
| guards.go:18:7:18:7 | 0 | tagged zero | true |
10+
| guards.go:20:7:20:7 | 1 | tagged other | false |
11+
| guards.go:20:10:20:10 | 2 | tagged other | false |
712
| guards.go:28:5:28:5 | a | compound true | true |
813
| guards.go:28:5:28:17 | ...&&... | compound false | false |
914
| guards.go:28:5:28:17 | ...&&... | compound true | true |
@@ -26,6 +31,16 @@ valueControlsResult
2631
| guards.go:9:7:9:11 | value | zero | 0 |
2732
| guards.go:9:7:9:16 | ...==... | positive | false |
2833
| guards.go:9:7:9:16 | ...==... | zero | true |
34+
| guards.go:17:9:17:13 | value | tagged one or two | not 0 |
35+
| guards.go:17:9:17:13 | value | tagged other | not 0 |
36+
| guards.go:17:9:17:13 | value | tagged other | not 1 |
37+
| guards.go:17:9:17:13 | value | tagged other | not 2 |
38+
| guards.go:17:9:17:13 | value | tagged zero | 0 |
39+
| guards.go:18:7:18:7 | 0 | tagged one or two | false |
40+
| guards.go:18:7:18:7 | 0 | tagged other | false |
41+
| guards.go:18:7:18:7 | 0 | tagged zero | true |
42+
| guards.go:20:7:20:7 | 1 | tagged other | false |
43+
| guards.go:20:10:20:10 | 2 | tagged other | false |
2944
| guards.go:28:5:28:5 | a | compound true | true |
3045
| guards.go:28:5:28:17 | ...&&... | compound false | false |
3146
| guards.go:28:5:28:17 | ...&&... | compound true | true |
@@ -49,6 +64,12 @@ valueControlsResult
4964
ensuresEqResult
5065
| guards.go:9:7:9:16 | ...==... | 0 = value | true |
5166
| guards.go:9:7:9:16 | ...==... | value = 0 | true |
67+
| guards.go:18:7:18:7 | 0 | 0 = value | true |
68+
| guards.go:18:7:18:7 | 0 | value = 0 | true |
69+
| guards.go:20:7:20:7 | 1 | 1 = value | true |
70+
| guards.go:20:7:20:7 | 1 | value = 1 | true |
71+
| guards.go:20:10:20:10 | 2 | 2 = value | true |
72+
| guards.go:20:10:20:10 | 2 | value = 2 | true |
5273
| guards.go:36:5:36:20 | ...==... | 0 = type conversion | true |
5374
| guards.go:36:5:36:20 | ...==... | type conversion = 0 | true |
5475
| guards.go:39:5:39:19 | ...==... | 0 = type conversion | true |

0 commit comments

Comments
 (0)