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()
|
else -> throw IllegalArgumentException()
|
||||||
}
|
}
|
||||||
|
|
||||||
val text = splitBooleanSequence(baseExpression)!!
|
val operands = splitBooleanSequence(baseExpression)!!.reverse()
|
||||||
.map { negatedExpressionText(it) }
|
|
||||||
.reverse()
|
val newExpression = JetPsiFactory(element).buildExpression {
|
||||||
.joinToString(operatorText)
|
for ((i, operand) in operands.withIndex()) {
|
||||||
|
if (i > 0) {
|
||||||
|
appendFixedText(operatorText)
|
||||||
|
}
|
||||||
|
appendExpression(operand.negated())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val newExpression = JetPsiFactory(element).createExpression(text)
|
|
||||||
element.replace(newExpression)
|
element.replace(newExpression)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun negatedExpressionText(expression: JetExpression): String {
|
private fun JetExpression.negated(): JetExpression {
|
||||||
val text = expression.getText()
|
return JetPsiFactory(this).createExpressionByPattern("!$0", this)
|
||||||
return when (expression) {
|
|
||||||
is JetSimpleNameExpression, is JetConstantExpression, is JetPrefixExpression, is JetParenthesizedExpression -> "!$text"
|
|
||||||
else -> "!($text)"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun splitBooleanSequence(expression: JetBinaryExpression): List<JetExpression>? {
|
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);
|
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")
|
@TestMetadata("doubleNegation.kt")
|
||||||
public void testDoubleNegation() throws Exception {
|
public void testDoubleNegation() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertNegatedExpressionWithDemorgansLaw/doubleNegation.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertNegatedExpressionWithDemorgansLaw/doubleNegation.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user