Cleanup: post-cleanup after deprecation cleanup in compiler

Replace `takeIf { !expr }` with `takeUnless { expr }`.
Cleanup redundant parethesis as in `listOf((expr))`.
Replace `listOf(expr)` with `expr.let(::listOf)` where the former caused significant indentation change.
This commit is contained in:
Ilya Gorbunov
2017-03-17 16:06:28 +03:00
parent e599688733
commit dce0da68c6
24 changed files with 43 additions and 48 deletions
@@ -63,8 +63,7 @@ object BuiltinSpecialBridgesUtil {
val specialBridgeExists = function.getSpecialBridgeSignatureIfExists(signatureByDescriptor) != null val specialBridgeExists = function.getSpecialBridgeSignatureIfExists(signatureByDescriptor) != null
val specialBridgesSignaturesInSuperClass = function.overriddenTreeAsSequence(useOriginal = true).mapNotNull { val specialBridgesSignaturesInSuperClass = function.overriddenTreeAsSequence(useOriginal = true).mapNotNull {
if (it === function) return@mapNotNull null it.takeUnless { it === function }?.getSpecialBridgeSignatureIfExists(signatureByDescriptor)
it.getSpecialBridgeSignatureIfExists(signatureByDescriptor)
} }
val isTherePossibleClashWithSpecialBridge = val isTherePossibleClashWithSpecialBridge =
specialBridgeSignature in specialBridgesSignaturesInSuperClass specialBridgeSignature in specialBridgesSignaturesInSuperClass
@@ -288,7 +288,7 @@ class CoroutineCodegen private constructor(
} }
private fun allFunctionParameters() = private fun allFunctionParameters() =
listOfNotNull(originalSuspendFunctionDescriptor.extensionReceiverParameter) + originalSuspendFunctionDescriptor.extensionReceiverParameter.let(::listOfNotNull) +
originalSuspendFunctionDescriptor.valueParameters.orEmpty() originalSuspendFunctionDescriptor.valueParameters.orEmpty()
private fun ParameterDescriptor.getFieldInfoForCoroutineLambdaParameter() = private fun ParameterDescriptor.getFieldInfoForCoroutineLambdaParameter() =
@@ -61,7 +61,7 @@ object KotlinCompilerClient {
): CompileService? { ): CompileService? {
val flagFile = System.getProperty(COMPILE_DAEMON_CLIENT_ALIVE_PATH_PROPERTY) val flagFile = System.getProperty(COMPILE_DAEMON_CLIENT_ALIVE_PATH_PROPERTY)
?.let(String::trimQuotes) ?.let(String::trimQuotes)
?.takeIf { !it.isBlank() } ?.takeUnless(String::isBlank)
?.let(::File) ?.let(::File)
?.takeIf(File::exists) ?.takeIf(File::exists)
?: makeAutodeletingFlagFile(baseDir = File(daemonOptions.runFilesPathOrDefault)) ?: makeAutodeletingFlagFile(baseDir = File(daemonOptions.runFilesPathOrDefault))
@@ -42,8 +42,8 @@ internal fun nonProjectionParametrization(samType: SimpleType): SimpleType? {
projection.projectionKind == Variance.INVARIANT -> projection projection.projectionKind == Variance.INVARIANT -> projection
projection.isStarProjection -> projection.isStarProjection ->
parameter.upperBounds.first().takeIf { parameter.upperBounds.first().takeUnless {
t -> !t.contains { it.constructor.declarationDescriptor in parametersSet } t -> t.contains { it.constructor.declarationDescriptor in parametersSet }
}?.asTypeProjection() ?: return@nonProjectionParametrization null }?.asTypeProjection() ?: return@nonProjectionParametrization null
else -> projection.type.asTypeProjection() else -> projection.type.asTypeProjection()
@@ -417,12 +417,12 @@ class QualifiedExpressionResolver {
val qualifierDescriptor = when (receiver) { val qualifierDescriptor = when (receiver) {
is PackageQualifier -> { is PackageQualifier -> {
val childPackageFQN = receiver.descriptor.fqName.child(name) val childPackageFQN = receiver.descriptor.fqName.child(name)
receiver.descriptor.module.getPackage(childPackageFQN).takeIf { !it.isEmpty() } ?: receiver.descriptor.module.getPackage(childPackageFQN).takeUnless { it.isEmpty() } ?:
receiver.descriptor.memberScope.getContributedClassifier(name, location) receiver.descriptor.memberScope.getContributedClassifier(name, location)
} }
is ClassQualifier -> receiver.staticScope.getContributedClassifier(name, location) is ClassQualifier -> receiver.staticScope.getContributedClassifier(name, location)
null -> context.scope.findClassifier(name, location) ?: null -> context.scope.findClassifier(name, location) ?:
context.scope.ownerDescriptor.module.getPackage(FqName.ROOT.child(name)).takeIf { !it.isEmpty() } context.scope.ownerDescriptor.module.getPackage(FqName.ROOT.child(name)).takeUnless { it.isEmpty() }
is ReceiverValue -> receiver.type.memberScope.memberScopeAsImportingScope().findClassifier(name, location) is ReceiverValue -> receiver.type.memberScope.memberScopeAsImportingScope().findClassifier(name, location)
else -> null else -> null
} }
@@ -55,7 +55,7 @@ class DefaultImportProvider(
defaultImports defaultImports
.filter { it.isAllUnder } .filter { it.isAllUnder }
.mapNotNull { .mapNotNull {
it.fqName.takeIf { !it.isSubpackageOf(KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME) } it.fqName.takeUnless { it.isSubpackageOf(KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME) }
} }
val nonKotlinAliasedTypeFqNames = val nonKotlinAliasedTypeFqNames =
builtinTypeAliases builtinTypeAliases
@@ -167,8 +167,8 @@ class EnumClassLowering(val context: JvmBackendContext) : ClassLoweringPass {
private fun lowerEnumEntries() { private fun lowerEnumEntries() {
irClass.declarations.transformFlat { declaration -> irClass.declarations.transformFlat { declaration ->
if (declaration is IrEnumEntry) { if (declaration is IrEnumEntry) {
listOf(createFieldForEnumEntry(declaration)) + listOfNotNull(createFieldForEnumEntry(declaration),
listOfNotNull(lowerEnumEntryClass(declaration.correspondingClass)) lowerEnumEntryClass(declaration.correspondingClass))
} }
else null else null
} }
@@ -50,12 +50,10 @@ fun KtElement.toLightElements(): List<PsiNamedElement> =
is KtSecondaryConstructor -> LightClassUtil.getLightClassMethods(this as KtFunction) is KtSecondaryConstructor -> LightClassUtil.getLightClassMethods(this as KtFunction)
is KtProperty -> LightClassUtil.getLightClassPropertyMethods(this).allDeclarations is KtProperty -> LightClassUtil.getLightClassPropertyMethods(this).allDeclarations
is KtPropertyAccessor -> listOfNotNull(LightClassUtil.getLightClassAccessorMethod(this)) is KtPropertyAccessor -> listOfNotNull(LightClassUtil.getLightClassAccessorMethod(this))
is KtParameter -> ArrayList<PsiNamedElement>().let { elements -> is KtParameter -> mutableListOf<PsiNamedElement>().also { elements ->
toPsiParameters().toCollection(elements) toPsiParameters().toCollection(elements)
LightClassUtil.getLightClassPropertyMethods(this).toCollection(elements) LightClassUtil.getLightClassPropertyMethods(this).toCollection(elements)
toAnnotationLightMethod()?.let { elements.add(it) } toAnnotationLightMethod()?.let(elements::add)
elements
} }
is KtTypeParameter -> toPsiTypeParameters() is KtTypeParameter -> toPsiTypeParameters()
is KtFile -> listOfNotNull(findFacadeClass()) is KtFile -> listOfNotNull(findFacadeClass())
@@ -334,12 +334,10 @@ class TestKotlinScriptDependenciesResolver : TestKotlinScriptDummyDependenciesRe
when (it) { when (it) {
is DependsOn -> if (it.path == "@{runtime}") listOf(kotlinPaths.runtimePath, kotlinPaths.scriptRuntimePath) else listOf(File(it.path)) is DependsOn -> if (it.path == "@{runtime}") listOf(kotlinPaths.runtimePath, kotlinPaths.scriptRuntimePath) else listOf(File(it.path))
is DependsOnTwo -> listOf(it.path1, it.path2).flatMap { is DependsOnTwo -> listOf(it.path1, it.path2).flatMap {
it.let { when {
when { it.isBlank() -> emptyList()
it.isBlank() -> emptyList() it == "@{runtime}" -> listOf(kotlinPaths.runtimePath, kotlinPaths.scriptRuntimePath)
it == "@{runtime}" -> listOf(kotlinPaths.runtimePath, kotlinPaths.scriptRuntimePath) else -> listOf(File(it))
else -> listOf(File(it))
}
} }
} }
is InvalidScriptResolverAnnotation -> throw Exception("Invalid annotation ${it.name}", it.error) is InvalidScriptResolverAnnotation -> throw Exception("Invalid annotation ${it.name}", it.error)
@@ -183,7 +183,7 @@ private class StringArgsConverter : ArgsConverter<String> {
DoubleArray::class -> args.map { it?.toDoubleOrNull() } DoubleArray::class -> args.map { it?.toDoubleOrNull() }
BooleanArray::class -> args.map { it?.toBoolean() } BooleanArray::class -> args.map { it?.toBoolean() }
else -> null else -> null
}?.toList()?.takeIf { list -> list.none { it == null } }?.toTypedArray() }?.toList()?.takeUnless { null in it }?.toTypedArray()
val parameterType = parameter.type val parameterType = parameter.type
if (parameterType.jvmErasure.java.isArray) { if (parameterType.jvmErasure.java.isArray) {
@@ -166,7 +166,7 @@ fun getFunctionTypeArgumentProjections(
arguments.addIfNotNull(receiverType?.asTypeProjection()) arguments.addIfNotNull(receiverType?.asTypeProjection())
parameterTypes.mapIndexedTo(arguments) { index, type -> parameterTypes.mapIndexedTo(arguments) { index, type ->
val name = parameterNames?.get(index)?.takeIf { !it.isSpecial } val name = parameterNames?.get(index)?.takeUnless { it.isSpecial }
val typeToUse = if (name != null) { val typeToUse = if (name != null) {
val annotationClass = builtIns.getBuiltInClassByName(KotlinBuiltIns.FQ_NAMES.parameterName.shortName()) val annotationClass = builtIns.getBuiltInClassByName(KotlinBuiltIns.FQ_NAMES.parameterName.shortName())
val nameValue = ConstantValueFactory(builtIns).createStringValue(name.asString()) val nameValue = ConstantValueFactory(builtIns).createStringValue(name.asString())
@@ -33,4 +33,4 @@ interface VariableDescriptorWithAccessors : VariableDescriptor {
} }
val VariableDescriptorWithAccessors.accessors: List<VariableAccessorDescriptor> val VariableDescriptorWithAccessors.accessors: List<VariableAccessorDescriptor>
get() = listOfNotNull(getter) + listOfNotNull(setter) get() = listOfNotNull(getter, setter)
@@ -181,7 +181,7 @@ class FuzzyType(
valueTransform = { valueTransform = {
val typeProjection = TypeProjectionImpl(Variance.INVARIANT, it.defaultType) val typeProjection = TypeProjectionImpl(Variance.INVARIANT, it.defaultType)
val substitutedProjection = substitutorToKeepCapturedTypes.substitute(typeProjection) val substitutedProjection = substitutorToKeepCapturedTypes.substitute(typeProjection)
substitutedProjection?.takeIf { !ErrorUtils.containsUninferredParameter(it.type) } ?: typeProjection substitutedProjection?.takeUnless { ErrorUtils.containsUninferredParameter(it.type) } ?: typeProjection
}) })
return TypeConstructorSubstitution.createByConstructorsMap(substitutionMap, approximateCapturedTypes = true).buildSubstitutor() return TypeConstructorSubstitution.createByConstructorsMap(substitutionMap, approximateCapturedTypes = true).buildSubstitutor()
} }
@@ -51,11 +51,11 @@ class StaticMembersCompletion(
return object : AbstractLookupElementFactory { return object : AbstractLookupElementFactory {
override fun createStandardLookupElementsForDescriptor(descriptor: DeclarationDescriptor, useReceiverTypes: Boolean): Collection<LookupElement> { override fun createStandardLookupElementsForDescriptor(descriptor: DeclarationDescriptor, useReceiverTypes: Boolean): Collection<LookupElement> {
if (!useReceiverTypes) return emptyList() if (!useReceiverTypes) return emptyList()
return listOfNotNull(lookupElementFactory.createLookupElement(descriptor, useReceiverTypes = false) return lookupElementFactory.createLookupElement(descriptor, useReceiverTypes = false)
.decorateAsStaticMember(descriptor, classNameAsLookupString = false) .decorateAsStaticMember(descriptor, classNameAsLookupString = false)
?.assignPriority(itemPriority) ?.assignPriority(itemPriority)
?.suppressAutoInsertion() ?.suppressAutoInsertion()
) .let(::listOfNotNull)
} }
override fun createLookupElement(descriptor: DeclarationDescriptor, useReceiverTypes: Boolean, override fun createLookupElement(descriptor: DeclarationDescriptor, useReceiverTypes: Boolean,
@@ -588,7 +588,7 @@ class ExpectedInfos(
val loopVar = forExpression.loopParameter val loopVar = forExpression.loopParameter
val loopVarType = if (loopVar != null && loopVar.typeReference != null) val loopVarType = if (loopVar != null && loopVar.typeReference != null)
(bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, loopVar] as VariableDescriptor).type.takeIf { !it.isError } (bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, loopVar] as VariableDescriptor).type.takeUnless { it.isError }
else else
null null
@@ -478,7 +478,7 @@ class KotlinIndicesHelper(
val translatedDeclaration = declarationTranslator(this) ?: return emptyList() val translatedDeclaration = declarationTranslator(this) ?: return emptyList()
if (!psiFilter(translatedDeclaration)) return emptyList() if (!psiFilter(translatedDeclaration)) return emptyList()
return listOfNotNull((resolutionFacade.resolveToDescriptor(translatedDeclaration))) return listOfNotNull(resolutionFacade.resolveToDescriptor(translatedDeclaration))
} }
} }
} }
@@ -322,7 +322,7 @@ class KotlinCodeFragmentFactory: CodeFragmentFactory() {
return containingFile return containingFile
} }
private fun KtElement?.check(): Boolean = this != null && this.takeIf { KotlinEditorTextProvider.isAcceptedAsCodeFragmentContext(it) } != null private fun KtElement?.check(): Boolean = this != null && KotlinEditorTextProvider.isAcceptedAsCodeFragmentContext(this)
//internal for tests //internal for tests
fun createCodeFragmentForLabeledObjects(project: Project, markupMap: Map<*, ValueMarkup>): Pair<String, Map<String, Value>> { fun createCodeFragmentForLabeledObjects(project: Project, markupMap: Map<*, ValueMarkup>): Pair<String, Map<String, Value>> {
@@ -397,7 +397,7 @@ class KotlinCodeFragmentFactory: CodeFragmentFactory() {
val sb = StringBuilder() val sb = StringBuilder()
javaFile?.packageName?.takeIf { !it.isBlank() }?.let { javaFile?.packageName?.takeUnless { it.isBlank() }?.let {
sb.append("package ").append(it.quoteIfNeeded()).append("\n") sb.append("package ").append(it.quoteIfNeeded()).append("\n")
} }
@@ -75,7 +75,7 @@ abstract class ChangeCallableReturnTypeFix(
val name = element.name val name = element.name
if (name != null) { if (name != null) {
val container = element.resolveToDescriptor().containingDeclaration as? ClassDescriptor val container = element.resolveToDescriptor().containingDeclaration as? ClassDescriptor
val containerName = container?.name?.takeIf { !it.isSpecial }?.asString() val containerName = container?.name?.takeUnless { it.isSpecial }?.asString()
val fullName = if (containerName != null) "'$containerName.$name'" else "'$name'" val fullName = if (containerName != null) "'$containerName.$name'" else "'$name'"
if (element is KtParameter) { if (element is KtParameter) {
return "property $fullName" return "property $fullName"
@@ -50,7 +50,7 @@ open class ChangeVariableTypeFix(element: KtVariableDeclaration, type: KotlinTyp
val name = element.name val name = element.name
if (name != null) { if (name != null) {
val container = element.resolveToDescriptor().containingDeclaration as? ClassDescriptor val container = element.resolveToDescriptor().containingDeclaration as? ClassDescriptor
val containerName = container?.name?.takeIf { !it.isSpecial }?.asString() val containerName = container?.name?.takeUnless { it.isSpecial }?.asString()
return if (containerName != null) "'$containerName.$name'" else "'$name'" return if (containerName != null) "'$containerName.$name'" else "'$name'"
} }
else { else {
@@ -488,8 +488,8 @@ internal class ImportForMismatchingArgumentsFix(
return callExpression.valueArguments + return callExpression.valueArguments +
callExpression.valueArguments.mapNotNull { it.getArgumentExpression() } + callExpression.valueArguments.mapNotNull { it.getArgumentExpression() } +
callExpression.valueArguments.mapNotNull { it.getArgumentName()?.referenceExpression } + callExpression.valueArguments.mapNotNull { it.getArgumentName()?.referenceExpression } +
listOfNotNull(callExpression.valueArgumentList) + listOfNotNull(callExpression.valueArgumentList,
listOfNotNull(callExpression.referenceExpression()) callExpression.referenceExpression())
} }
override fun fillCandidates( override fun fillCandidates(
@@ -37,11 +37,11 @@ abstract class KotlinSingleIntentionActionFactoryWithDelegate<E : KtElement, D :
diagnostic: Diagnostic, diagnostic: Diagnostic,
quickFixDataFactory: () -> D? quickFixDataFactory: () -> D?
): List<QuickFixWithDelegateFactory> { ): List<QuickFixWithDelegateFactory> {
return listOf(QuickFixWithDelegateFactory(actionPriority) factory@ { return QuickFixWithDelegateFactory(actionPriority) factory@ {
val originalElement = originalElementPointer.element ?: return@factory null val originalElement = originalElementPointer.element ?: return@factory null
val data = quickFixDataFactory() ?: return@factory null val data = quickFixDataFactory() ?: return@factory null
createFix(originalElement, data) createFix(originalElement, data)
}) }.let(::listOf)
} }
} }
@@ -45,7 +45,7 @@ object CreateClassFromCallWithConstructorCalleeActionFactory : CreateClassFromUs
} }
override fun getPossibleClassKinds(element: KtCallElement, diagnostic: Diagnostic): List<ClassKind> { override fun getPossibleClassKinds(element: KtCallElement, diagnostic: Diagnostic): List<ClassKind> {
return listOf((if (element is KtAnnotationEntry) ClassKind.ANNOTATION_CLASS else ClassKind.PLAIN_CLASS)) return listOf(if (element is KtAnnotationEntry) ClassKind.ANNOTATION_CLASS else ClassKind.PLAIN_CLASS)
} }
override fun extractFixData(element: KtCallElement, diagnostic: Diagnostic): ClassInfo? { override fun extractFixData(element: KtCallElement, diagnostic: Diagnostic): ClassInfo? {
@@ -76,10 +76,10 @@ object CreateTypeParameterUnmatchedTypeArgumentActionFactory : KotlinIntentionAc
diagnostic: Diagnostic, diagnostic: Diagnostic,
quickFixDataFactory: () -> CreateTypeParameterData? quickFixDataFactory: () -> CreateTypeParameterData?
): List<QuickFixWithDelegateFactory> { ): List<QuickFixWithDelegateFactory> {
return listOf(QuickFixWithDelegateFactory factory@ { return QuickFixWithDelegateFactory factory@ {
val originalElement = originalElementPointer.element ?: return@factory null val originalElement = originalElementPointer.element ?: return@factory null
val data = quickFixDataFactory() ?: return@factory null val data = quickFixDataFactory() ?: return@factory null
CreateTypeParameterFromUsageFix(originalElement, data, false) CreateTypeParameterFromUsageFix(originalElement, data, false)
}) }.let(::listOf)
} }
} }
@@ -561,11 +561,11 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
componentFunctions.map { suggestNamesForComponent(it, project, collectingValidator) } componentFunctions.map { suggestNamesForComponent(it, project, collectingValidator) }
} }
else { else {
listOf(KotlinNameSuggester.suggestNamesByExpressionAndType(expression, KotlinNameSuggester.suggestNamesByExpressionAndType(expression,
substringInfo?.type, substringInfo?.type,
bindingContext, bindingContext,
validator, validator,
"value")) "value").let(::listOf)
} }
val introduceVariableContext = IntroduceVariableContext( val introduceVariableContext = IntroduceVariableContext(