Skip to content

Commit 4292b03

Browse files
committed
Rust: Add logic for Vecs and slices.
1 parent 66d6770 commit 4292b03

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

rust/ql/lib/codeql/rust/internal/TypeInference.qll

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,10 +1140,17 @@ private Type inferForLoopExprType(AstNode n, TypePath path) {
11401140
exists(ForExpr fe, Type iterableType, TypePath iterablePath |
11411141
n = fe.getPat() and
11421142
iterableType = inferType(fe.getIterable(), iterablePath) and
1143+
result = iterableType and
11431144
(
1144-
iterablePath.isCons(any(ArrayTypeParameter tp), path) and
1145-
result = iterableType
1146-
// TODO: iterables (containers, ranges etc)
1145+
iterablePath.isCons(any(Vec v).getElementTypeParameter(), path)
1146+
or
1147+
iterablePath.isCons(any(ArrayTypeParameter tp), path)
1148+
or
1149+
exists(TypePath path0 |
1150+
iterablePath.isCons(any(RefTypeParameter tp), path0) and
1151+
path0.isCons(any(SliceTypeParameter tp), path)
1152+
)
1153+
// TODO: iterables (general case for containers, ranges etc)
11471154
)
11481155
)
11491156
}

rust/ql/test/library-tests/type-inference/type-inference.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2792,6 +2792,8 @@ inferType
27922792
| main.rs:1875:40:1875:40 | 2 | | {EXTERNAL LOCATION} | u64 |
27932793
| main.rs:1875:43:1875:43 | 3 | | {EXTERNAL LOCATION} | i32 |
27942794
| main.rs:1875:43:1875:43 | 3 | | {EXTERNAL LOCATION} | u64 |
2795+
| main.rs:1876:13:1876:13 | u | | file://:0:0:0:0 | & |
2796+
| main.rs:1876:13:1876:13 | u | &T | {EXTERNAL LOCATION} | u64 |
27952797
| main.rs:1876:18:1876:22 | vals4 | | {EXTERNAL LOCATION} | Vec |
27962798
| main.rs:1876:18:1876:22 | vals4 | T | file://:0:0:0:0 | & |
27972799
| main.rs:1876:18:1876:22 | vals4 | T.&T | {EXTERNAL LOCATION} | u64 |

0 commit comments

Comments
 (0)