Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Rust] Get receiver type information #20005

Answered by hvitved
Fee0 asked this question in Q&A
Discussion options

I just saw the developer preview for Rust and wanted to try it out.

I'm trying to find all calls to len() on a String object.

pub fn name_length(name: String) -> usize {
 name.len()
}

I expected it to work like this but there is no method for accessing the type information.
I saw other languages having this getType() function. Is Rust different here?

import rust
from MethodCallExpr call
where
 call.getIdentifier().toString() = "len" and
 call.getReceiver().getType().toString() = "String"
select call

From looking at the AST it seems I could get the TypeRepr from the function argument by tracing it back via dataflow analysis.
But I'm thinking there must be an easier way?
I'm new to codeql.

You must be logged in to vote

Type information is currently not exposed in the public API, since we still haven't decided what that API should look like.

In the meantime, you can use the following:

from MethodCallExpr call
where
 call.getStaticTarget().getCanonicalPath() = "<core::str>::len"
select call

Replies: 1 comment

Comment options

Type information is currently not exposed in the public API, since we still haven't decided what that API should look like.

In the meantime, you can use the following:

from MethodCallExpr call
where
 call.getStaticTarget().getCanonicalPath() = "<core::str>::len"
select call
You must be logged in to vote
0 replies
Answer selected by Fee0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /