diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/FoldInitializerAndIfToElvisInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/FoldInitializerAndIfToElvisInspection.kt index 433bc1ef231..965905d8f2b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/FoldInitializerAndIfToElvisInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/FoldInitializerAndIfToElvisInspection.kt @@ -19,6 +19,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny import org.jetbrains.kotlin.idea.core.replaced import org.jetbrains.kotlin.idea.core.setType +import org.jetbrains.kotlin.idea.core.util.isMultiLine import org.jetbrains.kotlin.idea.intentions.branchedTransformations.elvisPattern import org.jetbrains.kotlin.idea.intentions.branchedTransformations.expressionComparedToNull import org.jetbrains.kotlin.idea.intentions.branchedTransformations.fromIfKeywordToRightParenthesisTextRangeInThis @@ -63,6 +64,11 @@ class FoldInitializerAndIfToElvisInspection : AbstractApplicabilityBasedInspecti private fun applicabilityRange(element: KtIfExpression): TextRange? { val data = calcData(element) ?: return null + if (data.initializer !is KtParenthesizedExpression + && data.initializer.isMultiLine() + && createElvisExpression(element, data, KtPsiFactory(element)).left is KtParenthesizedExpression + ) return null + val type = data.ifNullExpression.analyze().getType(data.ifNullExpression) ?: return null if (!type.isNothing()) return null @@ -72,7 +78,8 @@ class FoldInitializerAndIfToElvisInspection : AbstractApplicabilityBasedInspecti fun isApplicable(element: KtIfExpression): Boolean = applicabilityRange(element) != null fun applyTo(element: KtIfExpression): KtBinaryExpression { - val (initializer, declaration, ifNullExpr, typeReference) = calcData(element)!! + val data = calcData(element)!! + val (initializer, declaration, _, typeReference) = data val factory = KtPsiFactory(element) val explicitTypeToSet = when { @@ -90,11 +97,7 @@ class FoldInitializerAndIfToElvisInspection : AbstractApplicabilityBasedInspecti val commentSaver = CommentSaver(childRangeBefore) val childRangeAfter = childRangeBefore.withoutLastStatement() - val margin = CodeStyle.getSettings(element.containingKtFile).defaultRightMargin - val declarationTextLength = declaration.text.split("\n").lastOrNull()?.trim()?.length ?: 0 - val pattern = elvisPattern(declarationTextLength + ifNullExpr.textLength + 5 >= margin || element.then?.hasComments() == true) - - val elvis = factory.createExpressionByPattern(pattern, initializer, ifNullExpr) as KtBinaryExpression + val elvis = createElvisExpression(element, data, factory) return runWriteAction { if (typeReference != null) { @@ -112,6 +115,14 @@ class FoldInitializerAndIfToElvisInspection : AbstractApplicabilityBasedInspecti } } + private fun createElvisExpression(element: KtIfExpression, data: Data, factory: KtPsiFactory): KtBinaryExpression { + val (initializer, declaration, ifNullExpr, _) = data + val margin = CodeStyle.getSettings(element.containingKtFile).defaultRightMargin + val declarationTextLength = declaration.text.split("\n").lastOrNull()?.trim()?.length ?: 0 + val pattern = elvisPattern(declarationTextLength + ifNullExpr.textLength + 5 >= margin || element.then?.hasComments() == true) + return factory.createExpressionByPattern(pattern, initializer, ifNullExpr) as KtBinaryExpression + } + private fun calcData(ifExpression: KtIfExpression): Data? { if (ifExpression.`else` != null) return null diff --git a/idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/ComplexExpression.kt b/idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/ComplexExpression.kt new file mode 100644 index 00000000000..1157c903827 --- /dev/null +++ b/idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/ComplexExpression.kt @@ -0,0 +1,12 @@ +// PROBLEM: none +private fun foo(arg: Any): Int { + val x = + if (arg is String) + (arg.length + 1) + else + arg.hashCode() + + if (x == null) return 0 + + return x +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 27e964142c4..ad60217f1e6 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -4814,6 +4814,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/Comments2.kt"); } + @TestMetadata("ComplexExpression.kt") + public void testComplexExpression() throws Exception { + runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/ComplexExpression.kt"); + } + @TestMetadata("Continue.kt") public void testContinue() throws Exception { runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/Continue.kt");