Skip to content

Commit 1518cad

Browse files
committed
Address review comments
1 parent 6876838 commit 1518cad

File tree

2 files changed

+24
-35
lines changed

2 files changed

+24
-35
lines changed

rust/ql/lib/codeql/rust/elements/RangeExprExt.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ private import rust
1313
final class RangeFromExpr extends RangeExpr {
1414
RangeFromExpr() {
1515
this.getOperatorName() = ".." and
16+
this.hasStart() and
1617
not this.hasEnd()
1718
}
1819
}
@@ -26,7 +27,8 @@ final class RangeFromExpr extends RangeExpr {
2627
final class RangeToExpr extends RangeExpr {
2728
RangeToExpr() {
2829
this.getOperatorName() = ".." and
29-
not this.hasStart()
30+
not this.hasStart() and
31+
this.hasEnd()
3032
}
3133
}
3234

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

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,24 @@ private Type inferAssignmentOperationType(AstNode n, TypePath path) {
231231
result = TUnit()
232232
}
233233

234+
pragma[nomagic]
235+
private Struct getRangeType(RangeExpr re) {
236+
re instanceof RangeFromExpr and
237+
result instanceof RangeFromStruct
238+
or
239+
re instanceof RangeToExpr and
240+
result instanceof RangeToStruct
241+
or
242+
re instanceof RangeFromToExpr and
243+
result instanceof RangeStruct
244+
or
245+
re instanceof RangeInclusiveExpr and
246+
result instanceof RangeInclusiveStruct
247+
or
248+
re instanceof RangeToInclusiveExpr and
249+
result instanceof RangeToInclusiveStruct
250+
}
251+
234252
/**
235253
* Holds if the type tree of `n1` at `prefix1` should be equal to the type tree
236254
* of `n2` at `prefix2` and type information should propagate in both directions
@@ -300,22 +318,8 @@ private predicate typeEquality(AstNode n1, TypePath prefix1, AstNode n2, TypePat
300318
exists(Struct s |
301319
n2 = [n1.(RangeExpr).getStart(), n1.(RangeExpr).getEnd()] and
302320
prefix1 = TypePath::singleton(TTypeParamTypeParameter(s.getGenericParamList().getATypeParam())) and
303-
prefix2.isEmpty()
304-
|
305-
n1 instanceof RangeFromExpr and
306-
s instanceof RangeFromStruct
307-
or
308-
n1 instanceof RangeToExpr and
309-
s instanceof RangeToStruct
310-
or
311-
n1 instanceof RangeFromToExpr and
312-
s instanceof RangeStruct
313-
or
314-
n1 instanceof RangeInclusiveExpr and
315-
s instanceof RangeInclusiveStruct
316-
or
317-
n1 instanceof RangeToInclusiveExpr and
318-
s instanceof RangeToInclusiveStruct
321+
prefix2.isEmpty() and
322+
s = getRangeType(n1)
319323
)
320324
}
321325

@@ -1123,24 +1127,7 @@ private Type inferArrayExprType(ArrayExpr ae) { exists(ae) and result = TArrayTy
11231127
* Gets the root type of the range expression `re`.
11241128
*/
11251129
pragma[nomagic]
1126-
private Type inferRangeExprType(RangeExpr re) {
1127-
exists(Struct s | result = TStruct(s) |
1128-
re instanceof RangeFromExpr and
1129-
s instanceof RangeFromStruct
1130-
or
1131-
re instanceof RangeToExpr and
1132-
s instanceof RangeToStruct
1133-
or
1134-
re instanceof RangeFromToExpr and
1135-
s instanceof RangeStruct
1136-
or
1137-
re instanceof RangeInclusiveExpr and
1138-
s instanceof RangeInclusiveStruct
1139-
or
1140-
re instanceof RangeToInclusiveExpr and
1141-
s instanceof RangeToInclusiveStruct
1142-
)
1143-
}
1130+
private Type inferRangeExprType(RangeExpr re) { result = TStruct(getRangeType(re)) }
11441131

11451132
/**
11461133
* According to [the Rust reference][1]: _"array and slice-typed expressions

0 commit comments

Comments
 (0)