Skip to content

Commit bc4b69b

Browse files
committed
Rust: Add ComparisonOperation library.
1 parent 9d65b5f commit bc4b69b

File tree

4 files changed

+91
-6
lines changed

4 files changed

+91
-6
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
private import codeql.rust.elements.Expr
2+
private import codeql.rust.elements.BinaryExpr
3+
private import codeql.rust.elements.Operation
4+
5+
/**
6+
* A comparison operation, such as `==`, `<` or `>=`.
7+
*/
8+
abstract private class ComparisonOperationImpl extends Operation { }
9+
10+
final class ComparisonOperation = ComparisonOperationImpl;
11+
12+
/**
13+
* An equality comparison operation, `==` or `!=`.
14+
*/
15+
abstract private class EqualityOperationImpl extends BinaryExpr, ComparisonOperationImpl { }
16+
17+
final class EqualityOperation = EqualityOperationImpl;
18+
19+
/**
20+
* The equal comparison operation, `==`.
21+
*/
22+
final class EqualOperation extends EqualityOperationImpl, BinaryExpr {
23+
EqualOperation() { this.getOperatorName() = "==" }
24+
}
25+
26+
/**
27+
* The not equal comparison operation, `!=`.
28+
*/
29+
final class NotEqualOperation extends EqualityOperationImpl {
30+
NotEqualOperation() { this.getOperatorName() = "!=" }
31+
}
32+
33+
/**
34+
* A relational comparison operation, that is, one of `<=`, `<`, `>`, or `>=`.
35+
*/
36+
abstract private class RelationalOperationImpl extends BinaryExpr, ComparisonOperationImpl { }
37+
38+
final class RelationalOperation = RelationalOperationImpl;
39+
40+
/**
41+
* The less than comparison operation, `<`.
42+
*/
43+
final class LessThanOperation extends RelationalOperationImpl, BinaryExpr {
44+
LessThanOperation() { this.getOperatorName() = "<" }
45+
}
46+
47+
/**
48+
* The greater than comparison operation, `>?`.
49+
*/
50+
final class GreaterThanOperation extends RelationalOperationImpl, BinaryExpr {
51+
GreaterThanOperation() { this.getOperatorName() = ">" }
52+
}
53+
54+
/**
55+
* The less than or equal comparison operation, `<=`.
56+
*/
57+
final class LessOrEqualOperation extends RelationalOperationImpl, BinaryExpr {
58+
LessOrEqualOperation() { this.getOperatorName() = "<=" }
59+
}
60+
61+
/**
62+
* The less than or equal comparison operation, `>=`.
63+
*/
64+
final class GreaterOrEqualOperation extends RelationalOperationImpl, BinaryExpr {
65+
GreaterOrEqualOperation() { this.getOperatorName() = ">=" }
66+
}

rust/ql/lib/rust.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import codeql.Locations
55
import codeql.files.FileSystem
66
import codeql.rust.elements.Operation
77
import codeql.rust.elements.AssignmentOperation
8+
import codeql.rust.elements.ComparisonOperation
89
import codeql.rust.elements.LiteralExprExt
910
import codeql.rust.elements.LogicalOperation
1011
import codeql.rust.elements.AsyncBlockExpr

rust/ql/test/library-tests/operations/Operations.ql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@ string describe(Expr op) {
1313
op instanceof LogicalOperation and result = "LogicalOperation"
1414
or
1515
op instanceof RefExpr and result = "RefExpr"
16+
or
17+
op instanceof ComparisonOperation and result = "ComparisonOperation"
18+
or
19+
op instanceof EqualityOperation and result = "EqualityOperation"
20+
or
21+
op instanceof EqualOperation and result = "EqualOperation"
22+
or
23+
op instanceof NotEqualOperation and result = "NotEqualOperation"
24+
or
25+
op instanceof RelationalOperation and result = "RelationalOperation"
26+
or
27+
op instanceof LessThanOperation and result = "LessThanOperation"
28+
or
29+
op instanceof GreaterThanOperation and result = "GreaterThanOperation"
30+
or
31+
op instanceof LessOrEqualOperation and result = "LessOrEqualOperation"
32+
or
33+
op instanceof GreaterOrEqualOperation and result = "GreaterOrEqualOperation"
1634
}
1735

1836
module OperationsTest implements TestSig {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ fn test_operations(
1111
x = y; // $ Operation Op== Operands=2 AssignmentOperation BinaryExpr
1212

1313
// comparison operations
14-
x == y; // $ Operation Op=== Operands=2 BinaryExpr
15-
x != y; // $ Operation Op=!= Operands=2 BinaryExpr
16-
x < y; // $ Operation Op=< Operands=2 BinaryExpr
17-
x <= y; // $ Operation Op=<= Operands=2 BinaryExpr
18-
x > y; // $ Operation Op=> Operands=2 BinaryExpr
19-
x >= y; // $ Operation Op=>= Operands=2 BinaryExpr
14+
x == y; // $ Operation Op=== Operands=2 BinaryExpr ComparisonOperation EqualityOperation EqualOperation
15+
x != y; // $ Operation Op=!= Operands=2 BinaryExpr ComparisonOperation EqualityOperation NotEqualOperation
16+
x < y; // $ Operation Op=< Operands=2 BinaryExpr ComparisonOperation RelationalOperation LessThanOperation
17+
x <= y; // $ Operation Op=<= Operands=2 BinaryExpr ComparisonOperation RelationalOperation LessOrEqualOperation
18+
x > y; // $ Operation Op=> Operands=2 BinaryExpr ComparisonOperation RelationalOperation GreaterThanOperation
19+
x >= y; // $ Operation Op=>= Operands=2 BinaryExpr ComparisonOperation RelationalOperation GreaterOrEqualOperation
2020

2121
// arithmetic operations
2222
x + y; // $ Operation Op=+ Operands=2 BinaryExpr

0 commit comments

Comments
 (0)