Skip to content

Commit e78270b

Browse files
committed
Rust: implement Expr::getType for cases where rust-analyzer does not provide one
1 parent ac9f038 commit e78270b

File tree

6 files changed

+72
-3
lines changed

6 files changed

+72
-3
lines changed

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

Lines changed: 12 additions & 1 deletion
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/FormatTemplateVariableAccessImpl.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ private import codeql.rust.elements.internal.generated.Synth
1111
private import codeql.rust.elements.Format
1212
private import codeql.rust.elements.NamedFormatArgument
1313
private import codeql.Locations
14+
private import codeql.rust.elements.Variable
1415

1516
/**
1617
* INTERNAL: This module contains the customizable definition of `FormatTemplateVariableAccess` and should not
@@ -36,5 +37,9 @@ module Impl {
3637

3738
/** Gets the underlying `NamedFormatArgument` . */
3839
NamedFormatArgument getArgument() { result = argument }
40+
41+
override string getType() {
42+
result = any(Variable v | v.getAnAccess() = this).getPat().getType()
43+
}
3944
}
4045
}

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

Lines changed: 9 additions & 1 deletion
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/LiteralExprImpl.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66

77
private import codeql.rust.elements.internal.generated.LiteralExpr
8+
private import codeql.rust.elements.FormatArgsExpr
9+
private import codeql.rust.elements.LiteralPat
810

911
/**
1012
* INTERNAL: This module contains the customizable definition of `LiteralExpr` and should not
@@ -27,5 +29,13 @@ module Impl {
2729
*/
2830
class LiteralExpr extends Generated::LiteralExpr {
2931
override string toString() { result = this.getTextValue() }
32+
33+
override string getType() {
34+
result = super.getType()
35+
or
36+
exists(FormatArgsExpr f | f.getTemplate() = this and result = "&str")
37+
or
38+
result = any(LiteralPat p | p.getLiteral() = this).getType()
39+
}
3040
}
3141
}

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

Lines changed: 18 additions & 1 deletion
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/PathExprImpl.qll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
private import codeql.rust.elements.internal.generated.PathExpr
8+
private import codeql.rust.elements.CallExpr
89

910
/**
1011
* INTERNAL: This module contains the customizable definition of `PathExpr` and should not
@@ -23,5 +24,22 @@ module Impl {
2324
*/
2425
class PathExpr extends Generated::PathExpr {
2526
override string toString() { result = this.getPath().toString() }
27+
28+
override string getType() {
29+
result = super.getType()
30+
or
31+
// Special case for `rustc_box`; these get translated to `box X` in the HIR layer.
32+
// ```rust
33+
// #[rustc_box]
34+
// alloc.boxed::Box::new(X)
35+
// ```
36+
not exists(super.getType()) and
37+
exists(CallExpr c, string tp |
38+
c.getAnAttr().getMeta().getPath().getPart().getNameRef().getText() = "rustc_box" and
39+
this = c.getExpr() and
40+
tp = c.getArgList().getArg(0).getType() and
41+
result = "fn new<T>(T) -> Box<T, Global>".replaceAll("T", tp)
42+
)
43+
}
2644
}
2745
}

0 commit comments

Comments
 (0)