COMMA_IN_WHEN_CONDITION_WITHOUT_ARGUMENT is an ERROR now.

Migrated code.
Updated test data in IDE tests.
Dropped whenWithRangeTestsAndMultiConditions.kt:
"Introduce subject" is not applicable to 'when' with ||-ed conditions.
This commit is contained in:
Dmitry Petrov
2015-12-10 12:12:44 +03:00
parent 199827635f
commit 0fe74a8b43
26 changed files with 36 additions and 58 deletions
@@ -706,7 +706,7 @@ public interface Errors {
DiagnosticFactory0<KtWhenEntry> ELSE_MISPLACED_IN_WHEN = DiagnosticFactory0.create(ERROR, ELSE_ENTRY);
DiagnosticFactory0<KtWhenExpression> NO_ELSE_IN_WHEN = DiagnosticFactory0.create(ERROR, WHEN_EXPRESSION);
DiagnosticFactory0<KtWhenExpression> NON_EXHAUSTIVE_WHEN = DiagnosticFactory0.create(WARNING, WHEN_EXPRESSION);
DiagnosticFactory0<PsiElement> COMMA_IN_WHEN_CONDITION_WITHOUT_ARGUMENT = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<PsiElement> COMMA_IN_WHEN_CONDITION_WITHOUT_ARGUMENT = DiagnosticFactory0.create(ERROR);
// Type mismatch
@@ -31,7 +31,7 @@ public abstract class ReflectJavaType : JavaType {
fun create(type: Type): ReflectJavaType {
return when {
type is Class<*> && type.isPrimitive() -> ReflectJavaPrimitiveType(type)
type is GenericArrayType, type is Class<*> && type.isArray() -> ReflectJavaArrayType(type)
type is GenericArrayType || type is Class<*> && type.isArray() -> ReflectJavaArrayType(type)
type is WildcardType -> ReflectJavaWildcardType(type)
else -> ReflectJavaClassifierType(type)
}
@@ -34,7 +34,7 @@ public val Class<*>.classId: ClassId
get() = when {
isPrimitive() -> throw IllegalArgumentException("Can't compute ClassId for primitive type: $this")
isArray() -> throw IllegalArgumentException("Can't compute ClassId for array type: $this")
getEnclosingMethod() != null, getEnclosingConstructor() != null, getSimpleName().isEmpty() -> {
getEnclosingMethod() != null || getEnclosingConstructor() != null || getSimpleName().isEmpty() -> {
val fqName = FqName(getName())
ClassId(fqName.parent(), FqName.topLevel(fqName.shortName()), /* local = */ true)
}
@@ -387,7 +387,7 @@ class GenerateProtoBufCompare {
private fun Printer.printlnIfWithComparison(field: Descriptors.FieldDescriptor, expr: String, statement: String = "return false") {
val line = when {
field.options.getExtension(DebugExtOptionsProtoBuf.stringIdInTable),
field.options.getExtension(DebugExtOptionsProtoBuf.stringIdInTable) ||
field.options.getExtension(DebugExtOptionsProtoBuf.nameIdInTable) ->
"if (!$CHECK_STRING_EQAULS_NAME(old.$expr, new.$expr)) $statement"
field.options.getExtension(DebugExtOptionsProtoBuf.fqNameIdInTable) ->
@@ -408,7 +408,7 @@ class GenerateProtoBufCompare {
private fun fieldToHashCode(field: Descriptors.FieldDescriptor, expr: String): String =
when {
field.options.getExtension(DebugExtOptionsProtoBuf.stringIdInTable),
field.options.getExtension(DebugExtOptionsProtoBuf.stringIdInTable) ||
field.options.getExtension(DebugExtOptionsProtoBuf.nameIdInTable) ->
"stringIndexes($expr)"
field.options.getExtension(DebugExtOptionsProtoBuf.fqNameIdInTable) ->
@@ -170,8 +170,8 @@ public object UsageTypeUtils {
} ->
if (descriptor is ConstructorDescriptor) CLASS_NEW_OPERATOR else FUNCTION_CALL
refExpr.getParentOfTypeAndBranch<KtBinaryExpression>(){ getOperationReference() } != null,
refExpr.getParentOfTypeAndBranch<KtUnaryExpression>(){ getOperationReference() } != null,
refExpr.getParentOfTypeAndBranch<KtBinaryExpression>(){ getOperationReference() } != null ||
refExpr.getParentOfTypeAndBranch<KtUnaryExpression>(){ getOperationReference() } != null ||
refExpr.getParentOfTypeAndBranch<KtWhenConditionInRange>(){ getOperationReference() } != null ->
FUNCTION_CALL
@@ -195,7 +195,7 @@ public object UsageTypeUtils {
return when (descriptor) {
is ClassifierDescriptor -> when {
// Treat object accesses as variables to simulate the old behaviour (when variables were created for objects)
DescriptorUtils.isNonCompanionObject(descriptor), DescriptorUtils.isEnumEntry(descriptor) -> getVariableUsageType()
DescriptorUtils.isNonCompanionObject(descriptor) || DescriptorUtils.isEnumEntry(descriptor) -> getVariableUsageType()
DescriptorUtils.isCompanionObject(descriptor) -> COMPANION_OBJECT_ACCESS
else -> getClassUsageType()
}
@@ -83,7 +83,7 @@ public class KotlinFindClassUsagesHandler(
public override fun execute(element: PsiClass): Boolean {
val isInterface = element.isInterface()
return when {
isInterface && options.isDerivedInterfaces, !isInterface && options.isDerivedClasses ->
isInterface && options.isDerivedInterfaces || !isInterface && options.isDerivedClasses ->
KotlinFindUsagesHandler.processUsage(processor, element.getNavigationElement())
else -> true
}
@@ -56,7 +56,7 @@ internal fun getPsiMethod(element: PsiElement?): PsiMethod? {
return when {
element == null -> null
element is PsiMethod -> element
parent is KtNamedFunction, parent is KtSecondaryConstructor -> LightClassUtil.getLightClassMethod(parent as KtFunction)
parent is KtNamedFunction || parent is KtSecondaryConstructor -> LightClassUtil.getLightClassMethod(parent as KtFunction)
else -> null
}
}
@@ -529,7 +529,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
val neighborType = neighbor?.getNode()?.getElementType()
val lineBreaksNeeded = when {
neighborType == KtTokens.LBRACE, neighborType == KtTokens.RBRACE -> 1
neighborType == KtTokens.LBRACE || neighborType == KtTokens.RBRACE -> 1
neighbor is KtDeclaration && (neighbor !is KtProperty || decl !is KtProperty) -> 2
else -> 1
}
@@ -86,7 +86,7 @@ object CreateParameterByRefActionFactory : CreateParameterFromUsageFactory<KtSim
.firstOrNull()
?.let {
when {
(it is KtNamedFunction || it is KtSecondaryConstructor) && varExpected,
(it is KtNamedFunction || it is KtSecondaryConstructor) && varExpected ||
it is KtPropertyAccessor -> chooseContainingClass(it)
it is KtAnonymousInitializer -> it.parent?.parent as? KtClass
it is KtSuperTypeListEntry -> {
@@ -153,7 +153,7 @@ class KotlinFunctionCallUsage(
private fun needSeparateVariable(element: PsiElement): Boolean {
return when {
element is KtConstantExpression, element is KtThisExpression, element is KtSimpleNameExpression -> false
element is KtConstantExpression || element is KtThisExpression || element is KtSimpleNameExpression -> false
element is KtBinaryExpression && OperatorConventions.ASSIGNMENT_OPERATIONS.contains(element.operationToken) -> true
element is KtUnaryExpression && OperatorConventions.INCREMENT_OPERATIONS.contains(element.operationToken) -> true
element is KtCallExpression -> element.getResolvedCall(context)?.resultingDescriptor is ConstructorDescriptor
@@ -333,7 +333,7 @@ val ControlFlow.possibleReturnTypes: List<KotlinType>
return when {
!returnType.isNullabilityFlexible() ->
listOf(returnType)
returnType.isAnnotatedNotNull(), returnType.isAnnotatedNullable() ->
returnType.isAnnotatedNotNull() || returnType.isAnnotatedNullable() ->
listOf(approximateFlexibleTypes(returnType))
else ->
returnType.getCapability(javaClass<Flexibility>()).let { listOf(it!!.upperBound, it.lowerBound) }
@@ -87,7 +87,7 @@ internal fun ExtractionData.inferParametersInfo(
val resolvedCall = refInfo.resolveResult.resolvedCall
val extensionReceiver = resolvedCall?.extensionReceiver
val receiverToExtract = (when {
extensionReceiver == ReceiverValue.NO_RECEIVER,
extensionReceiver == ReceiverValue.NO_RECEIVER ||
isSynthesizedInvoke(refInfo.resolveResult.descriptor) -> resolvedCall?.dispatchReceiver
else -> extensionReceiver
} as? ReceiverValue) ?: ReceiverValue.NO_RECEIVER
@@ -368,7 +368,7 @@ object KotlinIntroduceVariableHandler : KotlinIntroduceHandlerBase() {
for ((place, parent) in parentsWithSelf.zip(parents)) {
when {
parent is KtContainerNode && place !is KtBlockExpression && !parent.isBadContainerNode(place) -> result = parent
parent is KtClassBody, parent is KtFile -> return result
parent is KtClassBody || parent is KtFile -> return result
parent is KtBlockExpression -> result = parent
parent is KtWhenEntry && place !is KtBlockExpression -> result = parent
parent is KtDeclarationWithBody && parent.bodyExpression == place && place !is KtBlockExpression -> result = parent
@@ -436,8 +436,8 @@ public fun PsiElement.canRefactor(): Boolean {
return when {
this is PsiPackage ->
getDirectories().any { it.canRefactor() }
this is KtElement,
this is PsiMember && getLanguage() == JavaLanguage.INSTANCE,
this is KtElement ||
this is PsiMember && getLanguage() == JavaLanguage.INSTANCE ||
this is PsiDirectory ->
isWritable() && ProjectRootsUtil.isInProjectSource(this)
else ->
@@ -45,7 +45,7 @@ public class KotlinMemberInfoStorage(
descriptor1 is FunctionDescriptor && descriptor is FunctionDescriptor -> {
!OverloadUtil.isOverloadable(descriptor1, descriptor)
}
descriptor1 is PropertyDescriptor && descriptor is PropertyDescriptor,
descriptor1 is PropertyDescriptor && descriptor is PropertyDescriptor ||
descriptor1 is ClassDescriptor && descriptor is ClassDescriptor -> true
else -> false
}
@@ -28,7 +28,7 @@ public class KotlinMoveFilesOrDirectoriesHandler : MoveFilesOrDirectoriesHandler
private fun adjustElements(elements: Array<out PsiElement>): Array<PsiElement>? {
return elements.map {
when {
it is PsiFile, it is PsiDirectory -> it
it is PsiFile || it is PsiDirectory -> it
it is PsiClass && it.getContainingClass() == null -> it.getContainingFile()
it is KtClassOrObject && it.getParent() is KtFile -> it.getParent()
else -> return null
@@ -114,9 +114,9 @@ public fun KtElement.getInternalReferencesToUpdateOnPackageNameChange(packageNam
val oldPackageName = packageNameInfo.oldPackageName
val newPackageName = packageNameInfo.newPackageName
return when {
isExtension,
packageName == oldPackageName,
packageName?.asString() == newPackageName.asString(),
isExtension ||
packageName == oldPackageName ||
packageName?.asString() == newPackageName.asString() ||
isImported(descriptor) -> {
if (isAncestor(declaration, false)) {
if (descriptor.importableFqName == null) return null
@@ -176,11 +176,11 @@ public class KotlinPsiUnifier(
return when {
op1 == op2 ->
true
op1 == KtTokens.NOT_IN, op2 == KtTokens.NOT_IN ->
op1 == KtTokens.NOT_IN || op2 == KtTokens.NOT_IN ->
false
op1 == KtTokens.EXCLEQ, op2 == KtTokens.EXCLEQ ->
op1 == KtTokens.EXCLEQ || op2 == KtTokens.EXCLEQ ->
false
op1 in OperatorConventions.COMPARISON_OPERATIONS, op2 in OperatorConventions.COMPARISON_OPERATIONS ->
op1 in OperatorConventions.COMPARISON_OPERATIONS || op2 in OperatorConventions.COMPARISON_OPERATIONS ->
false
else ->
true
@@ -662,7 +662,7 @@ public class KotlinPsiUnifier(
private fun matchResolvedInfo(e1: PsiElement, e2: PsiElement): Status? {
return when {
e1 !is KtElement, e2 !is KtElement ->
e1 !is KtElement || e2 !is KtElement ->
null
e1 is KtDestructuringDeclaration && e2 is KtDestructuringDeclaration ->
@@ -1,6 +1,6 @@
fun test(n: Int): String {
return <caret>when {
n < 0, n > 1000 -> "unknown"
n < 0 || n > 1000 -> "unknown"
n <= 10 -> "small"
n <= 100 -> "average"
else -> "big"
@@ -3,7 +3,7 @@ fun test(n: Int): String {
return <caret>when {
n in 0..10 -> "small"
n >= 10 && n <= 100 -> "average"
n < 0, n > 1000 -> "unknown"
n < 0 || n > 1000 -> "unknown"
else -> "big"
}
}
@@ -1,8 +0,0 @@
fun test(n: Int): String {
return <caret>when {
n in 0..5, n in 5..10 -> "small"
n in 10..50, n in 50..100 -> "average"
n in 100..500, n in 500..1000 -> "big"
else -> "unknown"
}
}
@@ -1,8 +0,0 @@
fun test(n: Int): String {
return <caret>when (n) {
in 0..5, in 5..10 -> "small"
in 10..50, in 50..100 -> "average"
in 100..500, in 500..1000 -> "big"
else -> "unknown"
}
}
@@ -2062,12 +2062,6 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("whenWithRangeTestsAndMultiConditions.kt")
public void testWhenWithRangeTestsAndMultiConditions() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/when/introduceSubject/whenWithRangeTestsAndMultiConditions.kt");
doTest(fileName);
}
@TestMetadata("whenWithSubject.kt")
public void testWhenWithSubject() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/when/introduceSubject/whenWithSubject.kt");
@@ -102,8 +102,8 @@ public abstract class AbstractProtoComparisonTest : UsefulTestCase() {
)
val diff = when {
newClassHeader.isCompatibleClassKind(),
newClassHeader.isCompatibleFileFacadeKind(),
newClassHeader.isCompatibleClassKind() ||
newClassHeader.isCompatibleFileFacadeKind() ||
newClassHeader.isCompatibleMultifileClassPartKind() ->
difference(oldProto, newProto)
else -> {
@@ -10,13 +10,13 @@ fun id(s: String, value: Boolean): Boolean {
fun box(): String {
when {
id("A", true), id("B", true) -> 10
id("A", true) || id("B", true) -> 10
}
assertEquals("A", global)
global = ""
when {
id("A", false), id("B", true), id("C", true) -> 10
id("A", false) || id("B", true) || id("C", true) -> 10
}
assertEquals("AB", global)
@@ -39,14 +39,14 @@ fun box(): String {
global = ""
b = true
when {
b, try { global += "A"; !b } finally {} -> 10
b || try { global += "A"; !b } finally {} -> 10
}
assertEquals("", global)
global = ""
b = false
when {
b, try { global += "A"; !b } finally {} -> 10
b || try { global += "A"; !b } finally {} -> 10
}
assertEquals("A", global)
@@ -56,7 +56,7 @@ tailrec fun allSuperTypesImpl(roots: List<GenerateTraitOrClass>, all: Map<String
fun standardTypes() = typeMapper.values.map {it.dropNullable()}.toSet()
fun Type.dynamicIfUnknownType(allTypes: Set<String>, standardTypes: Set<Type> = standardTypes()): Type = when {
this is DynamicType, this is UnitType -> this
this is DynamicType || this is UnitType -> this
this is SimpleType && this.type in allTypes -> this
this.dropNullable() in standardTypes -> this