Skip to content

Commit 7db90fe

Browse files
authored
Merge pull request github#17847 from paldepind/rust-unused-variable-trait
Rust: Don't consider parameters in trait method definitions without bodies as variables
2 parents 7338eaf + bfa6113 commit 7db90fe

File tree

5 files changed

+43
-46
lines changed

5 files changed

+43
-46
lines changed

rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ module Impl {
8484
// an enum constant (e.g. `None`). This excludes static and constant variables (UPPERCASE),
8585
// which we don't appear to recognize yet anyway. This also assumes programmers follow the
8686
// naming guidelines, which they generally do, but they're not enforced.
87-
not name.charAt(0).isUppercase()
87+
not name.charAt(0).isUppercase() and
88+
// exclude parameters from functions without a body as these are trait method declarations
89+
// without implementations
90+
not exists(Function f | not f.hasBody() and f.getParamList().getAParam().getPat() = p)
8891
}
8992

9093
/** A variable. */

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,10 @@
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. |
22+
| more.rs:24:9:24:11 | val | Variable is assigned a value that is never used. |
23+
| more.rs:46:9:46:14 | a_ptr4 | Variable is assigned a value that is never used. |
24+
| more.rs:61:9:61:13 | d_ptr | Variable is assigned a value that is never used. |
25+
| more.rs:67:9:67:17 | f_ptr | Variable is assigned a value that is never used. |
3126
| unreachable.rs:166:6:166:6 | x | Variable is assigned a value that is never used. |
3227
| unreachable.rs:190:14:190:14 | a | Variable is assigned a value that is never used. |
3328
| unreachable.rs:199:9:199:9 | a | Variable is assigned a value that is never used. |

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@
1818
| main.rs:379:21:379:21 | y | Variable is not used. |
1919
| main.rs:427:27:427:29 | val | Variable is not used. |
2020
| main.rs:430:22:430:24 | acc | Variable is not used. |
21+
| main.rs:455:9:455:14 | unused | Variable is not used. |

rust/ql/test/query-tests/unusedentities/main.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,38 @@ fn folds_and_closures() {
434434
_ = a6.fold(0, | acc, val | acc + val + i6);
435435
}
436436

437+
// --- traits ---
438+
439+
trait Incrementable {
440+
fn increment(
441+
&mut self,
442+
times: i32,
443+
unused: &mut i32
444+
);
445+
}
446+
447+
struct MyValue {
448+
value: i32,
449+
}
450+
451+
impl Incrementable for MyValue {
452+
fn increment(
453+
&mut self,
454+
times: i32,
455+
unused: i32 // BAD: unused variable
456+
) {
457+
self.value += times;
458+
}
459+
}
460+
461+
fn traits() {
462+
let mut i = MyValue { value: 0 };
463+
let a = 1;
464+
let b = 2;
465+
466+
i.increment(a, b);
467+
}
468+
437469
// --- main ---
438470

439471
fn main() {

rust/ql/test/query-tests/unusedentities/more.rs

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,11 @@
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-
351
// --- generics ---
362

373
trait MySettable<T> {
38-
fn set(&mut self, val: T); // SPURIOUS: unused value
4+
fn set(&mut self, val: T);
395
}
406

417
trait MyGettable<T> {
42-
fn get(&self, val: T) -> &T; // SPURIOUS: unused value
8+
fn get(&self, val: T) -> &T;
439
}
4410

4511
struct MyContainer<T> {

0 commit comments

Comments
 (0)