Skip to content

Commit 6011ecb

Browse files
Copilothvitved
andcommitted
Add ConstAccess class with getConst() predicate and test cases
Co-authored-by: hvitved <3667920+hvitved@users.noreply.github.com>
1 parent 09bef0f commit 6011ecb

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

rust/ql/lib/codeql/rust/elements/Const.qll

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/test/library-tests/const_access/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import rust
2+
import utils.test.InlineExpectationsTest
3+
import TestUtils
4+
5+
query predicate constAccess(ConstAccess ca, Const c) {
6+
toBeTested(ca) and c = ca.getConst()
7+
}
8+
9+
module ConstAccessTest implements TestSig {
10+
string getARelevantTag() { result = "const_access" }
11+
12+
predicate hasActualResult(Location location, string element, string tag, string value) {
13+
exists(ConstAccess ca |
14+
toBeTested(ca) and
15+
location = ca.getLocation() and
16+
element = ca.toString() and
17+
tag = "const_access" and
18+
value = ca.getConst().getName().getText()
19+
)
20+
}
21+
}
22+
23+
import MakeTest<ConstAccessTest>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const GLOBAL_CONST: i32 = 42;
2+
const STRING_CONST: &str = "hello";
3+
4+
struct MyStruct {
5+
value: i32,
6+
}
7+
8+
impl MyStruct {
9+
const ASSOC_CONST: i32 = 100;
10+
}
11+
12+
mod my_module {
13+
pub const MODULE_CONST: i32 = 200;
14+
}
15+
16+
fn use_consts() {
17+
// $ const_access=GLOBAL_CONST
18+
let x = GLOBAL_CONST;
19+
// $ const_access=GLOBAL_CONST
20+
println!("{}", GLOBAL_CONST);
21+
22+
// $ const_access=STRING_CONST
23+
let s = STRING_CONST;
24+
25+
// $ const_access=ASSOC_CONST
26+
let y = MyStruct::ASSOC_CONST;
27+
28+
// $ const_access=MODULE_CONST
29+
let z = my_module::MODULE_CONST;
30+
31+
// $ const_access=GLOBAL_CONST
32+
if GLOBAL_CONST > 0 {
33+
println!("positive");
34+
}
35+
36+
// $ const_access=ASSOC_CONST
37+
let arr = [MyStruct::ASSOC_CONST; 5];
38+
}
39+
40+
fn main() {
41+
use_consts();
42+
}

0 commit comments

Comments
 (0)