KT-7454 Replace "||" with "&&" adds unnecessary parethesis
#KT-7454 Fixed
This commit is contained in:
+12
-11
@@ -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>? {
|
||||
|
||||
Vendored
+7
@@ -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
|
||||
}
|
||||
+7
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user