Skip to content

Commit

Permalink
Add tmp locals for pattern initializers to lambda method bodies (#5204)
Browse files Browse the repository at this point in the history
  • Loading branch information
johannescoetzee authored Jan 6, 2025
1 parent ec2b517 commit 51615c7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -302,19 +302,13 @@ private[expressions] trait AstForLambdasCreator { this: AstCreator =>
val bindingsToLocals = defineCapturedVariables(lambdaMethodName, capturedVariables)
val capturedLocalAsts = bindingsToLocals.map(_._2).map(Ast(_))
val closureBindingEntries = bindingsToLocals.map(_._1)
val temporaryLocalAsts = scope.enclosingMethod.map(_.getTemporaryLocals).getOrElse(Nil).map(Ast(_))

body match {
case block: BlockStmt =>
val blockAst = Ast(blockNode(block))
.withChildren(capturedLocalAsts)
.withChildren(stmts)
LambdaBody(blockAst, closureBindingEntries)
case stmt =>
val blockAst = Ast(blockNode(stmt))
.withChildren(capturedLocalAsts)
.withChildren(stmts)
LambdaBody(blockAst, closureBindingEntries)
}
val blockAst = Ast(blockNode(body))
.withChildren(temporaryLocalAsts)
.withChildren(capturedLocalAsts)
.withChildren(stmts)
LambdaBody(blockAst, closureBindingEntries)
}

private def genericParamTypeMapForLambda(expectedType: ExpectedType): ResolvedTypeParametersMap = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ import io.shiftleft.semanticcpg.language.*

class PatternExprTests extends JavaSrcCode2CpgFixture {

"a pattern initializer in a lambda method" should {
val cpg = code("""
|import java.util.function.Function;
|
|class Foo {
| Function test() {
| return o -> foo() instanceof String s ? s : null;
| }
|}
|""".stripMargin)

"not create any orphan locals" in {
cpg.local.exists(_._astIn.isEmpty) shouldBe false
}
}

"a type pattern in an expression in an explicit constructor" should {
val cpg = code("""
|class Test {
Expand Down

0 comments on commit 51615c7

Please sign in to comment.