Skip to content

Commit 7e93416

Browse files
committed
only resolve module types if we know that the TypeExpr could possibly resolve to a module
1 parent a59f0d3 commit 7e93416

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

ql/ql/src/codeql_ql/ast/Ast.qll

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,17 @@ class TypeExpr extends TType, TypeRef {
682682
// resolve type
683683
resolveTypeExpr(this, result)
684684
or
685-
// if it resolves to a module
686-
exists(FileOrModule mod | resolveModuleRef(this, mod) | result = mod.toType())
685+
// if it resolves to a module,
686+
exists(FileOrModule mod | resolveModuleRef(this, mod) | result = mod.toType()) and
687+
result instanceof ModuleType and
688+
// we can get spurious results in some cases, so we restrict to where it is possible to have a module.
689+
(
690+
// only possible if this is inside a moduleInstantiation.
691+
this = any(ModuleExpr mod).getArgument(_).asType()
692+
or
693+
// or if it's a parameter to a parameterized module
694+
this = any(SignatureExpr sig, Module mod | mod.hasParameter(_, _, sig) | sig).asType()
695+
)
687696
}
688697

689698
override AstNode getAChild(string pred) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Foo extends string {
2+
Foo() { this = "Foo" }
3+
}
4+
5+
predicate test(Foo f) { f.(Foo).toString() = "X" }
6+
7+
predicate test2(Foo a, Foo b) { a.(Foo) = b }
8+
9+
predicate called(Foo a) { a.toString() = "X" }
10+
11+
predicate test3(string s) { called(s.(Foo)) }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| Foo.qll:5:25:5:31 | InlineCast | Redundant cast to $@ | Foo.qll:5:28:5:30 | TypeExpr | Foo |
2+
| Foo.qll:7:33:7:39 | InlineCast | Redundant cast to $@ | Foo.qll:7:36:7:38 | TypeExpr | Foo |
3+
| Foo.qll:11:36:11:42 | InlineCast | Redundant cast to $@ | Foo.qll:11:39:11:41 | TypeExpr | Foo |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/style/RedundantCast.ql

0 commit comments

Comments
 (0)