making LambdaArgument methods nullable

overridden `LambdaArgument.getArgumentExpression` removed because it is already nullable in parent

#EA-117013
This commit is contained in:
Nicolay Mitropolsky
2018-02-26 15:34:28 +03:00
parent 664a25ce81
commit 2f6fb4091b
18 changed files with 47 additions and 45 deletions
@@ -173,7 +173,7 @@ class EffectsExtractingVisitor(
valueArgumentsByIndex?.mapTo(arguments) {
val valueArgument = (it as? ExpressionValueArgument)?.valueArgument ?: return null
when (valueArgument) {
is KtLambdaArgument -> ESLambda(valueArgument.getLambdaExpression())
is KtLambdaArgument -> valueArgument.getLambdaExpression()?.let { ESLambda(it) } ?: return null
else -> extractOrGetCached(valueArgument.getArgumentExpression() ?: return null)
}
} ?: return null
@@ -20,9 +20,7 @@ import com.intellij.lang.ASTNode
class KtLambdaArgument(node: ASTNode) : KtValueArgument(node), LambdaArgument {
override fun getArgumentExpression() = super.getArgumentExpression()!!
override fun getLambdaExpression(): KtLambdaExpression = getArgumentExpression().unpackFunctionLiteral()!!
override fun getLambdaExpression(): KtLambdaExpression? = getArgumentExpression()?.unpackFunctionLiteral()
}
fun KtExpression.unpackFunctionLiteral(allowParentheses: Boolean = false): KtLambdaExpression? {
@@ -37,9 +37,7 @@ interface ValueArgument {
}
interface LambdaArgument : ValueArgument {
fun getLambdaExpression(): KtLambdaExpression
override fun getArgumentExpression(): KtExpression
fun getLambdaExpression(): KtLambdaExpression?
}
interface ValueArgumentName {
@@ -31,11 +31,8 @@ import org.jetbrains.kotlin.resolve.scopes.MemberScopeImpl
import org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.storage.getValue
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.createDynamicType
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.expressions.OperatorConventions
import org.jetbrains.kotlin.types.isDynamic
import org.jetbrains.kotlin.utils.Printer
import java.util.*
@@ -53,7 +50,8 @@ class DynamicCallableDescriptors(storageManager: StorageManager, builtIns: Kotli
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> {
if (isAugmentedAssignmentConvention(name)) return listOf()
if (call.callType == Call.CallType.INVOKE
&& call.valueArgumentList == null && call.functionLiteralArguments.isEmpty()) {
&& call.valueArgumentList == null && call.functionLiteralArguments.isEmpty()
) {
// this means that we are looking for "imaginary" invokes,
// e.g. in `+d` we are looking for property "plus" with member "invoke"
return listOf()
@@ -217,7 +215,10 @@ class DynamicCallableDescriptors(storageManager: StorageManager, builtIns: Kotli
if (hasSpreadOperator) {
for (funLiteralArg in call.functionLiteralArguments) {
addParameter(funLiteralArg, getFunctionType(funLiteralArg.getLambdaExpression()), null)
addParameter(
funLiteralArg,
funLiteralArg.getLambdaExpression()?.let { getFunctionType(it) } ?: TypeUtils.CANT_INFER_FUNCTION_PARAM_TYPE,
null)
}
break
@@ -462,8 +462,8 @@ class PSICallResolver(
oldCall.valueArguments.last()
} else {
if (externalLambdaArguments.size > 2) {
externalLambdaArguments.drop(1).forEach {
context.trace.report(Errors.MANY_LAMBDA_EXPRESSION_ARGUMENTS.on(it.getLambdaExpression()))
externalLambdaArguments.drop(1).mapNotNull { it.getLambdaExpression() }.forEach {
context.trace.report(Errors.MANY_LAMBDA_EXPRESSION_ARGUMENTS.on(it))
}
}
@@ -50,17 +50,13 @@ class ErrorExpressionGenerator(statementGenerator: StatementGenerator) : Stateme
receiverExpression.genExpr()
}
ktCall.valueArguments.forEach {
(ktCall.valueArguments + ktCall.lambdaArguments).forEach {
val ktArgument = it.getArgumentExpression()
if (ktArgument != null) {
irErrorCall.addArgument(ktArgument.genExpr())
}
}
ktCall.lambdaArguments.forEach {
irErrorCall.addArgument(it.getArgumentExpression().genExpr())
}
irErrorCall
}
@@ -76,7 +76,7 @@ class KtInvokeFunctionReference(expression: KtCallExpression) : KtSimpleReferenc
val functionLiteralArguments = expression.lambdaArguments
for (functionLiteralArgument in functionLiteralArguments) {
val functionLiteralExpression = functionLiteralArgument.getArgumentExpression().unpackFunctionLiteral() ?: continue
val functionLiteralExpression = functionLiteralArgument.getLambdaExpression() ?: continue
list.add(getRange(functionLiteralExpression.leftCurlyBrace))
val rightCurlyBrace = functionLiteralExpression.rightCurlyBrace
if (rightCurlyBrace != null) {
@@ -41,6 +41,7 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.getValueArgumentsInParenthese
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.isError
import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments
import org.jetbrains.kotlin.utils.SmartList
@Suppress("UNCHECKED_CAST")
@@ -56,7 +57,10 @@ inline fun <reified T : PsiElement> PsiElement.replaced(newElement: T): T {
fun <T : PsiElement> T.copied(): T = copy() as T
fun KtLambdaArgument.moveInsideParentheses(bindingContext: BindingContext): KtCallExpression {
return moveInsideParenthesesAndReplaceWith(this.getArgumentExpression(), bindingContext)
val ktExpression = this.getArgumentExpression()
?: throw KotlinExceptionWithAttachments("no argument expression for $this")
.withAttachment("lambdaExpression", this.text)
return moveInsideParenthesesAndReplaceWith(ktExpression, bindingContext)
}
fun KtLambdaArgument.moveInsideParenthesesAndReplaceWith(
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.psi.psiUtil.canPlaceAfterSimpleNameEntry
import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression
import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments
import java.util.*
internal abstract class ReplacementPerformer<TElement : KtElement>(
@@ -210,7 +211,10 @@ internal class ExpressionReplacementPerformer(
val runExpression = psiFactory.createExpressionByPattern("run { $0 }", elementToBeReplaced) as KtCallExpression
val runAfterReplacement = elementToBeReplaced.replaced(runExpression)
val block = runAfterReplacement.lambdaArguments[0].getLambdaExpression().bodyExpression!!
val ktLambdaArgument = runAfterReplacement.lambdaArguments[0]
val block = ktLambdaArgument.getLambdaExpression()?.bodyExpression
?: throw KotlinExceptionWithAttachments("cant get body expression for $ktLambdaArgument")
.withAttachment("ktLambdaArgument", ktLambdaArgument.text)
elementToBeReplaced = block.statements.single()
return elementToBeReplaced
@@ -164,7 +164,7 @@ private fun UsageReplacementStrategy.specialUsageProcessing(usage: KtSimpleNameE
val grandParent = usageParent.parent
val specifySignature = SpecifyExplicitLambdaSignatureIntention()
for (lambdaArgument in lambdaArguments) {
val lambdaExpression = lambdaArgument.getLambdaExpression()
val lambdaExpression = lambdaArgument.getLambdaExpression() ?: continue
val functionDescriptor =
lambdaExpression.functionLiteral.resolveToDescriptorIfAny() as? FunctionDescriptor ?: continue
if (functionDescriptor.valueParameters.isNotEmpty()) {
@@ -217,7 +217,9 @@ public class KotlinExpressionMover extends AbstractKotlinUpDownMover {
List<KtLambdaArgument> functionLiterals = callExpression.getLambdaArguments();
if (functionLiterals.isEmpty()) return null;
return functionLiterals.get(0).getLambdaExpression().getBodyExpression();
KtLambdaExpression lambdaExpression = functionLiterals.get(0).getLambdaExpression();
if (lambdaExpression == null) return null;
return lambdaExpression.getBodyExpression();
}
@Nullable
@@ -72,9 +72,9 @@ private fun getCounterpart(expression: KtCallExpression): String? {
val callee = expression.calleeExpression as? KtNameReferenceExpression ?: return null
val calleeName = callee.getReferencedName()
val counterpartName = counterpartNames[calleeName]
val lambdaArgument = expression.lambdaArguments.singleOrNull()
if (counterpartName != null && lambdaArgument != null) {
if (lambdaArgument.getLambdaExpression().valueParameters.isNotEmpty()) {
val lambdaExpression = expression.lambdaArguments.singleOrNull()?.getLambdaExpression()
if (counterpartName != null && lambdaExpression != null) {
if (lambdaExpression.valueParameters.isNotEmpty()) {
return null
}
val bindingContext = callee.analyze(BodyResolveMode.PARTIAL)
@@ -151,7 +151,7 @@ abstract class ConvertScopeFunctionFix(private val counterpartName: String) : Lo
val bindingContext = callExpression.analyze()
val lambda = callExpression.lambdaArguments.firstOrNull() ?: return
val functionLiteral = lambda.getLambdaExpression().functionLiteral
val functionLiteral = lambda.getLambdaExpression()?.functionLiteral ?: return
val lambdaDescriptor = bindingContext[FUNCTION, functionLiteral] ?: return
val replacements = ReplacementCollection()
@@ -186,13 +186,13 @@ class ConvertScopeFunctionToParameter(counterpartName: String) : ConvertScopeFun
) {
val project = lambda.project
val factory = KtPsiFactory(project)
val functionLiteral = lambda.getLambdaExpression().functionLiteral
val functionLiteral = lambda.getLambdaExpression()?.functionLiteral
val lambdaExtensionReceiver = lambdaDescriptor.extensionReceiverParameter
val lambdaDispatchReceiver = lambdaDescriptor.dispatchReceiverParameter
var parameterName = "it"
val scopes = mutableSetOf<LexicalScope>()
if (needUniqueNameForParameter(lambda, scopes)) {
if (functionLiteral != null && needUniqueNameForParameter(lambda, scopes)) {
val parameterType = lambdaExtensionReceiver?.type ?: lambdaDispatchReceiver?.type
parameterName = findUniqueParameterName(parameterType, scopes)
replacements.createParameter = {
@@ -47,16 +47,16 @@ class SimplifyCallChainFix(val newName: String) : LocalQuickFix {
val lastArgumentPrefix = if (newName.startsWith("joinTo")) "transform = " else ""
val arguments = secondCallExpression.valueArgumentList?.arguments.orEmpty().map { it.text } +
firstCallExpression.valueArgumentList?.arguments.orEmpty().map { "$lastArgumentPrefix${it.text}" }
val lambdaArgument = firstCallExpression.lambdaArguments.singleOrNull()
val lambdaExpression = firstCallExpression.lambdaArguments.singleOrNull()?.getLambdaExpression()
val argumentsText = arguments.ifNotEmpty { joinToString(prefix = "(", postfix = ")") } ?: ""
val newQualifiedExpression = if (lambdaArgument != null) factory.createExpressionByPattern(
"$0$1$2 $3 $4",
receiverExpression ?: "",
operationSign,
newName,
argumentsText,
lambdaArgument.getLambdaExpression().text
val newQualifiedExpression = if (lambdaExpression != null) factory.createExpressionByPattern(
"$0$1$2 $3 $4",
receiverExpression ?: "",
operationSign,
newName,
argumentsText,
lambdaExpression.text
)
else factory.createExpressionByPattern(
"$0$1$2 $3",
@@ -76,7 +76,7 @@ class ConvertTryFinallyToUseCallIntention : SelfTargetingRangeIntention<KtTryExp
else -> return
}
val lambda = call.lambdaArguments.firstOrNull() ?: return
val lambdaParameter = lambda.getLambdaExpression().valueParameters.firstOrNull() ?: return
val lambdaParameter = lambda.getLambdaExpression()?.valueParameters?.firstOrNull() ?: return
editor?.selectionModel?.setSelection(lambdaParameter.startOffset, lambdaParameter.endOffset)
}
@@ -22,13 +22,12 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.core.moveInsideParentheses
import org.jetbrains.kotlin.psi.KtLambdaArgument
import org.jetbrains.kotlin.psi.psiUtil.containsInside
import org.jetbrains.kotlin.psi.unpackFunctionLiteral
class MoveLambdaInsideParenthesesIntention : SelfTargetingIntention<KtLambdaArgument>(
KtLambdaArgument::class.java, "Move lambda argument into parentheses"
), LowPriorityAction {
override fun isApplicableTo(element: KtLambdaArgument, caretOffset: Int): Boolean {
val body = element.getArgumentExpression().unpackFunctionLiteral()?.bodyExpression ?: return true
val body = element.getLambdaExpression()?.bodyExpression ?: return true
return !body.textRange.containsInside(caretOffset)
}
@@ -145,7 +145,7 @@ class ObjectLiteralToLambdaIntention : SelfTargetingRangeIntention<KtObjectLiter
val callee = replaced.getCalleeExpressionIfAny()!! as KtNameReferenceExpression
val callExpression = callee.parent as KtCallExpression
val functionLiteral = callExpression.lambdaArguments.single().getLambdaExpression()
val functionLiteral = callExpression.lambdaArguments.single().getLambdaExpression()!!
val returnLabel = callee.getReferencedNameAsName()
returnSaver.restore(functionLiteral, returnLabel)
@@ -101,7 +101,7 @@ abstract class WrapInWithReplacement : Replacement {
val call = (e as? KtSimpleNameExpression)?.getQualifiedElement() ?: return e
val replacingExpression = KtPsiFactory(e).createExpressionByPattern("with($0) { $1 }", argumentText, call)
val replace = call.replace(replacingExpression)
return (replace as KtCallExpression).lambdaArguments.first().getLambdaExpression().bodyExpression!!.statements.first()
return (replace as KtCallExpression).lambdaArguments.first().getLambdaExpression()!!.bodyExpression!!.statements.first()
}
}
@@ -328,7 +328,7 @@ internal object KotlinConverter {
}
is KtExpression -> KotlinConverter.convertExpression(element, givenParent, requiredType)
is KtLambdaArgument -> KotlinConverter.convertExpression(element.getLambdaExpression(), givenParent, requiredType)
is KtLambdaArgument -> element.getLambdaExpression()?.let { KotlinConverter.convertExpression(it, givenParent, requiredType) }
is KtLightAnnotationForSourceEntry.LightExpressionValue<*> -> {
val expression = element.originalExpression
when (expression) {