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
This commit is contained in:
@@ -925,21 +925,11 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.IfThenToElvisIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.SafeAccessToIfThenIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.IfThenToSafeAccessIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.IfToWhenIntention</className>
|
||||
<category>Kotlin</category>
|
||||
|
||||
@@ -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
|
||||
|
||||
+4
@@ -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<KtIfExpression>(
|
||||
{ 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<KtIfExpression>(
|
||||
|
||||
+4
@@ -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<KtIfExpression>(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<KtIfExpression>(
|
||||
|
||||
Reference in New Issue
Block a user