only resolve module types if we know that the TypeExpr could possibly resolve to a module

This commit is contained in:
Erik Krogh Kristensen
2022-06-17 21:32:19 +02:00
parent a59f0d36f5
commit 7e93416e97
4 changed files with 26 additions and 2 deletions

View File

@@ -682,8 +682,17 @@ class TypeExpr extends TType, TypeRef {
// resolve type
resolveTypeExpr(this, result)
or
// if it resolves to a module
exists(FileOrModule mod | resolveModuleRef(this, mod) | result = mod.toType())
// if it resolves to a module,
exists(FileOrModule mod | resolveModuleRef(this, mod) | result = mod.toType()) and
result instanceof ModuleType and
// we can get spurious results in some cases, so we restrict to where it is possible to have a module.
(
// only possible if this is inside a moduleInstantiation.
this = any(ModuleExpr mod).getArgument(_).asType()
or
// or if it's a parameter to a parameterized module
this = any(SignatureExpr sig, Module mod | mod.hasParameter(_, _, sig) | sig).asType()
)
}
override AstNode getAChild(string pred) {

View File

@@ -0,0 +1,11 @@
class Foo extends string {
Foo() { this = "Foo" }
}
predicate test(Foo f) { f.(Foo).toString() = "X" }
predicate test2(Foo a, Foo b) { a.(Foo) = b }
predicate called(Foo a) { a.toString() = "X" }
predicate test3(string s) { called(s.(Foo)) }

View File

@@ -0,0 +1,3 @@
| Foo.qll:5:25:5:31 | InlineCast | Redundant cast to $@ | Foo.qll:5:28:5:30 | TypeExpr | Foo |
| Foo.qll:7:33:7:39 | InlineCast | Redundant cast to $@ | Foo.qll:7:36:7:38 | TypeExpr | Foo |
| Foo.qll:11:36:11:42 | InlineCast | Redundant cast to $@ | Foo.qll:11:39:11:41 | TypeExpr | Foo |

View File

@@ -0,0 +1 @@
queries/style/RedundantCast.ql