Skip to content

Commit 025a673

Browse files
committed
Rust: Add data flow tests for operator overloading
1 parent 8efd870 commit 025a673

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,37 @@ fn data_through_method() {
9191
sink(b); // $ hasValueFlow=4
9292
}
9393

94+
use std::ops::Add;
95+
96+
struct MyInt {
97+
value: i64,
98+
}
99+
100+
impl Add for MyInt {
101+
type Output = MyInt;
102+
103+
fn add(self, other: MyInt) -> MyInt {
104+
MyInt { value: self.value }
105+
}
106+
}
107+
108+
pub fn test_operator_overloading() {
109+
let a = MyInt { value: source(5) };
110+
let b = MyInt { value: 2 };
111+
let c = a + b;
112+
sink(c.value); // $ MISSING: hasValueFlow=5
113+
114+
let a = MyInt { value: source(6) };
115+
let b = MyInt { value: 2 };
116+
let d = b + a;
117+
sink(d.value);
118+
119+
let a = MyInt { value: source(7) };
120+
let b = MyInt { value: 2 };
121+
let d = a.add(b);
122+
sink(d.value); // $ MISSING: hasValueFlow=7
123+
}
124+
94125
fn main() {
95126
data_out_of_call();
96127
data_in_to_call();
@@ -99,4 +130,6 @@ fn main() {
99130
data_out_of_method();
100131
data_in_to_method_call();
101132
data_through_method();
133+
134+
test_operator_overloading();
102135
}

rust/ql/test/library-tests/dataflow/global/viableCallable.expected

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,17 @@
1919
| main.rs:89:13:89:21 | source(...) | main.rs:1:1:3:1 | fn source |
2020
| main.rs:90:13:90:30 | mn.data_through(...) | main.rs:66:5:72:5 | fn data_through |
2121
| main.rs:91:5:91:11 | sink(...) | main.rs:5:1:7:1 | fn sink |
22-
| main.rs:95:5:95:22 | data_out_of_call(...) | main.rs:16:1:19:1 | fn data_out_of_call |
23-
| main.rs:96:5:96:21 | data_in_to_call(...) | main.rs:25:1:28:1 | fn data_in_to_call |
24-
| main.rs:97:5:97:23 | data_through_call(...) | main.rs:34:1:38:1 | fn data_through_call |
25-
| main.rs:99:5:99:24 | data_out_of_method(...) | main.rs:75:1:79:1 | fn data_out_of_method |
26-
| main.rs:100:5:100:28 | data_in_to_method_call(...) | main.rs:81:1:85:1 | fn data_in_to_method_call |
27-
| main.rs:101:5:101:25 | data_through_method(...) | main.rs:87:1:92:1 | fn data_through_method |
22+
| main.rs:109:28:109:36 | source(...) | main.rs:1:1:3:1 | fn source |
23+
| main.rs:112:5:112:17 | sink(...) | main.rs:5:1:7:1 | fn sink |
24+
| main.rs:114:28:114:36 | source(...) | main.rs:1:1:3:1 | fn source |
25+
| main.rs:117:5:117:17 | sink(...) | main.rs:5:1:7:1 | fn sink |
26+
| main.rs:119:28:119:36 | source(...) | main.rs:1:1:3:1 | fn source |
27+
| main.rs:121:13:121:20 | a.add(...) | main.rs:103:5:105:5 | fn add |
28+
| main.rs:122:5:122:17 | sink(...) | main.rs:5:1:7:1 | fn sink |
29+
| main.rs:126:5:126:22 | data_out_of_call(...) | main.rs:16:1:19:1 | fn data_out_of_call |
30+
| main.rs:127:5:127:21 | data_in_to_call(...) | main.rs:25:1:28:1 | fn data_in_to_call |
31+
| main.rs:128:5:128:23 | data_through_call(...) | main.rs:34:1:38:1 | fn data_through_call |
32+
| main.rs:130:5:130:24 | data_out_of_method(...) | main.rs:75:1:79:1 | fn data_out_of_method |
33+
| main.rs:131:5:131:28 | data_in_to_method_call(...) | main.rs:81:1:85:1 | fn data_in_to_method_call |
34+
| main.rs:132:5:132:25 | data_through_method(...) | main.rs:87:1:92:1 | fn data_through_method |
35+
| main.rs:134:5:134:31 | test_operator_overloading(...) | main.rs:108:1:123:1 | fn test_operator_overloading |

0 commit comments

Comments
 (0)