[kotlin] J2K: Remove redundant parentheses around Java ternary operator condition

^KTIJ-30900 Fixed

GitOrigin-RevId: b05a0b4ace13b64ad44ed7a1108608e4fa5cd8ad
This commit is contained in:
Alexey Belkov
2024-08-07 16:17:33 +04:00
committed by intellij-monorepo-bot
parent afd89f9ffd
commit fbdb34b321
6 changed files with 54 additions and 3 deletions

View File

@@ -5771,6 +5771,11 @@ public abstract class NewJavaToKotlinConverterSingleFileTestGenerated extends Ab
runTest("../../shared/tests/testData/newJ2k/parenthesizedExpression/removeUnnecessaryParentheses.java");
}
@TestMetadata("removeUnnecessaryParenthesesConditionalExpression.java")
public void testRemoveUnnecessaryParenthesesConditionalExpression() throws Exception {
runTest("../../shared/tests/testData/newJ2k/parenthesizedExpression/removeUnnecessaryParenthesesConditionalExpression.java");
}
@TestMetadata("removeUnnecessaryParenthesesConditionalOperators.java")
public void testRemoveUnnecessaryParenthesesConditionalOperators() throws Exception {
runTest("../../shared/tests/testData/newJ2k/parenthesizedExpression/removeUnnecessaryParenthesesConditionalOperators.java");

View File

@@ -5771,6 +5771,11 @@ public abstract class K2JavaToKotlinConverterSingleFileTestGenerated extends Abs
runTest("../../shared/tests/testData/newJ2k/parenthesizedExpression/removeUnnecessaryParentheses.java");
}
@TestMetadata("removeUnnecessaryParenthesesConditionalExpression.java")
public void testRemoveUnnecessaryParenthesesConditionalExpression() throws Exception {
runTest("../../shared/tests/testData/newJ2k/parenthesizedExpression/removeUnnecessaryParenthesesConditionalExpression.java");
}
@TestMetadata("removeUnnecessaryParenthesesConditionalOperators.java")
public void testRemoveUnnecessaryParenthesesConditionalOperators() throws Exception {
runTest("../../shared/tests/testData/newJ2k/parenthesizedExpression/removeUnnecessaryParenthesesConditionalOperators.java");

View File

@@ -194,8 +194,12 @@ class JavaToJKTreeBuilder(
}
private fun PsiConditionalExpression.toJK(): JKIfElseExpression {
val condition = this.condition.toJK().let {
// Unwrap parentheses to avoid double-wrapping in Kotlin: `if ((condition))`
if (it is JKParenthesizedExpression) it::expression.detached() else it
}
val expression = JKIfElseExpression(
condition.toJK(),
condition,
thenExpression.toJK(),
elseExpression.toJK(),
type.toJK()

View File

@@ -0,0 +1,18 @@
public class J {
String test1(String s) {
return (s != null) ? "true" : "false";
}
String test2(String s) {
return (s
!=
null) ? "true" : "false";
}
String test3(String s) {
return
(s != null)
? "true"
: "false";
}
}

View File

@@ -0,0 +1,19 @@
class J {
fun test1(s: String?): String {
return if (s != null) "true" else "false"
}
fun test2(s: String?): String {
return if (s
!=
null
) "true" else "false"
}
fun test3(s: String?): String {
return if (s != null)
"true"
else
"false"
}
}

View File

@@ -10,8 +10,8 @@ class Foo {
val product = (a[j].toLong() and LONG_MASK) * xLong + carry
val difference = q[offset--] - product
carry = ((product ushr 32)
+ (if (((difference and LONG_MASK) >
(((product.toInt().inv()).toLong() and LONG_MASK)))
+ (if ((difference and LONG_MASK) >
(((product.toInt().inv()).toLong() and LONG_MASK))
) 1 else 0))
}
return carry.toInt()