Skip to content

Commit 1fc6a0e

Browse files
committed
Rust: Add placeholder query + tests for unused values query.
1 parent 27dca74 commit 1fc6a0e

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
/**
3+
* @name Unused value
4+
* @description Unused values may be an indication that the code is incomplete or has a typo.
5+
* @kind problem
6+
* @problem.severity recommendation
7+
* @precision high
8+
* @id rust/unused-value
9+
* @tags maintainability
10+
*/
11+
12+
select 1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| 1 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/unusedentities/UnusedValue.ql
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
2+
//fn cond() -> bool;
3+
4+
// --- locals ---
5+
6+
fn locals_1() {
7+
let a = 1; // BAD: unused value
8+
let b = 1;
9+
let c = 1;
10+
let d = String::from("a"); // BAD: unused value
11+
let e = String::from("b");
12+
let _ = 1; // (deliberately unused)
13+
14+
println!("use {}", b);
15+
16+
if cond() {
17+
println!("use {}", c);
18+
}
19+
20+
println!("use {}", e);
21+
}
22+
23+
fn locals_2() {
24+
let a: i32;
25+
let b: i32; // BAD: unused variable
26+
let mut c: i32;
27+
let mut d: i32;
28+
let mut e: i32;
29+
let mut f: i32;
30+
let g: i32;
31+
let h: i32;
32+
let i: i32;
33+
34+
b = 1; // BAD: unused value
35+
36+
c = 1; // BAD: unused value
37+
c = 2;
38+
println!("use {}", c);
39+
c = 3; // BAD: unused value
40+
41+
d = 1;
42+
if cond() {
43+
d = 2; // BAD: unused value
44+
d = 3;
45+
} else {
46+
}
47+
println!("use {}", d);
48+
49+
e = 1; // BAD: unused value
50+
if cond() {
51+
e = 2;
52+
} else {
53+
e = 3;
54+
}
55+
println!("use {}", e);
56+
57+
f = 1;
58+
f += 1;
59+
println!("use {}", f);
60+
f += 1; // BAD: unused value
61+
f = 1;
62+
f += 1; // BAD: unused value
63+
64+
g = if cond() { 1 } else { 2 }; // BAD: unused value (x2)
65+
h = if cond() { 3 } else { 4 };
66+
i = if cond() { h } else { 5 };
67+
println!("use {}", i);
68+
69+
_ = 1; // (deliberately unused)
70+
}
71+
72+
// --- structs ---
73+
74+
#[derive(Debug)]
75+
struct MyStruct {
76+
val: i64
77+
}
78+
79+
impl MyStruct {
80+
fn my_get(&mut self) -> i64 {
81+
return self.val;
82+
}
83+
}
84+
85+
fn structs() {
86+
let a = MyStruct {val : 1 }; // BAD: unused value
87+
let b = MyStruct {val : 2 };
88+
let c = MyStruct {val : 3 };
89+
let mut d : MyStruct; // BAD: unused variable
90+
let mut e : MyStruct;
91+
let mut f : MyStruct;
92+
93+
println!("lets use {:?} and {}", b, c.val);
94+
95+
e = MyStruct {val : 4 };
96+
println!("lets use {}", e.my_get());
97+
e.val = 5;
98+
println!("lets use {}", e.my_get());
99+
100+
f = MyStruct {val : 6 }; // BAD: unused value
101+
f.val = 7; // BAD: unused value
102+
}
103+
104+
// --- arrays ---
105+
106+
fn arrays() {
107+
let is = [1, 2, 3]; // BAD: unused values (x3)
108+
let js = [1, 2, 3];
109+
let ks = [1, 2, 3];
110+
111+
println!("lets use {:?}", js);
112+
113+
for k in ks {
114+
println!("lets use {}", k);
115+
}
116+
}
117+
118+
// --- constants and statics ---
119+
120+
const CON1: i32 = 1;
121+
const CON2: i32 = 2; // BAD: unused value
122+
static mut STAT1: i32 = 1;
123+
static mut STAT2: i32 = 2; // BAD: unused value
124+
125+
fn statics() {
126+
static mut STAT3: i32 = 0;
127+
static mut STAT4: i32 = 0; // BAD: unused value
128+
129+
unsafe
130+
{
131+
let total = CON1 + STAT1 + STAT3;
132+
}
133+
}
134+
135+
// --- parameters ---
136+
137+
fn parameters(
138+
x: i32,
139+
y: i32, // BAD: unused variable
140+
_z: i32 // (`_` is asking the compiler, and by extension us, to not warn that this is unused)
141+
) -> i32 {
142+
return x;
143+
}
144+
145+
fn main() {
146+
locals_1();
147+
locals_2();
148+
structs();
149+
arrays();
150+
statics();
151+
println!("lets use result {}", parameters(1, 2, 3));
152+
}

0 commit comments

Comments
 (0)