Skip to content

Commit f7a45e6

Browse files
committed
Rust: Don't consider parameters in trait method definitions without bodies as variables
1 parent a5ce3c1 commit f7a45e6

File tree

5 files changed

+8
-9
lines changed

5 files changed

+8
-9
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 bodies 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: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
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:4:23:4:25 | val | Variable is assigned a value that is never used. |
23-
| more.rs:8:19:8:21 | val | Variable is assigned a value that is never used. |
2422
| more.rs:24:9:24:11 | val | Variable is assigned a value that is never used. |
2523
| more.rs:46:9:46:14 | a_ptr4 | Variable is assigned a value that is never used. |
2624
| more.rs:61:9:61:13 | d_ptr | Variable is assigned a value that is never used. |

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +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:442:9:442:13 | times | Variable is not used. |
22-
| main.rs:443:9:443:14 | unused | Variable is not used. |
2321
| main.rs:455:9:455:14 | unused | Variable is not used. |

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,8 @@ fn folds_and_closures() {
439439
trait Incrementable {
440440
fn increment(
441441
&mut self,
442-
times: i32, // SPURIOUS: unused variable
443-
unused: i32 // SPURIOUS: unused variable
442+
times: i32,
443+
unused: &mut i32
444444
);
445445
}
446446

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// --- generics ---
22

33
trait MySettable<T> {
4-
fn set(&mut self, val: T); // SPURIOUS: unused value
4+
fn set(&mut self, val: T);
55
}
66

77
trait MyGettable<T> {
8-
fn get(&self, val: T) -> &T; // SPURIOUS: unused value
8+
fn get(&self, val: T) -> &T;
99
}
1010

1111
struct MyContainer<T> {

0 commit comments

Comments
 (0)