Skip to content

Commit

Permalink
kotlin2cpg: handle destructuring for postfix exprs (#3413)
Browse files Browse the repository at this point in the history
  • Loading branch information
ursachec committed Aug 2, 2023
1 parent 13b2509 commit f9fd26e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,7 @@ trait KtPsiToAst {
case c: KtCallExpression => c
case dqe: KtDotQualifiedExpression => dqe
case ac: KtArrayAccessExpression => ac
case pf: KtPostfixExpression => pf
}
if (typedInit.isEmpty) {
logger.warn(
Expand Down Expand Up @@ -933,6 +934,8 @@ trait KtPsiToAst {
callAst(assignmentNode, List(assignmentLhsAst, Ast(assignmentRhsNode)))
} else if (expr.getInitializer.isInstanceOf[KtArrayAccessExpression]) {
astForArrayAccess(expr.getInitializer.asInstanceOf[KtArrayAccessExpression], None, None)
} else if (expr.getInitializer.isInstanceOf[KtPostfixExpression]) {
astForPostfixExpression(expr.getInitializer.asInstanceOf[KtPostfixExpression], None, None)
} else {
val assignmentNode = operatorCallNode(Operators.assignment, s"$tmpName = ${rhsCall.getText}", None)
val assignmentRhsAst = astsForExpression(rhsCall, None).head
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,4 +590,22 @@ class DestructuringTests extends KotlinCode2CpgFixture(withOssDataflow = false)
c.methodFullName shouldBe Operators.indexAccess
}
}

"CPG for code with destructuring expression with a postfix expression RHS" should {
val cpg = code("""
|package mypkg
|fun x98() {
| data class Entry(val p: String, val q: String)
| val mrr = mapOf("one" to Entry("p1", "q1"), "two" to Entry("p2", "q2"))
| val (p, q) = mrr.get("one")!!
| println(p)
| println(q)
|}
|""".stripMargin)

"should contain an `notNullAssert` CALL node for the RHS" in {
val List(c: Call) = cpg.call.codeExact("mrr.get(\"one\")!!").l
c.methodFullName shouldBe Operators.notNullAssert
}
}
}

0 comments on commit f9fd26e

Please sign in to comment.