Skip to content

Commit 2511986

Browse files
committed
Rust: Address PR review comments
1 parent bbf5902 commit 2511986

File tree

6 files changed

+78
-63
lines changed

6 files changed

+78
-63
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
private import codeql.util.Option
21
private import codeql.util.Boolean
32
private import codeql.rust.controlflow.ControlFlowGraph
43
private import rust
@@ -8,9 +7,14 @@ private newtype TCompletion =
87
TSimpleCompletion() or
98
TBooleanCompletion(Boolean b) or
109
TMatchCompletion(Boolean isMatch) or
11-
TLoopCompletion(TLoopJumpType kind, TLabelType label) or
12-
TReturnCompletion() or
13-
TDivergeCompletion() // A completion that never reaches the successor (e.g. by panicking or spinning)
10+
TLoopCompletion(TLoopJumpType kind, TLabelType label) {
11+
label = TNoLabel()
12+
or
13+
kind = TBreakJump() and label = TLabel(any(BreakExpr b).getLabel().getName())
14+
or
15+
kind = TContinueJump() and label = TLabel(any(ContinueExpr b).getLabel().getName())
16+
} or
17+
TReturnCompletion()
1418

1519
/** A completion of a statement or an expression. */
1620
abstract class Completion extends TCompletion {

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ module CfgInput implements InputSig<Location> {
5555
predicate scopeLast(CfgScope scope, AstNode last, Completion c) { scope.scopeLast(last, c) }
5656
}
5757

58-
module CfgImpl = Make<Location, CfgInput>;
58+
private module CfgImpl = Make<Location, CfgInput>;
5959

6060
import CfgImpl
6161

62-
/** Holds for a trivial pattern that is always guaranteed to match. */
62+
/** Holds if `p` is a trivial pattern that is always guaranteed to match. */
6363
predicate trivialPat(Pat p) { p instanceof WildcardPat or p instanceof IdentPat }
6464

6565
class AsmExprTree extends LeafTree instanceof AsmExpr { }
@@ -90,7 +90,6 @@ class LogicalOrBinaryOpExprTree extends PreOrderTree instanceof BinaryExpr {
9090
child = [super.getRhs(), super.getLhs()]
9191
}
9292

93-
// override predicate first(AstNode node) { first(super.getLhs(), node) }
9493
override predicate succ(AstNode pred, AstNode succ, Completion c) {
9594
// Edge to the first node in the lhs
9695
pred = this and
@@ -109,7 +108,7 @@ class LogicalOrBinaryOpExprTree extends PreOrderTree instanceof BinaryExpr {
109108
c.(BooleanCompletion).succeeded()
110109
or
111110
// Rhs. as the last node
112-
last(super.getRhs(), node, c) // and
111+
last(super.getRhs(), node, c)
113112
}
114113
}
115114

@@ -120,7 +119,6 @@ class LogicalAndBinaryOpExprTree extends PreOrderTree instanceof BinaryExpr {
120119
child = [super.getRhs(), super.getLhs()]
121120
}
122121

123-
// override predicate first(AstNode node) { first(super.getLhs(), node) }
124122
override predicate succ(AstNode pred, AstNode succ, Completion c) {
125123
// Edge to the first node in the lhs
126124
pred = this and
@@ -283,15 +281,15 @@ class LetStmtTree extends PreOrderTree instanceof LetStmt {
283281
pred = this and first(super.getInitializer(), succ) and completionIsSimple(c)
284282
or
285283
// Edge from end of initializer to pattern.
286-
last(super.getInitializer(), pred, c) and succ = super.getPat()
284+
last(super.getInitializer(), pred, c) and first(super.getPat(), succ)
287285
or
288286
// Edge from failed pattern to `else` branch.
289-
pred = super.getPat() and first(super.getElse(), succ) and c.(MatchCompletion).failed()
287+
last(super.getPat(), pred, c) and first(super.getElse(), succ) and c.(MatchCompletion).failed()
290288
}
291289

292290
override predicate last(AstNode node, Completion c) {
293291
// Edge out of a successfully matched pattern.
294-
node = super.getPat() and c.(MatchCompletion).succeeded()
292+
last(super.getPat(), node, c) and c.(MatchCompletion).succeeded()
295293
// NOTE: No edge out of the `else` branch as that is guaranteed to diverge.
296294
}
297295
}
@@ -304,7 +302,7 @@ class LoopExprTree extends PostOrderTree instanceof LoopExpr {
304302
override predicate first(AstNode node) { first(super.getBody(), node) }
305303

306304
/** Whether this `LoopExpr` captures the `c` completion. */
307-
predicate capturesLoopJumpCompletion(LoopJumpCompletion c) {
305+
private predicate capturesLoopJumpCompletion(LoopJumpCompletion c) {
308306
not c.hasLabel()
309307
or
310308
c.getLabelName() = super.getLabel().getName()
@@ -346,7 +344,11 @@ class MatchArmTree extends ControlFlowTree instanceof MatchArm {
346344
// Edge from pattern to guard/arm if match succeeds.
347345
pred = super.getPat() and
348346
c.(MatchCompletion).succeeded() and
349-
(if super.hasGuard() then first(super.getGuard(), succ) else first(super.getExpr(), succ))
347+
(
348+
first(super.getGuard(), succ)
349+
or
350+
not super.hasGuard() and first(super.getExpr(), succ)
351+
)
350352
or
351353
// Edge from guard to arm if the guard succeeds.
352354
last(super.getGuard(), pred, c) and
@@ -364,7 +366,9 @@ class MatchArmTree extends ControlFlowTree instanceof MatchArm {
364366
}
365367

366368
class MatchExprTree extends PostOrderTree instanceof MatchExpr {
367-
override predicate propagatesAbnormal(AstNode child) { child = super.getABranch().getExpr() }
369+
override predicate propagatesAbnormal(AstNode child) {
370+
child = [super.getExpr(), super.getABranch().getExpr()]
371+
}
368372

369373
override predicate first(AstNode node) { first(super.getExpr(), node) }
370374

@@ -380,7 +384,7 @@ class MatchExprTree extends PostOrderTree instanceof MatchExpr {
380384
)
381385
or
382386
// Edge from the end of each arm to the match expression.
383-
last(super.getBranch(_), pred, c) and succ = this and completionIsSimple(c)
387+
last(super.getBranch(_), pred, c) and succ = this and completionIsNormal(c)
384388
}
385389
}
386390

@@ -436,7 +440,7 @@ class ReturnExprTree extends PostOrderTree instanceof ReturnExpr {
436440
}
437441

438442
override predicate succ(AstNode pred, AstNode succ, Completion c) {
439-
last(super.getExpr(), pred, c) and succ = this
443+
last(super.getExpr(), pred, c) and succ = this and completionIsNormal(c)
440444
}
441445
}
442446

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final class FunctionScope extends CfgScope, Function {
1717
override predicate scopeLast(AstNode node, Completion c) { last(this.getBody(), node, c) }
1818
}
1919

20-
final class LambdaScope extends CfgScope, ClosureExpr {
20+
final class ClosureScope extends CfgScope, ClosureExpr {
2121
override predicate scopeFirst(AstNode node) { first(this.getBody(), node) }
2222

2323
override predicate scopeLast(AstNode node, Completion c) { last(this.getBody(), node, c) }

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ newtype TSuccessorType =
1717
TLoopSuccessor(TLoopJumpType kind, TLabelType label) or
1818
TReturnSuccessor()
1919

20-
// class TBreakSuccessor = TUnlabeledBreakSuccessor or TLabeledBreakSuccessor;
2120
/** The type of a control flow successor. */
2221
abstract private class SuccessorTypeImpl extends TSuccessorType {
2322
/** Gets a textual representation of successor type. */

rust/ql/test/library-tests/controlflow/Cfg.expected

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,17 @@ nodes
192192
| test.rs:106:44:112:1 | BlockExpr | semmle.order | 191 |
193193
| test.rs:107:5:111:5 | MatchExpr | semmle.order | 192 |
194194
| test.rs:107:11:107:21 | PathExpr | semmle.order | 193 |
195-
| test.rs:108:9:108:15 | TupleStructPat | semmle.order | 194 |
196-
| test.rs:108:20:108:20 | PathExpr | semmle.order | 195 |
197-
| test.rs:108:20:108:25 | BinaryExpr | semmle.order | 196 |
198-
| test.rs:108:24:108:25 | LiteralExpr | semmle.order | 197 |
199-
| test.rs:108:30:108:30 | PathExpr | semmle.order | 198 |
200-
| test.rs:108:30:108:34 | BinaryExpr | semmle.order | 199 |
201-
| test.rs:108:34:108:34 | LiteralExpr | semmle.order | 200 |
202-
| test.rs:109:9:109:15 | TupleStructPat | semmle.order | 201 |
203-
| test.rs:109:20:109:20 | PathExpr | semmle.order | 202 |
204-
| test.rs:110:9:110:12 | IdentPat | semmle.order | 203 |
205-
| test.rs:110:17:110:17 | LiteralExpr | semmle.order | 204 |
195+
| test.rs:108:9:108:23 | TupleStructPat | semmle.order | 194 |
196+
| test.rs:108:28:108:28 | PathExpr | semmle.order | 195 |
197+
| test.rs:108:28:108:33 | BinaryExpr | semmle.order | 196 |
198+
| test.rs:108:32:108:33 | LiteralExpr | semmle.order | 197 |
199+
| test.rs:108:38:108:38 | PathExpr | semmle.order | 198 |
200+
| test.rs:108:38:108:42 | BinaryExpr | semmle.order | 199 |
201+
| test.rs:108:42:108:42 | LiteralExpr | semmle.order | 200 |
202+
| test.rs:109:9:109:23 | TupleStructPat | semmle.order | 201 |
203+
| test.rs:109:28:109:28 | PathExpr | semmle.order | 202 |
204+
| test.rs:110:9:110:20 | PathPat | semmle.order | 203 |
205+
| test.rs:110:25:110:25 | LiteralExpr | semmle.order | 204 |
206206
| test.rs:115:5:120:5 | enter test_infinite_loop | semmle.order | 205 |
207207
| test.rs:116:9:118:9 | ExprStmt | semmle.order | 206 |
208208
| test.rs:116:14:118:9 | BlockExpr | semmle.order | 207 |
@@ -618,36 +618,44 @@ edges
618618
| test.rs:106:44:112:1 | BlockExpr | test.rs:106:1:112:1 | exit test_match (normal) | semmle.order | 1 |
619619
| test.rs:107:5:111:5 | MatchExpr | test.rs:106:44:112:1 | BlockExpr | semmle.label | |
620620
| test.rs:107:5:111:5 | MatchExpr | test.rs:106:44:112:1 | BlockExpr | semmle.order | 1 |
621-
| test.rs:107:11:107:21 | PathExpr | test.rs:108:9:108:15 | TupleStructPat | semmle.label | |
622-
| test.rs:107:11:107:21 | PathExpr | test.rs:108:9:108:15 | TupleStructPat | semmle.order | 1 |
623-
| test.rs:108:9:108:15 | TupleStructPat | test.rs:108:20:108:20 | PathExpr | semmle.label | match |
624-
| test.rs:108:9:108:15 | TupleStructPat | test.rs:108:20:108:20 | PathExpr | semmle.order | 1 |
625-
| test.rs:108:9:108:15 | TupleStructPat | test.rs:109:9:109:15 | TupleStructPat | semmle.label | no-match |
626-
| test.rs:108:9:108:15 | TupleStructPat | test.rs:109:9:109:15 | TupleStructPat | semmle.order | 2 |
627-
| test.rs:108:20:108:20 | PathExpr | test.rs:108:24:108:25 | LiteralExpr | semmle.label | |
628-
| test.rs:108:20:108:20 | PathExpr | test.rs:108:24:108:25 | LiteralExpr | semmle.order | 1 |
629-
| test.rs:108:20:108:25 | BinaryExpr | test.rs:108:30:108:30 | PathExpr | semmle.label | true |
630-
| test.rs:108:20:108:25 | BinaryExpr | test.rs:108:30:108:30 | PathExpr | semmle.order | 1 |
631-
| test.rs:108:20:108:25 | BinaryExpr | test.rs:109:9:109:15 | TupleStructPat | semmle.label | false |
632-
| test.rs:108:20:108:25 | BinaryExpr | test.rs:109:9:109:15 | TupleStructPat | semmle.order | 2 |
633-
| test.rs:108:24:108:25 | LiteralExpr | test.rs:108:20:108:25 | BinaryExpr | semmle.label | |
634-
| test.rs:108:24:108:25 | LiteralExpr | test.rs:108:20:108:25 | BinaryExpr | semmle.order | 1 |
635-
| test.rs:108:30:108:30 | PathExpr | test.rs:108:34:108:34 | LiteralExpr | semmle.label | |
636-
| test.rs:108:30:108:30 | PathExpr | test.rs:108:34:108:34 | LiteralExpr | semmle.order | 1 |
637-
| test.rs:108:30:108:34 | BinaryExpr | test.rs:107:5:111:5 | MatchExpr | semmle.label | |
638-
| test.rs:108:30:108:34 | BinaryExpr | test.rs:107:5:111:5 | MatchExpr | semmle.order | 1 |
639-
| test.rs:108:34:108:34 | LiteralExpr | test.rs:108:30:108:34 | BinaryExpr | semmle.label | |
640-
| test.rs:108:34:108:34 | LiteralExpr | test.rs:108:30:108:34 | BinaryExpr | semmle.order | 1 |
641-
| test.rs:109:9:109:15 | TupleStructPat | test.rs:109:20:109:20 | PathExpr | semmle.label | match |
642-
| test.rs:109:9:109:15 | TupleStructPat | test.rs:109:20:109:20 | PathExpr | semmle.order | 1 |
643-
| test.rs:109:9:109:15 | TupleStructPat | test.rs:110:9:110:12 | IdentPat | semmle.label | no-match |
644-
| test.rs:109:9:109:15 | TupleStructPat | test.rs:110:9:110:12 | IdentPat | semmle.order | 2 |
645-
| test.rs:109:20:109:20 | PathExpr | test.rs:107:5:111:5 | MatchExpr | semmle.label | |
646-
| test.rs:109:20:109:20 | PathExpr | test.rs:107:5:111:5 | MatchExpr | semmle.order | 1 |
647-
| test.rs:110:9:110:12 | IdentPat | test.rs:110:17:110:17 | LiteralExpr | semmle.label | match |
648-
| test.rs:110:9:110:12 | IdentPat | test.rs:110:17:110:17 | LiteralExpr | semmle.order | 1 |
649-
| test.rs:110:17:110:17 | LiteralExpr | test.rs:107:5:111:5 | MatchExpr | semmle.label | |
650-
| test.rs:110:17:110:17 | LiteralExpr | test.rs:107:5:111:5 | MatchExpr | semmle.order | 1 |
621+
| test.rs:107:11:107:21 | PathExpr | test.rs:108:9:108:23 | TupleStructPat | semmle.label | |
622+
| test.rs:107:11:107:21 | PathExpr | test.rs:108:9:108:23 | TupleStructPat | semmle.order | 1 |
623+
| test.rs:108:9:108:23 | TupleStructPat | test.rs:107:5:111:5 | MatchExpr | semmle.label | no-match |
624+
| test.rs:108:9:108:23 | TupleStructPat | test.rs:107:5:111:5 | MatchExpr | semmle.order | 1 |
625+
| test.rs:108:9:108:23 | TupleStructPat | test.rs:108:28:108:28 | PathExpr | semmle.label | match |
626+
| test.rs:108:9:108:23 | TupleStructPat | test.rs:108:28:108:28 | PathExpr | semmle.order | 2 |
627+
| test.rs:108:9:108:23 | TupleStructPat | test.rs:109:9:109:23 | TupleStructPat | semmle.label | no-match |
628+
| test.rs:108:9:108:23 | TupleStructPat | test.rs:109:9:109:23 | TupleStructPat | semmle.order | 3 |
629+
| test.rs:108:28:108:28 | PathExpr | test.rs:108:32:108:33 | LiteralExpr | semmle.label | |
630+
| test.rs:108:28:108:28 | PathExpr | test.rs:108:32:108:33 | LiteralExpr | semmle.order | 1 |
631+
| test.rs:108:28:108:33 | BinaryExpr | test.rs:107:5:111:5 | MatchExpr | semmle.label | false |
632+
| test.rs:108:28:108:33 | BinaryExpr | test.rs:107:5:111:5 | MatchExpr | semmle.order | 1 |
633+
| test.rs:108:28:108:33 | BinaryExpr | test.rs:108:38:108:38 | PathExpr | semmle.label | true |
634+
| test.rs:108:28:108:33 | BinaryExpr | test.rs:108:38:108:38 | PathExpr | semmle.order | 2 |
635+
| test.rs:108:28:108:33 | BinaryExpr | test.rs:109:9:109:23 | TupleStructPat | semmle.label | false |
636+
| test.rs:108:28:108:33 | BinaryExpr | test.rs:109:9:109:23 | TupleStructPat | semmle.order | 3 |
637+
| test.rs:108:32:108:33 | LiteralExpr | test.rs:108:28:108:33 | BinaryExpr | semmle.label | |
638+
| test.rs:108:32:108:33 | LiteralExpr | test.rs:108:28:108:33 | BinaryExpr | semmle.order | 1 |
639+
| test.rs:108:38:108:38 | PathExpr | test.rs:108:42:108:42 | LiteralExpr | semmle.label | |
640+
| test.rs:108:38:108:38 | PathExpr | test.rs:108:42:108:42 | LiteralExpr | semmle.order | 1 |
641+
| test.rs:108:38:108:42 | BinaryExpr | test.rs:107:5:111:5 | MatchExpr | semmle.label | |
642+
| test.rs:108:38:108:42 | BinaryExpr | test.rs:107:5:111:5 | MatchExpr | semmle.order | 1 |
643+
| test.rs:108:42:108:42 | LiteralExpr | test.rs:108:38:108:42 | BinaryExpr | semmle.label | |
644+
| test.rs:108:42:108:42 | LiteralExpr | test.rs:108:38:108:42 | BinaryExpr | semmle.order | 1 |
645+
| test.rs:109:9:109:23 | TupleStructPat | test.rs:107:5:111:5 | MatchExpr | semmle.label | no-match |
646+
| test.rs:109:9:109:23 | TupleStructPat | test.rs:107:5:111:5 | MatchExpr | semmle.order | 1 |
647+
| test.rs:109:9:109:23 | TupleStructPat | test.rs:109:28:109:28 | PathExpr | semmle.label | match |
648+
| test.rs:109:9:109:23 | TupleStructPat | test.rs:109:28:109:28 | PathExpr | semmle.order | 2 |
649+
| test.rs:109:9:109:23 | TupleStructPat | test.rs:110:9:110:20 | PathPat | semmle.label | no-match |
650+
| test.rs:109:9:109:23 | TupleStructPat | test.rs:110:9:110:20 | PathPat | semmle.order | 3 |
651+
| test.rs:109:28:109:28 | PathExpr | test.rs:107:5:111:5 | MatchExpr | semmle.label | |
652+
| test.rs:109:28:109:28 | PathExpr | test.rs:107:5:111:5 | MatchExpr | semmle.order | 1 |
653+
| test.rs:110:9:110:20 | PathPat | test.rs:107:5:111:5 | MatchExpr | semmle.label | no-match |
654+
| test.rs:110:9:110:20 | PathPat | test.rs:107:5:111:5 | MatchExpr | semmle.order | 1 |
655+
| test.rs:110:9:110:20 | PathPat | test.rs:110:25:110:25 | LiteralExpr | semmle.label | match |
656+
| test.rs:110:9:110:20 | PathPat | test.rs:110:25:110:25 | LiteralExpr | semmle.order | 2 |
657+
| test.rs:110:25:110:25 | LiteralExpr | test.rs:107:5:111:5 | MatchExpr | semmle.label | |
658+
| test.rs:110:25:110:25 | LiteralExpr | test.rs:107:5:111:5 | MatchExpr | semmle.order | 1 |
651659
| test.rs:115:5:120:5 | enter test_infinite_loop | test.rs:116:9:118:9 | ExprStmt | semmle.label | |
652660
| test.rs:115:5:120:5 | enter test_infinite_loop | test.rs:116:9:118:9 | ExprStmt | semmle.order | 1 |
653661
| test.rs:116:9:118:9 | ExprStmt | test.rs:117:13:117:13 | LiteralExpr | semmle.label | |

rust/ql/test/library-tests/controlflow/test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ mod logical_operators {
105105

106106
fn test_match(maybe_digit: Option<i64>) -> {
107107
match maybe_digit {
108-
Some(x) if x < 10 => x + 5,
109-
Some(x) => x,
110-
None => 5,
108+
Option::Some(x) if x < 10 => x + 5,
109+
Option::Some(x) => x,
110+
Option::None => 5,
111111
}
112112
}
113113

0 commit comments

Comments
 (0)