Swift: add ConstructorDecl.isFailable/0

This commit is contained in:
Nora Dimitrijević
2023-03-29 11:35:22 +02:00
parent 55ce9760e1
commit 3fbf90cbd7
2 changed files with 11 additions and 3 deletions

View File

@@ -602,12 +602,12 @@ predicate storeStep(Node node1, ContentSet c, Node node2) {
c instanceof OptionalSomeContentSet
)
or
// creation of an optional by returning from an optional initializer (`init?`)
exists(ConstructorDecl init, OptionalType initRetType |
// creation of an optional by returning from a failable initializer (`init?`)
exists(ConstructorDecl init |
node1.asExpr().(CallExpr).getStaticTarget() = init and
node2 = node1 and // HACK: again, we should ideally have a separate Node case here, and not reuse the CallExpr
c instanceof OptionalSomeContentSet and
init.getInterfaceType().(FunctionType).getResult().(FunctionType).getResult() = initRetType
init.isFailable()
)
or
FlowSummaryImpl::Private::Steps::summaryStoreStep(node1, c, node2)

View File

@@ -1,9 +1,17 @@
private import codeql.swift.generated.decl.ConstructorDecl
private import codeql.swift.elements.decl.MethodDecl
private import codeql.swift.elements.type.FunctionType
private import codeql.swift.elements.type.OptionalType
/**
* An initializer of a class, struct, enum or protocol.
*/
class ConstructorDecl extends Generated::ConstructorDecl, MethodDecl {
override string toString() { result = this.getSelfParam().getType() + "." + super.toString() }
/** Holds if this initializer returns an optional type. Failable initializers are written as `init?`. */
predicate isFailable() {
this.getInterfaceType().(FunctionType).getResult().(FunctionType).getResult() instanceof
OptionalType
}
}