Skip to content

Commit f114053

Browse files
authored
Merge pull request #18656 from hvitved/rust/record-destruct-shorthand
Rust: Shorthand record pattern destructuring in data flow
2 parents 2a32e88 + f1050c4 commit f114053

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

rust/ql/lib/codeql/rust/controlflow/CfgNodes.qll

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,16 @@ final class RecordPatCfgNode extends Nodes::RecordPatCfgNode {
261261
RecordPatCfgNode() { node = this.getRecordPat() }
262262

263263
/** Gets the record pattern for the field `field`. */
264+
pragma[nomagic]
264265
PatCfgNode getFieldPat(string field) {
265266
exists(RecordPatField rpf |
266267
rpf = node.getRecordPatFieldList().getAField() and
267-
any(ChildMapping mapping).hasCfgChild(node, rpf.getPat(), this, result) and
268+
any(ChildMapping mapping).hasCfgChild(node, rpf.getPat(), this, result)
269+
|
268270
field = rpf.getNameRef().getText()
271+
or
272+
not rpf.hasNameRef() and
273+
field = result.(IdentPatCfgNode).getName().getText()
269274
)
270275
}
271276
}

rust/ql/test/library-tests/dataflow/local/DataFlowStep.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,9 @@ readStep
639639
| main.rs:151:9:151:28 | Point {...} | Point.x | main.rs:151:20:151:20 | a |
640640
| main.rs:151:9:151:28 | Point {...} | Point.y | main.rs:151:26:151:26 | b |
641641
| main.rs:183:9:186:9 | Point3D {...} | Point3D.plane | main.rs:184:20:184:33 | Point {...} |
642+
| main.rs:183:9:186:9 | Point3D {...} | Point3D.z | main.rs:185:13:185:13 | z |
643+
| main.rs:184:20:184:33 | Point {...} | Point.x | main.rs:184:28:184:28 | x |
644+
| main.rs:184:20:184:33 | Point {...} | Point.y | main.rs:184:31:184:31 | y |
642645
| main.rs:201:9:201:23 | ...::Some(...) | Some | main.rs:201:22:201:22 | n |
643646
| main.rs:205:9:205:23 | ...::Some(...) | Some | main.rs:205:22:205:22 | n |
644647
| main.rs:214:9:214:15 | Some(...) | Some | main.rs:214:14:214:14 | n |

