Skip to content

Commit 2280796

Browse files
aibaarshvitved
authored andcommitted
Rust: fix QL code after removing Crate::getModule()
1 parent 39a74d6 commit 2280796

File tree

6 files changed

+12
-31
lines changed

6 files changed

+12
-31
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,11 @@ module Impl {
6060
Crate getADependency() { result = this.getDependency(_) }
6161

6262
/** Gets the source file that defines this crate, if any. */
63-
SourceFile getSourceFile() { result.getFile() = this.getModule().getFile() }
63+
SourceFile getSourceFile() { result.getFile() = this.getLocation().getFile() }
6464

6565
/**
6666
* Gets a source file that belongs to this crate, if any.
6767
*/
6868
SourceFile getASourceFile() { result = this.(CrateItemNode).getASourceFile() }
69-
70-
override Location getLocation() { result = this.getModule().getLocation() }
7169
}
7270
}

rust/ql/lib/codeql/rust/frameworks/stdlib/Stdlib.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class OptionEnum extends Enum {
3232
// todo: replace with canonical path, once calculated in QL
3333
exists(Crate core, Module m |
3434
core.getName() = "core" and
35-
m = core.getModule().getItemList().getAnItem() and
35+
m = core.getSourceFile().getAnItem() and
3636
m.getName().getText() = "option" and
3737
this = m.getItemList().getAnItem() and
3838
this.getName().getText() = "Option"
@@ -53,7 +53,7 @@ class ResultEnum extends Enum {
5353
// todo: replace with canonical path, once calculated in QL
5454
exists(Crate core, Module m |
5555
core.getName() = "core" and
56-
m = core.getModule().getItemList().getAnItem() and
56+
m = core.getSourceFile().getAnItem() and
5757
m.getName().getText() = "result" and
5858
this = m.getItemList().getAnItem() and
5959
this.getName().getText() = "Result"

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ query predicate multipleLocations(Locatable e) { strictcount(e.getLocation()) >
2424
/**
2525
* Holds if `e` does not have a `Location`.
2626
*/
27-
query predicate noLocation(Locatable e) {
28-
not exists(e.getLocation()) and
29-
not e.(AstNode).getParentNode*() = any(Crate c).getModule()
30-
}
27+
query predicate noLocation(Locatable e) { not exists(e.getLocation()) }
3128

3229
private predicate multiplePrimaryQlClasses(Element e) {
3330
strictcount(string cls | cls = e.getAPrimaryQlClass() and cls != "VariableAccess") > 1

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

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,7 @@ abstract private class ModuleLikeNode extends ItemNode {
247247
* Holds if this is a root module, meaning either a source file or
248248
* the entry module of a crate.
249249
*/
250-
predicate isRoot() {
251-
this instanceof SourceFileItemNode
252-
or
253-
this = any(Crate c).getModule()
254-
}
250+
predicate isRoot() { this instanceof SourceFileItemNode }
255251
}
256252

257253
private class SourceFileItemNode extends ModuleLikeNode, SourceFile {
@@ -279,12 +275,7 @@ class CrateItemNode extends ItemNode instanceof Crate {
279275
* or a module, when the crate is defined in a dependency.
280276
*/
281277
pragma[nomagic]
282-
ModuleLikeNode getModuleNode() {
283-
result = super.getSourceFile()
284-
or
285-
not exists(super.getSourceFile()) and
286-
result = super.getModule()
287-
}
278+
ModuleLikeNode getModuleNode() { result = super.getSourceFile() }
288279

289280
/**
290281
* Gets a source file that belongs to this crate, if any.
@@ -308,11 +299,7 @@ class CrateItemNode extends ItemNode instanceof Crate {
308299
/**
309300
* Gets a root module node belonging to this crate.
310301
*/
311-
ModuleLikeNode getARootModuleNode() {
312-
result = this.getASourceFile()
313-
or
314-
result = super.getModule()
315-
}
302+
ModuleLikeNode getARootModuleNode() { result = this.getASourceFile() }
316303

317304
pragma[nomagic]
318305
predicate isPotentialDollarCrateTarget() {
@@ -776,7 +763,7 @@ private predicate crateDependencyEdge(ModuleLikeNode m, string name, CrateItemNo
776763
or
777764
// paths inside the crate graph use the name of the crate itself as prefix,
778765
// although that is not valid in Rust
779-
dep = any(Crate c | name = c.getName() and m = c.getModule())
766+
dep = any(Crate c | name = c.getName() and m = c.getSourceFile())
780767
}
781768

782769
private predicate useTreeDeclares(UseTree tree, string name) {
@@ -1120,10 +1107,10 @@ private predicate useImportEdge(Use use, string name, ItemNode item) {
11201107
* [1]: https://doc.rust-lang.org/core/prelude/index.html
11211108
*/
11221109
private predicate preludeEdge(SourceFile f, string name, ItemNode i) {
1123-
exists(Crate core, ModuleItemNode mod, ModuleItemNode prelude, ModuleItemNode rust |
1110+
exists(Crate core, ModuleLikeNode mod, ModuleItemNode prelude, ModuleItemNode rust |
11241111
f = any(Crate c0 | core = c0.getDependency(_)).getASourceFile() and
11251112
core.getName() = "core" and
1126-
mod = core.getModule() and
1113+
mod = core.getSourceFile() and
11271114
prelude = mod.getASuccessorRec("prelude") and
11281115
rust = prelude.getASuccessorRec(["rust_2015", "rust_2018", "rust_2021", "rust_2024"]) and
11291116
i = rust.getASuccessorRec(name) and

rust/ql/src/queries/summary/Stats.qll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ int getQuerySinksCount() { result = count(QuerySink s) }
9191
class CrateElement extends Element {
9292
CrateElement() {
9393
this instanceof Crate or
94-
this instanceof NamedCrate or
95-
this.(AstNode).getParentNode*() = any(Crate c).getModule()
94+
this instanceof NamedCrate
9695
}
9796
}
9897

rust/ql/test/TestUtils.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class CrateElement extends Element {
66
CrateElement() {
77
this instanceof Crate or
88
this instanceof NamedCrate or
9-
any(Crate c).getModule() = this.(AstNode).getParentNode*()
9+
any(Crate c).getSourceFile() = this.(AstNode).getParentNode*()
1010
}
1111
}
1212

0 commit comments

Comments
 (0)