Skip to content

Commit 4577d1c

Browse files
committed
Rust: Additional test cases.
1 parent 5b66702 commit 4577d1c

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

rust/ql/test/query-tests/unusedentities/UnusedValue.expected

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,12 @@
1919
| main.rs:370:9:370:9 | x | Variable is assigned a value that is never used. |
2020
| main.rs:378:17:378:17 | x | Variable is assigned a value that is never used. |
2121
| main.rs:432:9:432:10 | i6 | Variable is assigned a value that is never used. |
22+
| more.rs:8:9:8:13 | times | Variable is assigned a value that is never used. |
23+
| more.rs:9:9:9:14 | unused | Variable is assigned a value that is never used. |
24+
| more.rs:21:9:21:14 | unused | Variable is assigned a value that is never used. |
25+
| more.rs:38:23:38:25 | val | Variable is assigned a value that is never used. |
26+
| more.rs:42:19:42:21 | val | Variable is assigned a value that is never used. |
27+
| more.rs:58:9:58:11 | val | Variable is assigned a value that is never used. |
28+
| more.rs:80:9:80:14 | a_ptr4 | Variable is assigned a value that is never used. |
29+
| more.rs:95:9:95:13 | d_ptr | Variable is assigned a value that is never used. |
30+
| more.rs:101:9:101:17 | f_ptr | Variable is assigned a value that is never used. |
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
2+
3+
// --- traits ---
4+
5+
trait Incrementable {
6+
fn increment(
7+
&mut self,
8+
times: i32, // SPURIOUS: unused value
9+
unused: i32 // SPURIOUS: unused value
10+
);
11+
}
12+
13+
struct MyValue {
14+
value: i32,
15+
}
16+
17+
impl Incrementable for MyValue {
18+
fn increment(
19+
&mut self,
20+
times: i32,
21+
unused: i32 // BAD: unused variable [NOT DETECTED] SPURIOUS: unused value
22+
) {
23+
self.value += times;
24+
}
25+
}
26+
27+
fn traits() {
28+
let mut i = MyValue { value: 0 };
29+
let a = 1;
30+
let b = 2;
31+
32+
i.increment(a, b);
33+
}
34+
35+
// --- generics ---
36+
37+
trait MySettable<T> {
38+
fn set(&mut self, val: T); // SPURIOUS: unused value
39+
}
40+
41+
trait MyGettable<T> {
42+
fn get(&self, val: T) -> &T; // SPURIOUS: unused value
43+
}
44+
45+
struct MyContainer<T> {
46+
val: T
47+
}
48+
49+
impl<T> MySettable<T> for MyContainer<T> {
50+
fn set(&mut self, val: T) {
51+
self.val = val;
52+
}
53+
}
54+
55+
impl<T> MyGettable<T> for MyContainer<T> {
56+
fn get(
57+
&self,
58+
val: T // BAD: unused variable [NOT DETECTED] SPURIOUS: unused value
59+
) -> &T {
60+
return &(self.val);
61+
}
62+
}
63+
64+
fn generics() {
65+
let mut a = MyContainer { val: 1 }; // BAD: unused value [NOT DETECTED]
66+
let b = MyContainer { val: 2 };
67+
68+
a.set(
69+
*b.get(3)
70+
);
71+
}
72+
73+
// --- pointers ---
74+
75+
fn pointers() {
76+
let a = 1;
77+
let a_ptr1 = &a;
78+
let a_ptr2 = &a;
79+
let a_ptr3 = &a; // BAD: unused value [NOT DETECTED]
80+
let a_ptr4 = &a; // BAD: unused value
81+
println!("{}", *a_ptr1);
82+
println!("{}", a_ptr2);
83+
println!("{}", &a_ptr3);
84+
85+
let b = 2; // BAD: unused value [NOT DETECTED]
86+
let b_ptr = &b;
87+
println!("{}", b_ptr);
88+
89+
let c = 3;
90+
let c_ptr = &c;
91+
let c_ptr_ptr = &c_ptr;
92+
println!("{}", **c_ptr_ptr);
93+
94+
let d = 4;
95+
let d_ptr = &d; // BAD: unused value
96+
let d_ptr_ptr = &&d;
97+
println!("{}", **d_ptr_ptr);
98+
99+
let e = 5; // BAD: unused value [NOT DETECTED]
100+
let f = 6;
101+
let mut f_ptr = &e; // BAD: unused value
102+
f_ptr = &f;
103+
println!("{}", *f_ptr);
104+
105+
let mut g = 7; // BAD: unused value [NOT DETECTED]
106+
let g_ptr = &mut g;
107+
*g_ptr = 77; // BAD: unused value [NOT DETECTED]
108+
109+
let mut h = 8; // BAD: unused value [NOT DETECTED]
110+
let h_ptr = &mut h;
111+
*h_ptr = 88;
112+
println!("{}", h);
113+
114+
let mut i = 9; // BAD: unused value [NOT DETECTED]
115+
let i_ptr = &mut i;
116+
*i_ptr = 99;
117+
let i_ptr2 = &mut i;
118+
println!("{}", *i_ptr2);
119+
}

0 commit comments

Comments
 (0)