From 8edd33ee929a3cf92d67303c9f5acc520bc62109 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 27 Oct 2016 15:04:47 +0300 Subject: [PATCH] If then to elvis: applied in IDEA, front-end & J2K modules --- .../kotlin/cfg/pseudocode/PseudocodeImpl.kt | 7 ++----- .../org/jetbrains/kotlin/psi/DebugTextUtil.kt | 2 +- .../jetbrains/kotlin/psi/createByPattern.kt | 2 +- .../resolve/AnnotationUseSiteTargetChecker.kt | 20 +++++++++---------- .../kotlin/resolve/OperatorModifierChecker.kt | 5 +---- .../kotlin/resolve/TypeAliasExpander.kt | 4 +--- .../checkers/CapturingInClosureChecker.kt | 2 +- .../ProtectedConstructorCallChecker.kt | 6 +----- .../calls/inference/ConstraintPosition.kt | 2 +- .../smartcasts/DelegatingDataFlowInfo.kt | 7 +------ ...inScriptDefinitionFromAnnotatedTemplate.kt | 4 ++-- .../internal/CheckPartialBodyResolveAction.kt | 2 +- .../debugger/KotlinSourcePositionProvider.kt | 2 +- .../debugger/evaluate/KotlinDebuggerCaches.kt | 2 +- .../extractFunctionForDebuggerUtil.kt | 5 +---- .../ConvertAssertToIfWithThrowIntention.kt | 11 +++------- .../ConvertLambdaToReferenceIntention.kt | 2 +- .../ConvertToForEachFunctionCallIntention.kt | 2 +- .../ConvertMemberToExtensionIntention.kt | 2 +- .../idea/projectView/projectViewProviders.kt | 2 +- .../InitializePropertyQuickFixFactory.kt | 9 +++------ .../quickfix/MapPlatformClassToKotlinFix.kt | 2 +- .../DeprecatedSymbolUsageFixBase.kt | 11 +++++----- .../changeSignature/KotlinChangeSignature.kt | 3 +-- .../KotlinChangeSignatureProcessor.kt | 4 ++-- .../extractClass/ExtractSuperRefactoring.kt | 2 +- .../KotlinIntroduceParameterHandler.kt | 9 ++------- .../ui/AbstractParameterTablePanel.kt | 16 ++------------- .../KotlinClassMembersRefactoringSupport.kt | 2 +- .../refactoring/pullUp/KotlinPullUpHelper.kt | 2 +- .../rename/RenameKotlinFunctionProcessor.kt | 2 +- .../KotlinDirectInheritorsSearcher.kt | 11 ++-------- .../kotlin/j2k/ExpressionConverter.kt | 2 +- .../org/jetbrains/kotlin/j2k/SpecialMethod.kt | 2 +- .../kotlin/j2k/StatementConverter.kt | 5 +---- .../jetbrains/kotlin/j2k/SwitchConverter.kt | 2 +- .../FieldToPropertyProcessing.kt | 6 ++---- 37 files changed, 61 insertions(+), 120 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/cfg/pseudocode/PseudocodeImpl.kt b/compiler/frontend/src/org/jetbrains/kotlin/cfg/pseudocode/PseudocodeImpl.kt index 303eb1eaf1c..d8eab623ffe 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/cfg/pseudocode/PseudocodeImpl.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/cfg/pseudocode/PseudocodeImpl.kt @@ -369,11 +369,8 @@ class PseudocodeImpl(override val correspondingElement: KtElement) : Pseudocode startLabel: Label?, finishLabel: Label?, labelCountArg: Int): Int { var labelCount = labelCountArg - val startIndex = if (startLabel != null) startLabel.targetInstructionIndex else 0 - val finishIndex = if (finishLabel != null) - finishLabel.targetInstructionIndex - else - originalPseudocode.mutableInstructionList.size + val startIndex = startLabel?.targetInstructionIndex ?: 0 + val finishIndex = finishLabel?.targetInstructionIndex ?: originalPseudocode.mutableInstructionList.size val originalToCopy = Maps.newLinkedHashMap() val originalLabelsForInstruction = HashMultimap.create() diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/DebugTextUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/psi/DebugTextUtil.kt index 231c2ba79c7..d1f29a2c2b2 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/DebugTextUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/DebugTextUtil.kt @@ -196,7 +196,7 @@ private object DebugTextBuildingVisitor : KtVisitor() { override fun visitPropertyAccessor(accessor: KtPropertyAccessor, data: Unit?): String? { val containingProperty = KtStubbedPsiUtil.getContainingDeclaration(accessor, KtProperty::class.java) val what = (if (accessor.isGetter()) "getter" else "setter") - return what + " for " + (if (containingProperty != null) containingProperty.getDebugText() else "...") + return what + " for " + (containingProperty?.getDebugText() ?: "...") } override fun visitClass(klass: KtClass, data: Unit?): String? { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/createByPattern.kt b/compiler/frontend/src/org/jetbrains/kotlin/psi/createByPattern.kt index 21a6853f7a6..9b1c95bbe59 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/createByPattern.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/createByPattern.kt @@ -225,7 +225,7 @@ private fun processPattern(pattern: String, args: List): PatternData { val arg: Any? = if (n < args.size) args[n] else null /* report wrong number of arguments later */ val placeholderText = if (charOrNull(i) != ':' || charOrNull(i + 1) != '\'') { - if (arg is String) arg else "xyz" + arg as? String ?: "xyz" } else { check(arg !is String, "do not specify placeholder text for $$n - plain text argument passed") diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUseSiteTargetChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUseSiteTargetChecker.kt index 7c99e7e7547..4eb29221059 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUseSiteTargetChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUseSiteTargetChecker.kt @@ -109,11 +109,11 @@ object AnnotationUseSiteTargetChecker { private fun BindingTrace.checkIfMutableProperty(annotated: KtAnnotated, annotation: KtAnnotationEntry) { if (!checkIfProperty(annotated, annotation)) return - val isMutable = if (annotated is KtProperty) - annotated.isVar - else if (annotated is KtParameter) - annotated.isMutable - else false + val isMutable = when (annotated) { + is KtProperty -> annotated.isVar + is KtParameter -> annotated.isMutable + else -> false + } if (!isMutable) { report(INAPPLICABLE_TARGET_PROPERTY_IMMUTABLE.on(annotation, annotation.useSiteDescription())) @@ -121,11 +121,11 @@ object AnnotationUseSiteTargetChecker { } private fun BindingTrace.checkIfProperty(annotated: KtAnnotated, annotation: KtAnnotationEntry): Boolean { - val isProperty = if (annotated is KtProperty) - !annotated.isLocal - else if (annotated is KtParameter) - annotated.hasValOrVar() - else false + val isProperty = when (annotated) { + is KtProperty -> !annotated.isLocal + is KtParameter -> annotated.hasValOrVar() + else -> false + } if (!isProperty) report(INAPPLICABLE_TARGET_ON_PROPERTY.on(annotation, annotation.useSiteDescription())) return isProperty diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/OperatorModifierChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/OperatorModifierChecker.kt index 01cc36b8009..0b4f291e060 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/OperatorModifierChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/OperatorModifierChecker.kt @@ -64,10 +64,7 @@ object OperatorModifierChecker { return } - val errorDescription = if (checkResult is CheckResult.IllegalSignature) - checkResult.error - else - "illegal function name" + val errorDescription = (checkResult as? CheckResult.IllegalSignature)?.error ?: "illegal function name" diagnosticHolder.report(Errors.INAPPLICABLE_OPERATOR_MODIFIER.on(modifier, errorDescription)) } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeAliasExpander.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeAliasExpander.kt index db5e3123e25..c7a30d430f4 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeAliasExpander.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeAliasExpander.kt @@ -100,10 +100,8 @@ class TypeAliasExpander( val originalVariance = if (originalProjection.projectionKind != Variance.INVARIANT) originalProjection.projectionKind - else if (typeParameterDescriptor != null) - typeParameterDescriptor.variance else - Variance.INVARIANT + typeParameterDescriptor?.variance ?: Variance.INVARIANT val argumentVariance = typeAliasArgument.projectionKind diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/CapturingInClosureChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/CapturingInClosureChecker.kt index ab1571240d6..097c4b89f8d 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/CapturingInClosureChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/CapturingInClosureChecker.kt @@ -31,7 +31,7 @@ import org.jetbrains.kotlin.types.expressions.CaptureKind class CapturingInClosureChecker : CallChecker { override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) { - val variableResolvedCall = if (resolvedCall is VariableAsFunctionResolvedCall) resolvedCall.variableCall else resolvedCall + val variableResolvedCall = (resolvedCall as? VariableAsFunctionResolvedCall)?.variableCall ?: resolvedCall val variableDescriptor = variableResolvedCall.resultingDescriptor as? VariableDescriptor if (variableDescriptor != null) { checkCapturingInClosure(variableDescriptor, context.trace, context.scope) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/ProtectedConstructorCallChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/ProtectedConstructorCallChecker.kt index e1a667daf24..71bba52fe6d 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/ProtectedConstructorCallChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/ProtectedConstructorCallChecker.kt @@ -33,11 +33,7 @@ object ProtectedConstructorCallChecker : CallChecker { val constructorOwner = descriptor.containingDeclaration.original val scopeOwner = context.scope.ownerDescriptor - val actualConstructor = - if (descriptor is TypeAliasConstructorDescriptor) - descriptor.underlyingConstructorDescriptor - else - descriptor + val actualConstructor = (descriptor as? TypeAliasConstructorDescriptor)?.underlyingConstructorDescriptor ?: descriptor if (actualConstructor.visibility.normalize() != Visibilities.PROTECTED) return // Error already reported diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintPosition.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintPosition.kt index fe0935085e4..16c6015f902 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintPosition.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintPosition.kt @@ -62,7 +62,7 @@ class CompoundConstraintPosition(vararg positions: ConstraintPosition) : Constra get() = COMPOUND_CONSTRAINT_POSITION val positions: Collection = - positions.flatMap { if (it is CompoundConstraintPosition) it.positions else listOf(it) }.toSet() + positions.flatMap { (it as? CompoundConstraintPosition)?.positions ?: listOf(it) }.toSet() override fun isStrong() = positions.any { it.isStrong() } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/smartcasts/DelegatingDataFlowInfo.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/smartcasts/DelegatingDataFlowInfo.kt index 0148d244cf8..f552422a080 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/smartcasts/DelegatingDataFlowInfo.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/smartcasts/DelegatingDataFlowInfo.kt @@ -82,12 +82,7 @@ internal class DelegatingDataFlowInfo private constructor( key.immanentNullability } else { - nullabilityInfo[key] ?: if (parent != null) { - parent.getCollectedNullability(key) - } - else { - key.immanentNullability - } + nullabilityInfo[key] ?: parent?.getCollectedNullability(key) ?: key.immanentNullability } private fun putNullability(map: MutableMap, value: DataFlowValue, diff --git a/compiler/frontend/src/org/jetbrains/kotlin/script/KotlinScriptDefinitionFromAnnotatedTemplate.kt b/compiler/frontend/src/org/jetbrains/kotlin/script/KotlinScriptDefinitionFromAnnotatedTemplate.kt index 4d3f69032bd..bf359e8c62e 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/script/KotlinScriptDefinitionFromAnnotatedTemplate.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/script/KotlinScriptDefinitionFromAnnotatedTemplate.kt @@ -134,8 +134,8 @@ open class KotlinScriptDefinitionFromAnnotatedTemplate( } private fun getAnnotationEntriesFromPsiFile(file: PsiFile) = - if (file is KtFile) file.annotationEntries - else throw IllegalArgumentException("Unable to extract kotlin annotations from ${file.name} (${file.fileType})") + (file as? KtFile)?.annotationEntries + ?: throw IllegalArgumentException("Unable to extract kotlin annotations from ${file.name} (${file.fileType})") private fun getAnnotationEntriesFromVirtualFile(file: VirtualFile, project: Project): Iterable { val psiFile: PsiFile = PsiManager.getInstance(project).findFile(file) diff --git a/idea/src/org/jetbrains/kotlin/idea/actions/internal/CheckPartialBodyResolveAction.kt b/idea/src/org/jetbrains/kotlin/idea/actions/internal/CheckPartialBodyResolveAction.kt index 94f0167d33c..055ad7d66f8 100644 --- a/idea/src/org/jetbrains/kotlin/idea/actions/internal/CheckPartialBodyResolveAction.kt +++ b/idea/src/org/jetbrains/kotlin/idea/actions/internal/CheckPartialBodyResolveAction.kt @@ -127,7 +127,7 @@ class CheckPartialBodyResolveAction : AnAction() { val offset = expression.textOffset val line = document.getLineNumber(offset) val column = offset - document.getLineStartOffset(line) - val exprName = if (expression is KtNameReferenceExpression) expression.getReferencedName() else expression.javaClass.simpleName + val exprName = (expression as? KtNameReferenceExpression)?.getReferencedName() ?: expression.javaClass.simpleName builder.append("$exprName at (${line + 1}:${column + 1})") if (expression is KtReferenceExpression) { diff --git a/idea/src/org/jetbrains/kotlin/idea/debugger/KotlinSourcePositionProvider.kt b/idea/src/org/jetbrains/kotlin/idea/debugger/KotlinSourcePositionProvider.kt index 46d71d1738d..44cb4e5ce48 100644 --- a/idea/src/org/jetbrains/kotlin/idea/debugger/KotlinSourcePositionProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/debugger/KotlinSourcePositionProvider.kt @@ -103,7 +103,7 @@ class KotlinSourcePositionProvider: SourcePositionProvider() { private fun findClassByType(project: Project, type: ReferenceType, context: DebuggerContextImpl): PsiElement? { val session = context.debuggerSession - val scope = if (session != null) session.searchScope else GlobalSearchScope.allScope(project) + val scope = session?.searchScope ?: GlobalSearchScope.allScope(project) val className = JvmClassName.byInternalName(type.name()).fqNameForClassNameWithoutDollars.asString() val myClass = JavaPsiFacade.getInstance(project).findClass(className, scope) diff --git a/idea/src/org/jetbrains/kotlin/idea/debugger/evaluate/KotlinDebuggerCaches.kt b/idea/src/org/jetbrains/kotlin/idea/debugger/evaluate/KotlinDebuggerCaches.kt index 789d15735e7..37a8381a0e1 100644 --- a/idea/src/org/jetbrains/kotlin/idea/debugger/evaluate/KotlinDebuggerCaches.kt +++ b/idea/src/org/jetbrains/kotlin/idea/debugger/evaluate/KotlinDebuggerCaches.kt @@ -155,7 +155,7 @@ class KotlinDebuggerCaches(project: Project) { } private fun getElementToCreateTypeMapperForLibraryFile(element: PsiElement?) = - runReadAction { if (element is KtElement) element else PsiTreeUtil.getParentOfType(element, KtElement::class.java)!! } + runReadAction { element as? KtElement ?: PsiTreeUtil.getParentOfType(element, KtElement::class.java)!! } private fun createTypeMapperForLibraryFile(element: KtElement, file: KtFile): KotlinTypeMapper = runReadAction { diff --git a/idea/src/org/jetbrains/kotlin/idea/debugger/evaluate/extractFunctionForDebuggerUtil.kt b/idea/src/org/jetbrains/kotlin/idea/debugger/evaluate/extractFunctionForDebuggerUtil.kt index a805c322a7e..d8abe9472f4 100644 --- a/idea/src/org/jetbrains/kotlin/idea/debugger/evaluate/extractFunctionForDebuggerUtil.kt +++ b/idea/src/org/jetbrains/kotlin/idea/debugger/evaluate/extractFunctionForDebuggerUtil.kt @@ -327,10 +327,7 @@ private fun findElementBefore(contextElement: PsiElement): PsiElement? { contextElement is KtDeclarationWithBody && contextElement.hasBlockBody() -> { val block = contextElement.bodyExpression as KtBlockExpression val last = block.statements.lastOrNull() - if (last is KtReturnExpression) - last - else - block.rBrace + last as? KtReturnExpression ?: block.rBrace } contextElement is KtWhenEntry -> { val entryExpression = contextElement.expression diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertAssertToIfWithThrowIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertAssertToIfWithThrowIntention.kt index fd04fc0ade5..1a278600b75 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertAssertToIfWithThrowIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertAssertToIfWithThrowIntention.kt @@ -72,10 +72,8 @@ class ConvertAssertToIfWithThrowIntention : SelfTargetingIntention val psiFactory = KtPsiFactory(project) - if (constructor is KtSecondaryConstructor) { - constructor.getOrCreateBody().appendElement(psiFactory.createExpression("this.${element.name} = ${newParam.name!!}")) - } - else { - element.setInitializer(psiFactory.createExpression(newParam.name!!)) - } + (constructor as? KtSecondaryConstructor)?.getOrCreateBody()?.appendElement( + psiFactory.createExpression("this.${element.name} = ${newParam.name!!}") + ) ?: element.setInitializer(psiFactory.createExpression(newParam.name!!)) } processConstructors(project, propertyDescriptor, descriptorsToProcess) } diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/MapPlatformClassToKotlinFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/MapPlatformClassToKotlinFix.kt index 66898416803..4857b2842d2 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/MapPlatformClassToKotlinFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/MapPlatformClassToKotlinFix.kt @@ -105,7 +105,7 @@ class MapPlatformClassToKotlinFix( val replacedElements = ArrayList() for (usage in usages) { val typeArguments = usage.typeArgumentList - val typeArgumentsString = if (typeArguments == null) "" else typeArguments.text + val typeArgumentsString = typeArguments?.text ?: "" val replacementType = KtPsiFactory(project).createType(replacementClassName + typeArgumentsString) val replacementTypeElement = replacementType.typeElement!! val replacedElement = usage.replace(replacementTypeElement) diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceWith/DeprecatedSymbolUsageFixBase.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceWith/DeprecatedSymbolUsageFixBase.kt index ad9309d2578..7c2e656be28 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceWith/DeprecatedSymbolUsageFixBase.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceWith/DeprecatedSymbolUsageFixBase.kt @@ -94,12 +94,11 @@ abstract class DeprecatedSymbolUsageFixBase( else -> null } ?: return null */ - val nameExpression: KtSimpleNameExpression = (if (psiElement is KtSimpleNameExpression) - psiElement - else if (psiElement is KtConstructorCalleeExpression) - psiElement.constructorReferenceExpression - else - null) ?: return null + val nameExpression: KtSimpleNameExpression = when (psiElement) { + is KtSimpleNameExpression -> psiElement + is KtConstructorCalleeExpression -> psiElement.constructorReferenceExpression + else -> null + } ?: return null val descriptor = DiagnosticFactory.cast(deprecatedDiagnostic, Errors.DEPRECATION, Errors.DEPRECATION_ERROR).a val replacement = DeprecatedSymbolUsageFixBase.fetchReplaceWithPattern(descriptor, nameExpression.project) ?: return null diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/changeSignature/KotlinChangeSignature.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/changeSignature/KotlinChangeSignature.kt index 01fa0805a8c..5dd8747010d 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/changeSignature/KotlinChangeSignature.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/changeSignature/KotlinChangeSignature.kt @@ -222,8 +222,7 @@ class KotlinChangeSignature(project: Project, ): KotlinChangeInfo? { val jetChangeSignature = KotlinChangeSignature(project, callableDescriptor, configuration, defaultValueContext, null) val declarations = - if (callableDescriptor is CallableMemberDescriptor) callableDescriptor.getDeepestSuperDeclarations() - else listOf(callableDescriptor) + (callableDescriptor as? CallableMemberDescriptor)?.getDeepestSuperDeclarations() ?: listOf(callableDescriptor) val adjustedDescriptor = jetChangeSignature.adjustDescriptor(declarations) ?: return null diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/changeSignature/KotlinChangeSignatureProcessor.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/changeSignature/KotlinChangeSignatureProcessor.kt index b777f50ca3e..6d2ec6e8a43 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/changeSignature/KotlinChangeSignatureProcessor.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/changeSignature/KotlinChangeSignatureProcessor.kt @@ -93,8 +93,8 @@ class KotlinChangeSignatureProcessor(project: Project, if (u2 is KotlinImplicitReceiverUsage && u1 is KotlinFunctionCallUsage) return@sort 1 val element1 = u1.element val element2 = u2.element - val rank1 = if (element1 != null) element1.textOffset else -1 - val rank2 = if (element2 != null) element2.textOffset else -1 + val rank1 = element1?.textOffset ?: -1 + val rank2 = element2?.textOffset ?: -1 rank2 - rank1 // Reverse order } refUsages.set(usageArray) diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractClass/ExtractSuperRefactoring.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractClass/ExtractSuperRefactoring.kt index 5163799f796..ab7a93ba3a0 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractClass/ExtractSuperRefactoring.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractClass/ExtractSuperRefactoring.kt @@ -199,7 +199,7 @@ class ExtractSuperRefactoring( .asSequence() .flatMap { val (element, info) = it - if (info != null) info.getChildrenToAnalyze().asSequence() else sequenceOf(element) + info?.getChildrenToAnalyze()?.asSequence() ?: sequenceOf(element) } .forEach { it.accept(visitor) } } diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceParameter/KotlinIntroduceParameterHandler.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceParameter/KotlinIntroduceParameterHandler.kt index 1abec488929..d88cf14aa34 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceParameter/KotlinIntroduceParameterHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceParameter/KotlinIntroduceParameterHandler.kt @@ -263,13 +263,8 @@ open class KotlinIntroduceParameterHandler( val parametersUsages = findInternalUsagesOfParametersAndReceiver(targetParent, functionDescriptor) ?: return - val forbiddenRanges = - if (targetParent is KtClass) { - targetParent.declarations.filter { isObjectOrNonInnerClass(it) }.map { it.textRange } - } - else { - Collections.emptyList() - } + val forbiddenRanges = (targetParent as? KtClass)?.declarations?.filter(::isObjectOrNonInnerClass)?.map { it.textRange } + ?: Collections.emptyList() val occurrencesToReplace = if (expression is KtProperty) { ReferencesSearch.search(expression).mapNotNullTo(SmartList(expression.toRange())) { it.element?.toRange() } diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/ui/AbstractParameterTablePanel.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/ui/AbstractParameterTablePanel.kt index f1d34046ec5..342f52e9c42 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/ui/AbstractParameterTablePanel.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/ui/AbstractParameterTablePanel.kt @@ -123,26 +123,14 @@ abstract class AbstractParameterTablePanel(true) { override fun processQuery(queryParameters: DirectClassInheritorsSearch.SearchParameters, consumer: Processor) { val baseClass = queryParameters.classToProcess - if (baseClass == null) return - val name = baseClass.name - if (name == null) return + val name = baseClass.name ?: return val originalScope = queryParameters.scope - val scope = if (originalScope is GlobalSearchScope) - originalScope - else - baseClass.containingFile?.let { file -> file.fileScope() } - - if (scope == null) return + val scope = originalScope as? GlobalSearchScope ?: baseClass.containingFile?.fileScope() ?: return runReadAction { val noLibrarySourceScope = KotlinSourceFilterScope.projectSourceAndClassFiles(scope, baseClass.project) diff --git a/j2k/src/org/jetbrains/kotlin/j2k/ExpressionConverter.kt b/j2k/src/org/jetbrains/kotlin/j2k/ExpressionConverter.kt index 125e8d84c03..d339fcc329a 100644 --- a/j2k/src/org/jetbrains/kotlin/j2k/ExpressionConverter.kt +++ b/j2k/src/org/jetbrains/kotlin/j2k/ExpressionConverter.kt @@ -825,7 +825,7 @@ class DefaultExpressionConverter : JavaElementVisitor(), ExpressionConverter { else if (specialMethod != null) { val factory = PsiElementFactory.SERVICE.getInstance(converter.project) val fakeReceiver = receiver?.let { - val psiExpression = if (qualifier is PsiExpression) qualifier else factory.createExpressionFromText("fakeReceiver", null) + val psiExpression = qualifier as? PsiExpression ?: factory.createExpressionFromText("fakeReceiver", null) psiExpression.convertedExpression = it.first psiExpression } diff --git a/j2k/src/org/jetbrains/kotlin/j2k/SpecialMethod.kt b/j2k/src/org/jetbrains/kotlin/j2k/SpecialMethod.kt index b42ee855683..2a568b3ecb6 100644 --- a/j2k/src/org/jetbrains/kotlin/j2k/SpecialMethod.kt +++ b/j2k/src/org/jetbrains/kotlin/j2k/SpecialMethod.kt @@ -477,7 +477,7 @@ enum class SpecialMethod(val qualifiedClassName: String?, val methodName: String private fun castQualifierToType(codeConverter: CodeConverter, qualifier: PsiExpression, type: String): TypeCastExpression? { val convertedQualifier = codeConverter.convertExpression(qualifier) val qualifierType = codeConverter.typeConverter.convertType(qualifier.type) - val typeArgs = if (qualifierType is ClassType) qualifierType.referenceElement.typeArgs else emptyList() + val typeArgs = (qualifierType as? ClassType)?.referenceElement?.typeArgs ?: emptyList() val referenceElement = ReferenceElement(Identifier.withNoPrototype(type), typeArgs).assignNoPrototype() val newType = ClassType(referenceElement, Nullability.Default, codeConverter.settings).assignNoPrototype() return TypeCastExpression(newType, convertedQualifier).assignNoPrototype() diff --git a/j2k/src/org/jetbrains/kotlin/j2k/StatementConverter.kt b/j2k/src/org/jetbrains/kotlin/j2k/StatementConverter.kt index 44a9ce4f8bb..26773cc15f9 100644 --- a/j2k/src/org/jetbrains/kotlin/j2k/StatementConverter.kt +++ b/j2k/src/org/jetbrains/kotlin/j2k/StatementConverter.kt @@ -191,10 +191,7 @@ class DefaultStatementConverter : JavaElementVisitor(), StatementConverter { val blockConverted = codeConverter.convertBlock(block) val annotations = converter.convertAnnotations(parameter) val parameterType = parameter.type - val types = if (parameterType is PsiDisjunctionType) - parameterType.disjunctions - else - listOf(parameterType) + val types = (parameterType as? PsiDisjunctionType)?.disjunctions ?: listOf(parameterType) for (t in types) { val convertedType = codeConverter.typeConverter.convertType(t, Nullability.NotNull) val convertedParameter = FunctionParameter(parameter.declarationIdentifier(), diff --git a/j2k/src/org/jetbrains/kotlin/j2k/SwitchConverter.kt b/j2k/src/org/jetbrains/kotlin/j2k/SwitchConverter.kt index ff43b205e82..adda40e3381 100644 --- a/j2k/src/org/jetbrains/kotlin/j2k/SwitchConverter.kt +++ b/j2k/src/org/jetbrains/kotlin/j2k/SwitchConverter.kt @@ -94,7 +94,7 @@ class SwitchConverter(private val codeConverter: CodeConverter) { } else { val block = case.statements.singleOrNull() as? PsiBlockStatement - val statements = if (block != null) block.codeBlock.statements.toList() else case.statements + val statements = block?.codeBlock?.statements?.toList() ?: case.statements !statements.any { it is PsiBreakStatement || it is PsiContinueStatement || it is PsiReturnStatement || it is PsiThrowStatement } } return if (fallsThrough) // we fall through into the next case diff --git a/j2k/src/org/jetbrains/kotlin/j2k/usageProcessing/FieldToPropertyProcessing.kt b/j2k/src/org/jetbrains/kotlin/j2k/usageProcessing/FieldToPropertyProcessing.kt index 14517ee5a8d..2b0a95e01d7 100644 --- a/j2k/src/org/jetbrains/kotlin/j2k/usageProcessing/FieldToPropertyProcessing.kt +++ b/j2k/src/org/jetbrains/kotlin/j2k/usageProcessing/FieldToPropertyProcessing.kt @@ -106,10 +106,8 @@ class FieldToPropertyProcessing( is PsiPrefixExpression, is PsiPostfixExpression -> { //TODO: what if it's used as value? - val operationType = if (parent is PsiPrefixExpression) - parent.operationTokenType - else - (parent as PsiPostfixExpression).operationTokenType + val operationType = (parent as? PsiPrefixExpression)?.operationTokenType + ?: (parent as PsiPostfixExpression).operationTokenType val opText = when (operationType) { JavaTokenType.PLUSPLUS -> "+" JavaTokenType.MINUSMINUS -> "-"