Compare commits

...

2 Commits

Author SHA1 Message Date
Ilya Gorbunov
351e1cc05f Replace some usages of check to takeUnless 2017-02-10 18:04:56 +03:00
Ilya Gorbunov
cf5d6916a9 Introduce 'takeUnless' function which is like 'takeIf' but with the predicate inverted.
#KT-7858
2017-02-10 18:03:22 +03:00
11 changed files with 20 additions and 11 deletions

View File

@@ -57,9 +57,9 @@ object KotlinCompilerClient {
): CompileService? {
val flagFile = System.getProperty(COMPILE_DAEMON_CLIENT_ALIVE_PATH_PROPERTY)
?.let(String::trimQuotes)
?.check { !it.isBlank() }
?.takeUnless(String::isBlank)
?.let(::File)
?.check(File::exists)
?.takeIf(File::exists)
?: makeAutodeletingFlagFile(baseDir = File(daemonOptions.runFilesPathOrDefault))
return connectToCompileService(compilerId, flagFile, daemonJVMOptions, daemonOptions, reportingTargets, autostart)
}

View File

@@ -412,12 +412,12 @@ class QualifiedExpressionResolver {
val qualifierDescriptor = when (receiver) {
is PackageQualifier -> {
val childPackageFQN = receiver.descriptor.fqName.child(name)
receiver.descriptor.module.getPackage(childPackageFQN).check { !it.isEmpty() } ?:
receiver.descriptor.module.getPackage(childPackageFQN).takeUnless { it.isEmpty() } ?:
receiver.descriptor.memberScope.getContributedClassifier(name, location)
}
is ClassQualifier -> receiver.staticScope.getContributedClassifier(name, location)
null -> context.scope.findClassifier(name, location) ?:
context.scope.ownerDescriptor.module.getPackage(FqName.ROOT.child(name)).check { !it.isEmpty() }
context.scope.ownerDescriptor.module.getPackage(FqName.ROOT.child(name)).takeUnless { it.isEmpty() }
is ReceiverValue -> receiver.type.memberScope.memberScopeAsImportingScope().findClassifier(name, location)
else -> null
}

View File

@@ -56,7 +56,7 @@ class DefaultImportProvider(
defaultImports
.filter { it.isAllUnder }
.mapNotNull {
it.fqnPart().check { !it.isSubpackageOf(KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME) }
it.fqnPart().takeUnless { it.isSubpackageOf(KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME) }
}
val nonKotlinAliasedTypeFqNames =
builtinTypeAliases

View File

@@ -167,7 +167,7 @@ fun getFunctionTypeArgumentProjections(
arguments.addIfNotNull(receiverType?.asTypeProjection())
parameterTypes.mapIndexedTo(arguments) { index, type ->
val name = parameterNames?.get(index)?.check { !it.isSpecial }
val name = parameterNames?.get(index)?.takeUnless { it.isSpecial }
val typeToUse = if (name != null) {
val annotationClass = builtIns.getBuiltInClassByName(KotlinBuiltIns.FQ_NAMES.parameterName.shortName())
val nameValue = ConstantValueFactory(builtIns).createStringValue(name.asString())

View File

@@ -182,7 +182,7 @@ class FuzzyType(
valueTransform = {
val typeProjection = TypeProjectionImpl(Variance.INVARIANT, it.defaultType)
val substitutedProjection = substitutorToKeepCapturedTypes.substitute(typeProjection)
substitutedProjection?.check { !ErrorUtils.containsUninferredParameter(it.type) } ?: typeProjection
substitutedProjection?.takeUnless { ErrorUtils.containsUninferredParameter(it.type) } ?: typeProjection
})
return TypeConstructorSubstitution.createByConstructorsMap(substitutionMap, approximateCapturedTypes = true).buildSubstitutor()
}

View File

@@ -23,4 +23,6 @@ class C {
// ORDER: nullableX
// ORDER: takeIf
// ORDER: takeIf
// ORDER: takeUnless
// ORDER: takeUnless
// ORDER: C

View File

@@ -582,7 +582,7 @@ class ExpectedInfos(
val loopVar = forExpression.loopParameter
val loopVarType = if (loopVar != null && loopVar.typeReference != null)
(bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, loopVar] as VariableDescriptor).type.check { !it.isError }
(bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, loopVar] as VariableDescriptor).type.takeUnless { it.isError }
else
null

View File

@@ -398,7 +398,7 @@ class KotlinCodeFragmentFactory: CodeFragmentFactory() {
val sb = StringBuilder()
javaFile?.packageName?.check { !it.isBlank() }?.let {
javaFile?.packageName?.takeUnless { it.isBlank() }?.let {
sb.append("package ").append(it.quoteIfNeeded()).append("\n")
}

View File

@@ -76,7 +76,7 @@ abstract class ChangeCallableReturnTypeFix(
val name = element.name
if (name != null) {
val container = element.resolveToDescriptor().containingDeclaration as? ClassDescriptor
val containerName = container?.name?.check { !it.isSpecial }?.asString()
val containerName = container?.name?.takeUnless { it.isSpecial }?.asString()
val fullName = if (containerName != null) "'$containerName.$name'" else "'$name'"
if (element is KtParameter) {
return "property $fullName"

View File

@@ -51,7 +51,7 @@ open class ChangeVariableTypeFix(element: KtVariableDeclaration, type: KotlinTyp
val name = element.name
if (name != null) {
val container = element.resolveToDescriptor().containingDeclaration as? ClassDescriptor
val containerName = container?.name?.check { !it.isSpecial }?.asString()
val containerName = container?.name?.takeUnless { it.isSpecial }?.asString()
return if (containerName != null) "'$containerName.$name'" else "'$name'"
}
else {

View File

@@ -68,6 +68,13 @@ public inline fun <T, R> T.let(block: (T) -> R): R = block(this)
@SinceKotlin("1.1")
public inline fun <T> T.takeIf(predicate: (T) -> Boolean): T? = if (predicate(this)) this else null
/**
* Returns `this` value if it _does not_ satisfy the given [predicate] or `null`, if it does.
*/
@kotlin.internal.InlineOnly
@SinceKotlin("1.1")
public inline fun <T> T.takeUnless(predicate: (T) -> Boolean): T? = if (!predicate(this)) this else null
/**
* Executes the given function [action] specified number of [times].
*