Skip to content

Commit 00688eb

Browse files
authored
Merge pull request #18312 from hvitved/rust/operator-overloading-test
Rust: Add data flow tests for operator overloading
2 parents 927d359 + 3a63dbc commit 00688eb

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,38 @@ 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+
// Ignore `_other` to get value flow for `self.value`
105+
MyInt { value: self.value }
106+
}
107+
}
108+
109+
pub fn test_operator_overloading() {
110+
let a = MyInt { value: source(5) };
111+
let b = MyInt { value: 2 };
112+
let c = a + b;
113+
sink(c.value); // $ MISSING: hasValueFlow=5
114+
115+
let a = MyInt { value: 2 };
116+
let b = MyInt { value: source(6) };
117+
let d = a + b;
118+
sink(d.value);
119+
120+
let a = MyInt { value: source(7) };
121+
let b = MyInt { value: 2 };
122+
let d = a.add(b);
123+
sink(d.value); // $ MISSING: hasValueFlow=7
124+
}
125+
94126
fn main() {
95127
data_out_of_call();
96128
data_in_to_call();
@@ -99,4 +131,6 @@ fn main() {
99131
data_out_of_method();
100132
data_in_to_method_call();
101133
data_through_method();
134+
135+
test_operator_overloading();
102136
}

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:110:28:110:36 | source(...) | main.rs:1:1:3:1 | fn source |
23+
| main.rs:113:5:113:17 | sink(...) | main.rs:5:1:7:1 | fn sink |
24+
| main.rs:116:28:116:36 | source(...) | main.rs:1:1:3:1 | fn source |
25+
| main.rs:118:5:118:17 | sink(...) | main.rs:5:1:7:1 | fn sink |
26+
| main.rs:120:28:120:36 | source(...) | main.rs:1:1:3:1 | fn source |
27+
| main.rs:122:13:122:20 | a.add(...) | main.rs:103:5:106:5 | fn add |
28+
| main.rs:123:5:123:17 | sink(...) | main.rs:5:1:7:1 | fn sink |
29+
| main.rs:127:5:127:22 | data_out_of_call(...) | main.rs:16:1:19:1 | fn data_out_of_call |
30+
| main.rs:128:5:128:21 | data_in_to_call(...) | main.rs:25:1:28:1 | fn data_in_to_call |
31+
| main.rs:129:5:129:23 | data_through_call(...) | main.rs:34:1:38:1 | fn data_through_call |
32+
| main.rs:131:5:131:24 | data_out_of_method(...) | main.rs:75:1:79:1 | fn data_out_of_method |
33+
| main.rs:132:5:132:28 | data_in_to_method_call(...) | main.rs:81:1:85:1 | fn data_in_to_method_call |
34+
| main.rs:133:5:133:25 | data_through_method(...) | main.rs:87:1:92:1 | fn data_through_method |
35+
| main.rs:135:5:135:31 | test_operator_overloading(...) | main.rs:109:1:124:1 | fn test_operator_overloading |

0 commit comments

Comments
 (0)