KT-7454 Replace "||" with "&&" adds unnecessary parethesis

#KT-7454 Fixed
This commit is contained in:
Valentin Kipyatkov
2015-06-02 23:04:33 +03:00
parent 1a0f8a5070
commit ac6ef6b544
4 changed files with 32 additions and 11 deletions
@@ -52,21 +52,22 @@ public class ConvertNegatedExpressionWithDemorgansLawIntention : JetSelfTargetin
else -> throw IllegalArgumentException()
}
val text = splitBooleanSequence(baseExpression)!!
.map { negatedExpressionText(it) }
.reverse()
.joinToString(operatorText)
val operands = splitBooleanSequence(baseExpression)!!.reverse()
val newExpression = JetPsiFactory(element).buildExpression {
for ((i, operand) in operands.withIndex()) {
if (i > 0) {
appendFixedText(operatorText)
}
appendExpression(operand.negated())
}
}
val newExpression = JetPsiFactory(element).createExpression(text)
element.replace(newExpression)
}
private fun negatedExpressionText(expression: JetExpression): String {
val text = expression.getText()
return when (expression) {
is JetSimpleNameExpression, is JetConstantExpression, is JetPrefixExpression, is JetParenthesizedExpression -> "!$text"
else -> "!($text)"
}
private fun JetExpression.negated(): JetExpression {
return JetPsiFactory(this).createExpressionByPattern("!$0", this)
}
private fun splitBooleanSequence(expression: JetBinaryExpression): List<JetExpression>? {
@@ -0,0 +1,7 @@
object O {
fun foo(): Boolean = true
fun bar(): Boolean = true
}
fun foo(p1: Boolean, p2: Boolean) {
if (<caret>!(O.foo() || O.bar())) return
}
@@ -0,0 +1,7 @@
object O {
fun foo(): Boolean = true
fun bar(): Boolean = true
}
fun foo(p1: Boolean, p2: Boolean) {
if (<caret>!O.foo() && !O.bar()) return
}
@@ -3247,6 +3247,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("dontAddRedundantParenthesis.kt")
public void testDontAddRedundantParenthesis() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertNegatedExpressionWithDemorgansLaw/dontAddRedundantParenthesis.kt");
doTest(fileName);
}
@TestMetadata("doubleNegation.kt")
public void testDoubleNegation() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertNegatedExpressionWithDemorgansLaw/doubleNegation.kt");