Skip to content

Commit 3fbf90c

Browse files
committed
Swift: add ConstructorDecl.isFailable/0
1 parent 55ce976 commit 3fbf90c

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

swift/ql/lib/codeql/swift/dataflow/internal/DataFlowPrivate.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,12 +602,12 @@ predicate storeStep(Node node1, ContentSet c, Node node2) {
602602
c instanceof OptionalSomeContentSet
603603
)
604604
or
605-
// creation of an optional by returning from an optional initializer (`init?`)
606-
exists(ConstructorDecl init, OptionalType initRetType |
605+
// creation of an optional by returning from a failable initializer (`init?`)
606+
exists(ConstructorDecl init |
607607
node1.asExpr().(CallExpr).getStaticTarget() = init and
608608
node2 = node1 and // HACK: again, we should ideally have a separate Node case here, and not reuse the CallExpr
609609
c instanceof OptionalSomeContentSet and
610-
init.getInterfaceType().(FunctionType).getResult().(FunctionType).getResult() = initRetType
610+
init.isFailable()
611611
)
612612
or
613613
FlowSummaryImpl::Private::Steps::summaryStoreStep(node1, c, node2)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
private import codeql.swift.generated.decl.ConstructorDecl
22
private import codeql.swift.elements.decl.MethodDecl
3+
private import codeql.swift.elements.type.FunctionType
4+
private import codeql.swift.elements.type.OptionalType
35

46
/**
57
* An initializer of a class, struct, enum or protocol.
68
*/
79
class ConstructorDecl extends Generated::ConstructorDecl, MethodDecl {
810
override string toString() { result = this.getSelfParam().getType() + "." + super.toString() }
11+
12+
/** Holds if this initializer returns an optional type. Failable initializers are written as `init?`. */
13+
predicate isFailable() {
14+
this.getInterfaceType().(FunctionType).getResult().(FunctionType).getResult() instanceof
15+
OptionalType
16+
}
917
}

0 commit comments

Comments
 (0)