From 43476bd773e2585dd44675083a689933f84a2e76 Mon Sep 17 00:00:00 2001 From: Natalia Ukhorskaya Date: Thu, 17 Dec 2015 11:01:03 +0300 Subject: [PATCH] Do not drop negation if resulting type isn't boolean --- idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt | 8 +++++++- .../doubleNegation.kt | 1 - .../doubleNegation.kt.after | 3 +-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt index 84505b7c290..ea50cceda46 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt @@ -18,9 +18,11 @@ package org.jetbrains.kotlin.idea.intentions import com.intellij.psi.tree.IElementType import org.jetbrains.kotlin.KtNodeTypes +import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.idea.caches.resolve.analyzeAndGetResult import org.jetbrains.kotlin.idea.references.mainReference import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers import org.jetbrains.kotlin.idea.util.ShortenReferences @@ -136,7 +138,11 @@ private fun KtExpression.specialNegation(): KtExpression? { if (getOperationReference().getReferencedName() == "!") { val baseExpression = getBaseExpression() if (baseExpression != null) { - return KtPsiUtil.safeDeparenthesize(baseExpression) + val context = baseExpression.analyzeAndGetResult().bindingContext + val type = context.getType(baseExpression) + if (type != null && KotlinBuiltIns.isBoolean(type)) { + return KtPsiUtil.safeDeparenthesize(baseExpression) + } } } } diff --git a/idea/testData/intentions/convertNegatedExpressionWithDemorgansLaw/doubleNegation.kt b/idea/testData/intentions/convertNegatedExpressionWithDemorgansLaw/doubleNegation.kt index b3ca9001022..82e75fd43ba 100644 --- a/idea/testData/intentions/convertNegatedExpressionWithDemorgansLaw/doubleNegation.kt +++ b/idea/testData/intentions/convertNegatedExpressionWithDemorgansLaw/doubleNegation.kt @@ -1,4 +1,3 @@ -// SKIP_ERRORS_AFTER operator fun String.not(): Boolean { return length == 0 } diff --git a/idea/testData/intentions/convertNegatedExpressionWithDemorgansLaw/doubleNegation.kt.after b/idea/testData/intentions/convertNegatedExpressionWithDemorgansLaw/doubleNegation.kt.after index a9c1116c49a..bc10f2441a0 100644 --- a/idea/testData/intentions/convertNegatedExpressionWithDemorgansLaw/doubleNegation.kt.after +++ b/idea/testData/intentions/convertNegatedExpressionWithDemorgansLaw/doubleNegation.kt.after @@ -1,8 +1,7 @@ -// SKIP_ERRORS_AFTER operator fun String.not(): Boolean { return length == 0 } fun foo(a: Boolean, b: Boolean) : Boolean { - return "" && !b + return !!"" && !b }