From 4f829583096081071666c51abe10d884d6e8724c Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Fri, 28 Oct 2016 16:28:03 +0300 Subject: [PATCH] If then to elvis: do not report inspection if not used as expression (cherry picked from commit 2adcb27) --- .../intentions/IfThenToElvisIntention.kt | 7 +- .../ifThenToElvis/inspectionData/expected.xml | 72 ------------------- 2 files changed, 6 insertions(+), 73 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToElvisIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToElvisIntention.kt index 42a69b1111e..05aa9378676 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToElvisIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToElvisIntention.kt @@ -29,11 +29,16 @@ import org.jetbrains.kotlin.idea.intentions.replaceFirstReceiver import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression import org.jetbrains.kotlin.resolve.calls.callUtil.getType +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.types.TypeUtils import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf -class IfThenToElvisInspection : IntentionBasedInspection(IfThenToElvisIntention::class) +class IfThenToElvisInspection : IntentionBasedInspection( + IfThenToElvisIntention::class, + { it -> it.isUsedAsExpression(it.analyze(BodyResolveMode.PARTIAL)) } +) class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention( KtIfExpression::class.java, diff --git a/idea/testData/intentions/branched/ifThenToElvis/inspectionData/expected.xml b/idea/testData/intentions/branched/ifThenToElvis/inspectionData/expected.xml index c229a862e9f..9b06f6aa724 100644 --- a/idea/testData/intentions/branched/ifThenToElvis/inspectionData/expected.xml +++ b/idea/testData/intentions/branched/ifThenToElvis/inspectionData/expected.xml @@ -7,38 +7,6 @@ If-Then foldable to '?:'. Replace 'if' expression with elvis expression - - rhsNotEqualsNull.kt - 9 - light_idea_test_case - - If-Then foldable to '?:'. - Replace 'if' expression with elvis expression - - - rhsEqualsNull.kt - 8 - light_idea_test_case - - If-Then foldable to '?:'. - Replace 'if' expression with elvis expression - - - lhsNotEqualsNull.kt - 8 - light_idea_test_case - - If-Then foldable to '?:'. - Replace 'if' expression with elvis expression - - - lhsEqualsNull.kt - 8 - light_idea_test_case - - If-Then foldable to '?:'. - Replace 'if' expression with elvis expression - ifAsExpression.kt 8 @@ -55,30 +23,6 @@ If-Then foldable to '?:'. Replace 'if' expression with elvis expression - - ifAndElseNotInBlocks.kt - 8 - light_idea_test_case - - If-Then foldable to '?:'. - Replace 'if' expression with elvis expression - - - ifAndElseBothInBlocks.kt - 8 - light_idea_test_case - - If-Then foldable to '?:'. - Replace 'if' expression with elvis expression - - - doesNotinlineValueOutsideOfScope.kt - 8 - light_idea_test_case - - If-Then foldable to '?:'. - Replace 'if' expression with elvis expression - doesNotInlineVariableInMultiDeclaration.kt 7 @@ -87,22 +31,6 @@ If-Then foldable to '?:'. Replace 'if' expression with elvis expression - - doesNotinlineValueIfUsedMoreThanOnce.kt - 11 - light_idea_test_case - - If-Then foldable to '?:'. - Replace 'if' expression with elvis expression - - - throwsNPEwithArgument.kt - 4 - light_idea_test_case - - If-Then foldable to '?:' - Replace 'if' expression with elvis expression - notIsCheck.kt 4