Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions rust/ql/lib/codeql/rust/elements/Const.qll

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions rust/ql/lib/codeql/rust/elements/internal/ConstImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/

private import codeql.rust.elements.internal.generated.Const
private import codeql.rust.elements.PathExpr
private import codeql.rust.elements.internal.PathExprBaseImpl::Impl as PathExprBaseImpl
private import codeql.rust.internal.PathResolution

/**
* INTERNAL: This module contains the customizable definition of `Const` and should not
Expand All @@ -21,4 +24,34 @@ module Impl {
* ```
*/
class Const extends Generated::Const { }

/**
* A constant access.
*
* For example:
* ```rust
* const X: i32 = 42;
*
* fn main() {
* println!("{}", X);
* }
* ```
*/
class ConstAccess extends PathExprBaseImpl::PathExprBase {
private Const c;

ConstAccess() {
exists(PathExpr pe |
pe = this and
c = resolvePath(pe.getPath())
)
}

/** Gets the constant being accessed. */
Const getConst() { result = c }

override string toStringImpl() { result = c.getName().getText() }

override string getAPrimaryQlClass() { result = "ConstAccess" }
}
}
7 changes: 7 additions & 0 deletions rust/ql/test/library-tests/const_access/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions rust/ql/test/library-tests/const_access/const_access.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import rust
import utils.test.InlineExpectationsTest
import TestUtils

query predicate constAccess(ConstAccess ca, Const c) {
toBeTested(ca) and c = ca.getConst()
}

module ConstAccessTest implements TestSig {
string getARelevantTag() { result = "const_access" }

predicate hasActualResult(Location location, string element, string tag, string value) {
exists(ConstAccess ca |
toBeTested(ca) and
location = ca.getLocation() and
element = ca.toString() and
tag = "const_access" and
value = ca.getConst().getName().getText()
)
}
}

import MakeTest<ConstAccessTest>
42 changes: 42 additions & 0 deletions rust/ql/test/library-tests/const_access/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const GLOBAL_CONST: i32 = 42;
const STRING_CONST: &str = "hello";

struct MyStruct {
value: i32,
}

impl MyStruct {
const ASSOC_CONST: i32 = 100;
}

mod my_module {
pub const MODULE_CONST: i32 = 200;
}

fn use_consts() {
// $ const_access=GLOBAL_CONST
Comment thread
hvitved marked this conversation as resolved.
Outdated
let x = GLOBAL_CONST;
// $ const_access=GLOBAL_CONST
println!("{}", GLOBAL_CONST);

// $ const_access=STRING_CONST
let s = STRING_CONST;

// $ const_access=ASSOC_CONST
let y = MyStruct::ASSOC_CONST;

// $ const_access=MODULE_CONST
let z = my_module::MODULE_CONST;

// $ const_access=GLOBAL_CONST
if GLOBAL_CONST > 0 {
println!("positive");
}

// $ const_access=ASSOC_CONST
let arr = [MyStruct::ASSOC_CONST; 5];
}

fn main() {
use_consts();
}
Loading