Skip to content

Commit c4eafb2

Browse files
committed
Rust: Skip ParenExprs in the CFG
1 parent 8b66dc1 commit c4eafb2

File tree

4 files changed

+38
-35
lines changed

4 files changed

+38
-35
lines changed

rust/ql/consistency-queries/CfgConsistency.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import codeql.rust.controlflow.internal.Completion
1111
query predicate nonPostOrderExpr(Expr e, string cls) {
1212
cls = e.getPrimaryQlClasses() and
1313
not e instanceof LetExpr and
14+
not e instanceof ParenExpr and
1415
not e instanceof LogicalAndExpr and // todo
1516
not e instanceof LogicalOrExpr and
1617
exists(AstNode last, Completion c |

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ class BooleanCompletion extends ConditionalCompletion, TBooleanCompletion {
9494
)
9595
or
9696
exists(Expr parent | this.isValidForSpecific0(parent) |
97+
e = parent.(ParenExpr).getExpr()
98+
or
9799
parent =
98100
any(PrefixExpr expr |
99101
expr.getOperatorName() = "!" and

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,18 @@ class NameRefTree extends LeafTree, NameRef { }
499499

500500
class OffsetOfExprTree extends LeafTree instanceof OffsetOfExpr { }
501501

502-
class ParenExprTree extends StandardPostOrderTree, ParenExpr {
503-
override AstNode getChildNode(int i) { i = 0 and result = super.getExpr() }
502+
class ParenExprTree extends ControlFlowTree, ParenExpr {
503+
private ControlFlowTree expr;
504+
505+
ParenExprTree() { expr = super.getExpr() }
506+
507+
override predicate propagatesAbnormal(AstNode child) { expr.propagatesAbnormal(child) }
508+
509+
override predicate first(AstNode first) { expr.first(first) }
510+
511+
override predicate last(AstNode last, Completion c) { expr.last(last, c) }
512+
513+
override predicate succ(AstNode pred, AstNode succ, Completion c) { none() }
504514
}
505515

506516
// This covers all patterns as they all extend `Pat`

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

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,9 @@
111111
| test.rs:57:13:57:14 | ExprStmt | test.rs:57:13:57:13 | 1 | |
112112
| test.rs:58:13:60:13 | ExprStmt | test.rs:58:17:58:17 | i | |
113113
| test.rs:58:13:60:13 | IfExpr | test.rs:61:13:61:22 | ExprStmt | |
114-
| test.rs:58:16:58:22 | ParenExpr | test.rs:58:13:60:13 | IfExpr | false |
115-
| test.rs:58:16:58:22 | ParenExpr | test.rs:59:17:59:22 | ExprStmt | true |
116114
| test.rs:58:17:58:17 | i | test.rs:58:21:58:21 | 0 | |
117-
| test.rs:58:17:58:21 | ... > ... | test.rs:58:16:58:22 | ParenExpr | |
115+
| test.rs:58:17:58:21 | ... > ... | test.rs:58:13:60:13 | IfExpr | false |
116+
| test.rs:58:17:58:21 | ... > ... | test.rs:59:17:59:22 | ExprStmt | true |
118117
| test.rs:58:21:58:21 | 0 | test.rs:58:17:58:21 | ... > ... | |
119118
| test.rs:59:17:59:21 | BreakExpr | test.rs:56:9:62:9 | WhileExpr | break |
120119
| test.rs:59:17:59:22 | ExprStmt | test.rs:59:17:59:21 | BreakExpr | |
@@ -136,10 +135,9 @@
136135
| test.rs:67:19:67:25 | TupleStructPat | test.rs:68:17:68:17 | PathExpr | match |
137136
| test.rs:67:41:71:9 | BlockExpr | test.rs:67:15:67:39 | LetExpr | |
138137
| test.rs:68:13:70:13 | IfExpr | test.rs:67:41:71:9 | BlockExpr | |
139-
| test.rs:68:16:68:22 | ParenExpr | test.rs:68:13:70:13 | IfExpr | false |
140-
| test.rs:68:16:68:22 | ParenExpr | test.rs:69:17:69:22 | ExprStmt | true |
141138
| test.rs:68:17:68:17 | PathExpr | test.rs:68:21:68:21 | 5 | |
142-
| test.rs:68:17:68:21 | ... = ... | test.rs:68:16:68:22 | ParenExpr | |
139+
| test.rs:68:17:68:21 | ... = ... | test.rs:68:13:70:13 | IfExpr | false |
140+
| test.rs:68:17:68:21 | ... = ... | test.rs:69:17:69:22 | ExprStmt | true |
143141
| test.rs:68:21:68:21 | 5 | test.rs:68:17:68:21 | ... = ... | |
144142
| test.rs:69:17:69:21 | BreakExpr | test.rs:67:9:71:9 | WhileExpr | break |
145143
| test.rs:69:17:69:22 | ExprStmt | test.rs:69:17:69:21 | BreakExpr | |
@@ -155,10 +153,9 @@
155153
| test.rs:75:24:80:9 | BlockExpr | test.rs:75:13:75:13 | i | |
156154
| test.rs:76:13:78:13 | ExprStmt | test.rs:76:17:76:17 | i | |
157155
| test.rs:76:13:78:13 | IfExpr | test.rs:79:13:79:14 | ExprStmt | |
158-
| test.rs:76:16:76:23 | ParenExpr | test.rs:76:13:78:13 | IfExpr | false |
159-
| test.rs:76:16:76:23 | ParenExpr | test.rs:77:17:77:22 | ExprStmt | true |
160156
| test.rs:76:17:76:17 | i | test.rs:76:22:76:22 | j | |
161-
| test.rs:76:17:76:22 | ... == ... | test.rs:76:16:76:23 | ParenExpr | |
157+
| test.rs:76:17:76:22 | ... == ... | test.rs:76:13:78:13 | IfExpr | false |
158+
| test.rs:76:17:76:22 | ... == ... | test.rs:77:17:77:22 | ExprStmt | true |
162159
| test.rs:76:22:76:22 | j | test.rs:76:17:76:22 | ... == ... | |
163160
| test.rs:77:17:77:21 | BreakExpr | test.rs:75:9:80:9 | ForExpr | break |
164161
| test.rs:77:17:77:22 | ExprStmt | test.rs:77:17:77:21 | BreakExpr | |
@@ -220,21 +217,20 @@
220217
| test.rs:114:5:120:5 | exit test_nested_if (normal) | test.rs:114:5:120:5 | exit test_nested_if | |
221218
| test.rs:114:38:120:5 | BlockExpr | test.rs:114:5:120:5 | exit test_nested_if (normal) | |
222219
| test.rs:115:9:119:9 | IfExpr | test.rs:114:38:120:5 | BlockExpr | |
223-
| test.rs:115:12:115:49 | ParenExpr | test.rs:116:13:116:13 | 1 | true |
224-
| test.rs:115:12:115:49 | ParenExpr | test.rs:118:13:118:13 | 0 | false |
225-
| test.rs:115:13:115:48 | IfExpr | test.rs:115:12:115:49 | ParenExpr | |
220+
| test.rs:115:13:115:48 | IfExpr | test.rs:116:13:116:13 | 1 | true |
221+
| test.rs:115:13:115:48 | IfExpr | test.rs:118:13:118:13 | 0 | false |
226222
| test.rs:115:16:115:16 | PathExpr | test.rs:115:20:115:20 | 0 | |
227223
| test.rs:115:16:115:20 | ... < ... | test.rs:115:24:115:24 | a | true |
228224
| test.rs:115:16:115:20 | ... < ... | test.rs:115:41:115:41 | a | false |
229225
| test.rs:115:20:115:20 | 0 | test.rs:115:16:115:20 | ... < ... | |
230-
| test.rs:115:22:115:32 | BlockExpr | test.rs:115:13:115:48 | IfExpr | |
226+
| test.rs:115:22:115:32 | BlockExpr | test.rs:115:13:115:48 | IfExpr | false, true |
231227
| test.rs:115:24:115:24 | a | test.rs:115:29:115:30 | 10 | |
232-
| test.rs:115:24:115:30 | ... < ... | test.rs:115:22:115:32 | BlockExpr | |
228+
| test.rs:115:24:115:30 | ... < ... | test.rs:115:22:115:32 | BlockExpr | false, true |
233229
| test.rs:115:28:115:30 | - ... | test.rs:115:24:115:30 | ... < ... | |
234230
| test.rs:115:29:115:30 | 10 | test.rs:115:28:115:30 | - ... | |
235-
| test.rs:115:39:115:48 | BlockExpr | test.rs:115:13:115:48 | IfExpr | |
231+
| test.rs:115:39:115:48 | BlockExpr | test.rs:115:13:115:48 | IfExpr | false, true |
236232
| test.rs:115:41:115:41 | a | test.rs:115:45:115:46 | 10 | |
237-
| test.rs:115:41:115:46 | ... > ... | test.rs:115:39:115:48 | BlockExpr | |
233+
| test.rs:115:41:115:46 | ... > ... | test.rs:115:39:115:48 | BlockExpr | false, true |
238234
| test.rs:115:45:115:46 | 10 | test.rs:115:41:115:46 | ... > ... | |
239235
| test.rs:115:51:117:9 | BlockExpr | test.rs:115:9:119:9 | IfExpr | |
240236
| test.rs:116:13:116:13 | 1 | test.rs:115:51:117:9 | BlockExpr | |
@@ -244,9 +240,8 @@
244240
| test.rs:122:5:131:5 | exit test_nested_if_match (normal) | test.rs:122:5:131:5 | exit test_nested_if_match | |
245241
| test.rs:122:44:131:5 | BlockExpr | test.rs:122:5:131:5 | exit test_nested_if_match (normal) | |
246242
| test.rs:123:9:130:9 | IfExpr | test.rs:122:44:131:5 | BlockExpr | |
247-
| test.rs:123:12:126:10 | ParenExpr | test.rs:127:13:127:13 | 1 | true |
248-
| test.rs:123:12:126:10 | ParenExpr | test.rs:129:13:129:13 | 0 | false |
249-
| test.rs:123:13:126:9 | MatchExpr | test.rs:123:12:126:10 | ParenExpr | |
243+
| test.rs:123:13:126:9 | MatchExpr | test.rs:127:13:127:13 | 1 | true |
244+
| test.rs:123:13:126:9 | MatchExpr | test.rs:129:13:129:13 | 0 | false |
250245
| test.rs:123:19:123:19 | a | test.rs:124:13:124:13 | LiteralPat | |
251246
| test.rs:124:13:124:13 | LiteralPat | test.rs:124:18:124:21 | true | match |
252247
| test.rs:124:13:124:13 | LiteralPat | test.rs:125:13:125:13 | WildcardPat | no-match |
@@ -291,9 +286,8 @@
291286
| test.rs:153:5:164:5 | exit test_if_loop1 (normal) | test.rs:153:5:164:5 | exit test_if_loop1 | |
292287
| test.rs:153:37:164:5 | BlockExpr | test.rs:153:5:164:5 | exit test_if_loop1 (normal) | |
293288
| test.rs:154:9:163:9 | IfExpr | test.rs:153:37:164:5 | BlockExpr | |
294-
| test.rs:154:12:159:10 | ParenExpr | test.rs:160:13:160:13 | 1 | true |
295-
| test.rs:154:12:159:10 | ParenExpr | test.rs:162:13:162:13 | 0 | false |
296-
| test.rs:154:13:159:9 | LoopExpr | test.rs:154:12:159:10 | ParenExpr | |
289+
| test.rs:154:13:159:9 | LoopExpr | test.rs:160:13:160:13 | 1 | true |
290+
| test.rs:154:13:159:9 | LoopExpr | test.rs:162:13:162:13 | 0 | false |
297291
| test.rs:154:18:159:9 | BlockExpr | test.rs:155:13:157:14 | ExprStmt | |
298292
| test.rs:155:13:157:13 | IfExpr | test.rs:158:13:158:19 | ExprStmt | |
299293
| test.rs:155:13:157:14 | ExprStmt | test.rs:155:16:155:16 | a | |
@@ -318,9 +312,8 @@
318312
| test.rs:166:5:177:5 | exit test_if_loop2 (normal) | test.rs:166:5:177:5 | exit test_if_loop2 | |
319313
| test.rs:166:37:177:5 | BlockExpr | test.rs:166:5:177:5 | exit test_if_loop2 (normal) | |
320314
| test.rs:167:9:176:9 | IfExpr | test.rs:166:37:177:5 | BlockExpr | |
321-
| test.rs:167:12:172:10 | ParenExpr | test.rs:173:13:173:13 | 1 | true |
322-
| test.rs:167:12:172:10 | ParenExpr | test.rs:175:13:175:13 | 0 | false |
323-
| test.rs:167:13:172:9 | LoopExpr | test.rs:167:12:172:10 | ParenExpr | |
315+
| test.rs:167:13:172:9 | LoopExpr | test.rs:173:13:173:13 | 1 | true |
316+
| test.rs:167:13:172:9 | LoopExpr | test.rs:175:13:175:13 | 0 | false |
324317
| test.rs:167:26:172:9 | BlockExpr | test.rs:168:13:170:14 | ExprStmt | |
325318
| test.rs:168:13:170:13 | IfExpr | test.rs:171:13:171:19 | ExprStmt | |
326319
| test.rs:168:13:170:14 | ExprStmt | test.rs:168:16:168:16 | a | |
@@ -345,9 +338,8 @@
345338
| test.rs:179:5:187:5 | exit test_labelled_block (normal) | test.rs:179:5:187:5 | exit test_labelled_block | |
346339
| test.rs:179:43:187:5 | BlockExpr | test.rs:179:5:187:5 | exit test_labelled_block (normal) | |
347340
| test.rs:180:9:186:9 | IfExpr | test.rs:179:43:187:5 | BlockExpr | |
348-
| test.rs:180:12:182:10 | ParenExpr | test.rs:183:13:183:13 | 1 | true |
349-
| test.rs:180:12:182:10 | ParenExpr | test.rs:185:13:185:13 | 0 | false |
350-
| test.rs:180:13:182:9 | BlockExpr | test.rs:180:12:182:10 | ParenExpr | |
341+
| test.rs:180:13:182:9 | BlockExpr | test.rs:183:13:183:13 | 1 | true |
342+
| test.rs:180:13:182:9 | BlockExpr | test.rs:185:13:185:13 | 0 | false |
351343
| test.rs:181:13:181:30 | BreakExpr | test.rs:180:13:182:9 | BlockExpr | break('block) |
352344
| test.rs:181:13:181:31 | ExprStmt | test.rs:181:26:181:26 | a | |
353345
| test.rs:181:26:181:26 | a | test.rs:181:30:181:30 | 0 | |
@@ -392,10 +384,9 @@
392384
| test.rs:203:17:203:17 | a | test.rs:203:23:203:23 | b | false |
393385
| test.rs:203:17:203:30 | ... \|\| ... | test.rs:203:17:203:17 | a | |
394386
| test.rs:203:17:203:35 | ... \|\| ... | test.rs:203:17:203:30 | ... \|\| ... | |
395-
| test.rs:203:22:203:30 | ParenExpr | test.rs:203:13:203:13 | d | true |
396-
| test.rs:203:22:203:30 | ParenExpr | test.rs:203:35:203:35 | c | false |
397387
| test.rs:203:23:203:23 | b | test.rs:203:28:203:29 | 28 | |
398-
| test.rs:203:23:203:29 | ... == ... | test.rs:203:22:203:30 | ParenExpr | |
388+
| test.rs:203:23:203:29 | ... == ... | test.rs:203:13:203:13 | d | true |
389+
| test.rs:203:23:203:29 | ... == ... | test.rs:203:35:203:35 | c | false |
399390
| test.rs:203:28:203:29 | 28 | test.rs:203:23:203:29 | ... == ... | |
400391
| test.rs:203:35:203:35 | c | test.rs:203:13:203:13 | d | |
401392
| test.rs:204:9:204:9 | d | test.rs:202:61:205:5 | BlockExpr | |
@@ -485,8 +476,7 @@
485476
| test.rs:259:1:264:1 | enter dead_code | test.rs:260:5:262:5 | ExprStmt | |
486477
| test.rs:259:1:264:1 | exit dead_code (normal) | test.rs:259:1:264:1 | exit dead_code | |
487478
| test.rs:260:5:262:5 | ExprStmt | test.rs:260:9:260:12 | true | |
488-
| test.rs:260:8:260:13 | ParenExpr | test.rs:261:9:261:17 | ExprStmt | true |
489-
| test.rs:260:9:260:12 | true | test.rs:260:8:260:13 | ParenExpr | |
479+
| test.rs:260:9:260:12 | true | test.rs:261:9:261:17 | ExprStmt | true |
490480
| test.rs:261:9:261:16 | ReturnExpr | test.rs:259:1:264:1 | exit dead_code (normal) | return |
491481
| test.rs:261:9:261:17 | ExprStmt | test.rs:261:16:261:16 | 0 | |
492482
| test.rs:261:16:261:16 | 0 | test.rs:261:9:261:16 | ReturnExpr | |

0 commit comments

Comments
 (0)