Skip to content

Commit 3fd1909

Browse files
committed
Rust: Minor cleanup
1 parent c972bb8 commit 3fd1909

3 files changed

Lines changed: 20 additions & 33 deletions

File tree

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ private import codeql.rust.elements.internal.generated.Meta
1111
* be referenced directly.
1212
*/
1313
module Impl {
14+
private import rust
15+
1416
// the following QLdoc is generated: if you need to edit it, do it in the schema file
1517
/**
1618
* A meta item in an attribute.
@@ -26,5 +28,15 @@ module Impl {
2628
* }
2729
* ```
2830
*/
29-
class Meta extends Generated::Meta { }
31+
class Meta extends Generated::Meta {
32+
/** Gets the path, if it exists. */
33+
pragma[nomagic]
34+
Path getMetaPath() {
35+
result = this.(PathMeta).getPath()
36+
or
37+
result = this.(KeyValueMeta).getPath()
38+
or
39+
result = this.(TokenTreeMeta).getPath()
40+
}
41+
}
3042
}

rust/ql/lib/codeql/rust/internal/PathResolution.qll

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,6 @@ private import codeql.rust.internal.CachedStages
4949
private import codeql.rust.frameworks.stdlib.Builtins as Builtins
5050
private import codeql.util.Option
5151

52-
/** Gets the path of a `Meta` node, regardless of its concrete subtype. */
53-
pragma[nomagic]
54-
private Path getMetaPath(Meta m) {
55-
result = m.(PathMeta).getPath()
56-
or
57-
result = m.(KeyValueMeta).getPath()
58-
or
59-
result = m.(TokenTreeMeta).getPath()
60-
}
61-
62-
/** Gets the expression of a `Meta` node, regardless of its concrete subtype. */
63-
pragma[nomagic]
64-
private Expr getMetaExpr(Meta m) { result = m.(KeyValueMeta).getExpr() }
65-
6652
private newtype TNamespace =
6753
TTypeNamespace() or
6854
TValueNamespace() or
@@ -277,7 +263,7 @@ abstract class ItemNode extends Locatable {
277263
pragma[nomagic]
278264
final Attr getAttr(string name) {
279265
result = this.getAnAttr() and
280-
getMetaPath(result.getMeta()).(PathExt).isUnqualified(name)
266+
result.getMeta().getMetaPath().(PathExt).isUnqualified(name)
281267
}
282268

283269
final predicate hasAttr(string name) { exists(this.getAttr(name)) }
@@ -1394,7 +1380,7 @@ private predicate fileModuleInlineLate(SourceFile f, string name, Folder folder)
13941380
*/
13951381
private Meta getPathAttrMeta(Module m) {
13961382
result = m.getAnAttr().getMeta() and
1397-
getMetaPath(result).getText() = "path"
1383+
result.getMetaPath().getText() = "path"
13981384
}
13991385

14001386
/**
@@ -1455,10 +1441,10 @@ private predicate modImportNestedLookup(Module m, ModuleItemNode ancestor, Folde
14551441
}
14561442

14571443
private predicate pathAttrImport(Folder f, Module m, string relativePath) {
1458-
exists(Meta meta |
1444+
exists(KeyValueMeta meta |
14591445
f = m.getFile().getParentContainer() and
14601446
meta = getPathAttrMeta(m) and
1461-
relativePath = getMetaExpr(meta).(LiteralExpr).getTextValue().regexpCapture("\"(.+)\"", 1)
1447+
relativePath = meta.getExpr().(LiteralExpr).getTextValue().regexpCapture("\"(.+)\"", 1)
14621448
)
14631449
}
14641450

@@ -1839,7 +1825,7 @@ private module DollarCrateResolution {
18391825
or
18401826
exists(ItemNode type |
18411827
expansion = type.(TypeItem).getDeriveMacroExpansion(_) and
1842-
macroDefPath = getMetaPath(type.getAttr("derive").getMeta())
1828+
macroDefPath = type.getAttr("derive").getMeta().getMetaPath()
18431829
)
18441830
}
18451831

@@ -1998,7 +1984,7 @@ private predicate pathUsesNamespace(PathExt p, Namespace n) {
19981984
(
19991985
p = any(MacroCall mc).getPath()
20001986
or
2001-
p = getMetaPath(any(Meta m))
1987+
p = any(Meta m).getMetaPath()
20021988
)
20031989
}
20041990

rust/ql/src/queries/security/CWE-696/BadCtorInitialization.ql

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,6 @@
1212
*/
1313

1414
import rust
15-
private import codeql.rust.elements.PathMeta
16-
private import codeql.rust.elements.KeyValueMeta
17-
private import codeql.rust.elements.TokenTreeMeta
18-
19-
private Path getMetaPath(Meta m) {
20-
result = m.(PathMeta).getPath()
21-
or
22-
result = m.(KeyValueMeta).getPath()
23-
or
24-
result = m.(TokenTreeMeta).getPath()
25-
}
2615

2716
/**
2817
* A `#[ctor]` or `#[dtor]` attribute, that is, a source for this query.
@@ -31,7 +20,7 @@ class CtorAttr extends Attr {
3120
string whichAttr;
3221

3322
CtorAttr() {
34-
whichAttr = getMetaPath(this.getMeta()).getText() and
23+
whichAttr = this.getMeta().getMetaPath().getText() and
3524
whichAttr = ["ctor", "dtor"]
3625
}
3726

0 commit comments

Comments
 (0)