From a77390ccd60409ef03813caaf243a7869386aaa1 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Fri, 5 May 2017 13:10:45 +0300 Subject: [PATCH] Change both IfThenTo... inspections highlight level Retain WEAK WARNING for null checks, one-liners, and is checks without dot calls in main branch. Set INFORMATION (no highlighting, just fix) for other cases. Switch off both bound intentions. So #KT-15076 Fixed --- idea/src/META-INF/plugin.xml | 10 ---------- .../branchedTransformations/IfThenUtils.kt | 16 ++++++++++++++++ .../intentions/IfThenToElvisIntention.kt | 4 ++++ .../intentions/IfThenToSafeAccessIntention.kt | 4 ++++ 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index cc7c573c329..ee7b0388a05 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -925,21 +925,11 @@ Kotlin - - org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.IfThenToElvisIntention - Kotlin - - org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.SafeAccessToIfThenIntention Kotlin - - org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.IfThenToSafeAccessIntention - Kotlin - - org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.IfToWhenIntention Kotlin diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt index be4742df7f2..df8cde68ef2 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt @@ -27,6 +27,7 @@ import org.jetbrains.kotlin.idea.intentions.getLeftMostReceiverExpression import org.jetbrains.kotlin.idea.intentions.replaceFirstReceiver import org.jetbrains.kotlin.idea.refactoring.inline.KotlinInlineValHandler import org.jetbrains.kotlin.idea.refactoring.introduce.introduceVariable.KotlinIntroduceVariableHandler +import org.jetbrains.kotlin.idea.refactoring.isMultiLine import org.jetbrains.kotlin.idea.references.mainReference import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* @@ -201,6 +202,21 @@ internal fun KtIfExpression.buildSelectTransformationData(): IfThenToSelectData? return IfThenToSelectData(context, condition, receiverExpression, baseClause, negatedClause) } +internal fun KtIfExpression.shouldBeTransformed(): Boolean { + val condition = condition + return when (condition) { + is KtBinaryExpression -> true + is KtIsExpression -> { + if (!isMultiLine()) true + else { + val baseClause = (if (condition.isNegated) `else` else then)?.unwrapBlockOrParenthesis() + baseClause !is KtDotQualifiedExpression + } + } + else -> false + } +} + private fun KtExpression.checkedExpression() = when (this) { is KtBinaryExpression -> expressionComparedToNull() is KtIsExpression -> leftHandSide 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 db45470b06a..5fa78985942 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 @@ -16,6 +16,7 @@ package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions +import com.intellij.codeInspection.ProblemHighlightType import com.intellij.openapi.editor.Editor import org.jetbrains.kotlin.KtNodeTypes import org.jetbrains.kotlin.builtins.KotlinBuiltIns @@ -35,6 +36,9 @@ class IfThenToElvisInspection : IntentionBasedInspection( { it -> it.isUsedAsExpression(it.analyze(BodyResolveMode.PARTIAL)) } ) { override fun inspectionTarget(element: KtIfExpression) = element.ifKeyword + + override fun problemHighlightType(element: KtIfExpression): ProblemHighlightType = + if (element.shouldBeTransformed()) ProblemHighlightType.WEAK_WARNING else super.problemHighlightType(element) } class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention( diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToSafeAccessIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToSafeAccessIntention.kt index b9cbf698540..130268fca63 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToSafeAccessIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToSafeAccessIntention.kt @@ -16,6 +16,7 @@ package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions +import com.intellij.codeInspection.ProblemHighlightType import com.intellij.openapi.editor.Editor import com.intellij.psi.PsiElement import org.jetbrains.kotlin.idea.core.replaced @@ -27,6 +28,9 @@ import org.jetbrains.kotlin.psi.* class IfThenToSafeAccessInspection : IntentionBasedInspection(IfThenToSafeAccessIntention::class) { override fun inspectionTarget(element: KtIfExpression) = element.ifKeyword + + override fun problemHighlightType(element: KtIfExpression): ProblemHighlightType = + if (element.shouldBeTransformed()) ProblemHighlightType.WEAK_WARNING else ProblemHighlightType.INFORMATION } class IfThenToSafeAccessIntention : SelfTargetingOffsetIndependentIntention(