rust/ql/test/library-tests/dataflow/local/inline-flow.expected

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ edges
4949
| main.rs:148:12:148:21 | source(...) | main.rs:147:13:150:5 | Point {...} [Point.x] | provenance | |
5050
| main.rs:151:9:151:28 | Point {...} [Point.x] | main.rs:151:20:151:20 | a | provenance | |
5151
| main.rs:151:20:151:20 | a | main.rs:152:10:152:10 | a | provenance | |
52+
| main.rs:175:9:175:9 | p [Point3D.plane, Point.y] | main.rs:182:11:182:11 | p [Point3D.plane, Point.y] | provenance | |
53+
| main.rs:175:13:181:5 | Point3D {...} [Point3D.plane, Point.y] | main.rs:175:9:175:9 | p [Point3D.plane, Point.y] | provenance | |
54+
| main.rs:176:16:179:9 | Point {...} [Point.y] | main.rs:175:13:181:5 | Point3D {...} [Point3D.plane, Point.y] | provenance | |
55+
| main.rs:178:16:178:25 | source(...) | main.rs:176:16:179:9 | Point {...} [Point.y] | provenance | |
56+
| main.rs:182:11:182:11 | p [Point3D.plane, Point.y] | main.rs:183:9:186:9 | Point3D {...} [Point3D.plane, Point.y] | provenance | |
57+
| main.rs:183:9:186:9 | Point3D {...} [Point3D.plane, Point.y] | main.rs:184:20:184:33 | Point {...} [Point.y] | provenance | |
58+
| main.rs:184:20:184:33 | Point {...} [Point.y] | main.rs:184:31:184:31 | y | provenance | |
59+
| main.rs:184:31:184:31 | y | main.rs:188:18:188:18 | y | provenance | |
5260
| main.rs:198:9:198:10 | s1 [Some] | main.rs:200:11:200:12 | s1 [Some] | provenance | |
5361
| main.rs:198:14:198:37 | ...::Some(...) [Some] | main.rs:198:9:198:10 | s1 [Some] | provenance | |
5462
| main.rs:198:27:198:36 | source(...) | main.rs:198:14:198:37 | ...::Some(...) [Some] | provenance | |
@@ -226,6 +234,15 @@ nodes
226234
| main.rs:151:9:151:28 | Point {...} [Point.x] | semmle.label | Point {...} [Point.x] |
227235
| main.rs:151:20:151:20 | a | semmle.label | a |
228236
| main.rs:152:10:152:10 | a | semmle.label | a |
237+
| main.rs:175:9:175:9 | p [Point3D.plane, Point.y] | semmle.label | p [Point3D.plane, Point.y] |
238+
| main.rs:175:13:181:5 | Point3D {...} [Point3D.plane, Point.y] | semmle.label | Point3D {...} [Point3D.plane, Point.y] |
239+
| main.rs:176:16:179:9 | Point {...} [Point.y] | semmle.label | Point {...} [Point.y] |
240+
| main.rs:178:16:178:25 | source(...) | semmle.label | source(...) |
241+
| main.rs:182:11:182:11 | p [Point3D.plane, Point.y] | semmle.label | p [Point3D.plane, Point.y] |
242+
| main.rs:183:9:186:9 | Point3D {...} [Point3D.plane, Point.y] | semmle.label | Point3D {...} [Point3D.plane, Point.y] |
243+
| main.rs:184:20:184:33 | Point {...} [Point.y] | semmle.label | Point {...} [Point.y] |
244+
| main.rs:184:31:184:31 | y | semmle.label | y |
245+
| main.rs:188:18:188:18 | y | semmle.label | y |
229246
| main.rs:198:9:198:10 | s1 [Some] | semmle.label | s1 [Some] |
230247
| main.rs:198:14:198:37 | ...::Some(...) [Some] | semmle.label | ...::Some(...) [Some] |
231248
| main.rs:198:27:198:36 | source(...) | semmle.label | source(...) |
@@ -384,6 +401,7 @@ testFailures
384401
| main.rs:113:10:113:12 | a.0 | main.rs:111:11:111:20 | source(...) | main.rs:113:10:113:12 | a.0 | $@ | main.rs:111:11:111:20 | source(...) | source(...) |
385402
| main.rs:121:10:121:15 | ... .1 | main.rs:118:17:118:26 | source(...) | main.rs:121:10:121:15 | ... .1 | $@ | main.rs:118:17:118:26 | source(...) | source(...) |
386403
| main.rs:152:10:152:10 | a | main.rs:148:12:148:21 | source(...) | main.rs:152:10:152:10 | a | $@ | main.rs:148:12:148:21 | source(...) | source(...) |
404+
| main.rs:188:18:188:18 | y | main.rs:178:16:178:25 | source(...) | main.rs:188:18:188:18 | y | $@ | main.rs:178:16:178:25 | source(...) | source(...) |
387405
| main.rs:201:33:201:33 | n | main.rs:198:27:198:36 | source(...) | main.rs:201:33:201:33 | n | $@ | main.rs:198:27:198:36 | source(...) | source(...) |
388406
| main.rs:214:25:214:25 | n | main.rs:211:19:211:28 | source(...) | main.rs:214:25:214:25 | n | $@ | main.rs:211:19:211:28 | source(...) | source(...) |
389407
| main.rs:225:10:225:20 | s1.unwrap(...) | main.rs:224:19:224:28 | source(...) | main.rs:225:10:225:20 | s1.unwrap(...) | $@ | main.rs:224:19:224:28 | source(...) | source(...) |

rust/ql/test/library-tests/dataflow/local/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ fn struct_nested_match() {
185185
z,
186186
} => {
187187
sink(x);
188-
sink(y); // MISSING: hasValueFlow=93
188+
sink(y); // $ hasValueFlow=93
189189
sink(z);
190190
}
191191
}

0 commit comments

Comments
 (0)