Skip to content

Commit a3d37de

Browse files
authored
Preserve parentheses around unary operators in method invocation select (#6663)
* Preserve parentheses around unary operators in method invocation select When the select expression of a method invocation is a parenthesized unary operator like (++i).toString(), the parentheses are required. Without them, ++i.toString() is a syntax error because the . operator has higher precedence than ++. The fix adds J.Unary to the list of expression types that should retain their parentheses in visitMethodInvocation, alongside Assignment, Binary, Ternary, TypeCast, and SwitchExpression. Fixes moderneinc/customer-requests#1790 * Add tests for unwrapping unary operators in assignment and argument contexts Adds tests to verify that unnecessary parentheses around unary operators (++i, i++, --i, i--) are correctly removed when they appear in: - Variable initializers: int x = (++i) → int x = ++i - Method arguments: println((++i)) → println(++i) These complement the methodInvocationSelectUnary test which verifies that parentheses are preserved when required (i.e., (++i).toString()).
1 parent 7b6d68b commit a3d37de

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

rewrite-java-test/src/test/java/org/openrewrite/java/cleanup/UnnecessaryParenthesesTest.java

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,63 @@ boolean falseMethod() {
932932
);
933933
}
934934

935+
@Test
936+
void unwrapUnaryInAssignment() {
937+
rewriteRun(
938+
unnecessaryParentheses(style -> style.withAssign(true)),
939+
java(
940+
"""
941+
class Test {
942+
void test() {
943+
int i = 1;
944+
int a = (++i);
945+
int b = (i++);
946+
int c = (--i);
947+
int d = (i--);
948+
}
949+
}
950+
""",
951+
"""
952+
class Test {
953+
void test() {
954+
int i = 1;
955+
int a = ++i;
956+
int b = i++;
957+
int c = --i;
958+
int d = i--;
959+
}
960+
}
961+
"""
962+
)
963+
);
964+
}
965+
966+
@Test
967+
void unwrapUnaryInMethodArgument() {
968+
rewriteRun(
969+
java(
970+
"""
971+
class Test {
972+
void test() {
973+
int i = 1;
974+
System.out.println((++i));
975+
System.out.println((i++));
976+
}
977+
}
978+
""",
979+
"""
980+
class Test {
981+
void test() {
982+
int i = 1;
983+
System.out.println(++i);
984+
System.out.println(i++);
985+
}
986+
}
987+
"""
988+
)
989+
);
990+
}
991+
935992
@ExpectedToFail("Not implemented yet")
936993
@Test
937994
void unwrapBinaryInIf() {
@@ -1140,6 +1197,54 @@ void f(Object x) {
11401197
)
11411198
);
11421199
}
1200+
1201+
@Issue("https://github.com/moderneinc/customer-requests/issues/1790")
1202+
@Test
1203+
void methodInvocationSelectUnary() {
1204+
//language=java
1205+
rewriteRun(
1206+
java(
1207+
"""
1208+
class A {
1209+
void f() {
1210+
Integer i = Integer.valueOf(1);
1211+
String s = (++i).toString();
1212+
}
1213+
}
1214+
"""
1215+
),
1216+
java(
1217+
"""
1218+
class B {
1219+
void f() {
1220+
Integer i = Integer.valueOf(1);
1221+
String s = (i++).toString();
1222+
}
1223+
}
1224+
"""
1225+
),
1226+
java(
1227+
"""
1228+
class C {
1229+
void f() {
1230+
Integer i = Integer.valueOf(1);
1231+
String s = (--i).toString();
1232+
}
1233+
}
1234+
"""
1235+
),
1236+
java(
1237+
"""
1238+
class D {
1239+
void f() {
1240+
Integer i = Integer.valueOf(1);
1241+
String s = (i--).toString();
1242+
}
1243+
}
1244+
"""
1245+
)
1246+
);
1247+
}
11431248
}
11441249

11451250
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/534")

rewrite-java/src/main/java/org/openrewrite/java/cleanup/UnnecessaryParenthesesVisitor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ public J visitMethodInvocation(J.MethodInvocation method, P p) {
226226
parentheses.getTree() instanceof J.Binary ||
227227
parentheses.getTree() instanceof J.Ternary ||
228228
parentheses.getTree() instanceof J.TypeCast ||
229-
parentheses.getTree() instanceof J.SwitchExpression) {
229+
parentheses.getTree() instanceof J.SwitchExpression ||
230+
parentheses.getTree() instanceof J.Unary) {
230231
return mi;
231232
}
232233
Expression tree = (Expression) parentheses.getTree();

0 commit comments

Comments
 (0)