Skip to content

Commit ffd6b98

Browse files
committed
Address review comments
1 parent 756affa commit ffd6b98

File tree

2 files changed

+16
-31
lines changed

2 files changed

+16
-31
lines changed

rust/ql/lib/codeql/rust/controlflow/internal/Completion.qll

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,29 +117,28 @@ class BooleanCompletion extends ConditionalCompletion, TBooleanCompletion {
117117
override string toString() { result = "boolean(" + value + ")" }
118118
}
119119

120-
/** Holds if node `pat` has the constant match value `value`. */
120+
/** Holds if `pat` is guaranteed to match. */
121121
pragma[nomagic]
122-
private predicate isMatchConstant(Pat pat, boolean value) {
123-
value = true and
122+
private predicate isIrrefutablePattern(Pat pat) {
124123
(
125124
pat instanceof WildcardPat
126125
or
127126
pat = any(IdentPat ip | not ip.hasPat() and ip = any(Variable v).getPat())
128127
or
129128
pat instanceof RestPat
130129
or
131-
// `let` statements without an `else` branch must be exhaustive
130+
// `let` statements without an `else` branch must be irrefutible
132131
pat = any(LetStmt let | not let.hasLetElse()).getPat()
133132
or
134-
// `match` expressions must be exhaustive, so last arm cannot fail
133+
// `match` expressions must be irrefutible, so last arm cannot fail
135134
pat = any(MatchExpr me).getLastArm().getPat()
136135
or
137-
// parameter patterns must be exhaustive
136+
// parameter patterns must be irrefutible
138137
pat = any(Param p).getPat()
139138
) and
140139
not pat = any(ForExpr for).getPat() // workaround until `for` loops are desugared
141140
or
142-
exists(Pat parent | isMatchConstant(parent, value) |
141+
exists(Pat parent | isIrrefutablePattern(parent) |
143142
pat = parent.(BoxPat).getPat()
144143
or
145144
pat = parent.(IdentPat).getPat()
@@ -166,11 +165,7 @@ class MatchCompletion extends TMatchCompletion, ConditionalCompletion {
166165

167166
override predicate isValidForSpecific(AstNode e) {
168167
e instanceof Pat and
169-
(
170-
isMatchConstant(e, value)
171-
or
172-
not isMatchConstant(e, _)
173-
)
168+
if isIrrefutablePattern(e) then value = true else any()
174169
}
175170

176171
override MatchSuccessor getAMatchingSuccessorType() { result.getValue() = value }

rust/ql/lib/codeql/rust/controlflow/internal/ControlFlowGraphImpl.qll

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,14 @@ class CastExprTree extends StandardPostOrderTree instanceof CastExpr {
179179
override AstNode getChildNode(int i) { i = 0 and result = super.getExpr() }
180180
}
181181

182-
// Closures have their own CFG scope, so we need to make sure that their
183-
// CFG is not mixed with the surrounding CFG. This is done by retrofitting
184-
// `first`, `propagatesAbnormal`, and `succ` below.
185-
class ClosureExprTree extends StandardPostOrderTree, ClosureExpr {
182+
class ClosureExprTree extends StandardTree, ClosureExpr {
186183
override predicate first(AstNode first) { first = this }
187184

185+
override predicate last(AstNode last, Completion c) {
186+
last = this and
187+
completionIsValidFor(c, this)
188+
}
189+
188190
override predicate propagatesAbnormal(AstNode child) { none() }
189191

190192
override AstNode getChildNode(int i) {
@@ -193,11 +195,6 @@ class ClosureExprTree extends StandardPostOrderTree, ClosureExpr {
193195
i = this.getParamList().getNumberOfParams() and
194196
result = this.getBody()
195197
}
196-
197-
override predicate succ(AstNode pred, AstNode succ, Completion c) {
198-
super.succ(pred, succ, c) and
199-
not succ = this
200-
}
201198
}
202199

203200
class ContinueExprTree extends LeafTree, ContinueExpr {
@@ -218,11 +215,9 @@ class FieldExprTree extends StandardPostOrderTree instanceof FieldExpr {
218215
override AstNode getChildNode(int i) { i = 0 and result = super.getExpr() }
219216
}
220217

221-
// Functions have their own CFG scope, so we need to make sure that their
222-
// CFG is not mixed with the surrounding CFG in case of nested functions.
223-
// This is done by retrofitting `last`, `propagatesAbnormal`, and `succ`
224-
// below.
225-
class FunctionTree extends StandardPreOrderTree, Function {
218+
class FunctionTree extends StandardTree, Function {
219+
override predicate first(AstNode first) { first = this }
220+
226221
override predicate last(AstNode last, Completion c) {
227222
last = this and
228223
completionIsValidFor(c, this)
@@ -236,11 +231,6 @@ class FunctionTree extends StandardPreOrderTree, Function {
236231
i = this.getParamList().getNumberOfParams() and
237232
result = this.getBody()
238233
}
239-
240-
override predicate succ(AstNode pred, AstNode succ, Completion c) {
241-
super.succ(pred, succ, c) and
242-
not pred = this
243-
}
244234
}
245235

246236
class ParamTree extends StandardPostOrderTree, Param {

0 commit comments

Comments
 (0)