Skip to content

Commit

Permalink
Fix findReceiverScopeFunctionLiteral to only resolve scope functions …
Browse files Browse the repository at this point in the history
…if their argument is used
  • Loading branch information
leveretka authored and Godin committed Nov 28, 2024
1 parent 0b5895f commit 71f5ded
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,15 @@ class CollectionInappropriateCallsCheckSample {
intArray.lastIndexOf(10)
}

// The collection type was resolved incorrectly and led to java.lang.IndexOutOfBoundsException
// We should only resolve let/apply/with if its argument was used as a receiver
fun bugWithLetApplyWithResolution(s: String?, params: Map<String, Array<String>>) = s?.let {
val params1 = params.get("ABC")!!
mutableListOf(params1.indexOf(it))
}

fun bugWithLetApplyWithResolution(s: String?, params: Array<String>?) = params?.let {
mutableListOf(it.indexOf(s))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,12 @@ private fun KaImplicitReceiverValue.findReceiverScopeFunctionLiteral(
@Rewritten
private fun KtReferenceExpression.findReceiverScopeFunctionLiteral(): KtFunctionLiteral? =
analyze {
this@findReceiverScopeFunctionLiteral.mainReference.resolveToSymbol()?.containingSymbol?.findFunctionLiteral(
this@findReceiverScopeFunctionLiteral
)
when (val resolvedSymbol = this@findReceiverScopeFunctionLiteral.mainReference.resolveToSymbol()) {
is KaValueParameterSymbol -> resolvedSymbol.containingSymbol?.findFunctionLiteral(
this@findReceiverScopeFunctionLiteral
)
else -> null
}
}


Expand Down

0 comments on commit 71f5ded

Please sign in to comment.