Skip to content

Commit 3c1a313

Browse files
Laurens-Wtimtebeek
andauthored
No longer remove String.valueof when it's called on a method invocation (#752)
* Fix CSA issue * Explain the need to exclude method invocations for now --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent 1d8d504 commit 3c1a313

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

src/main/java/org/openrewrite/staticanalysis/NoValueOfOnStringType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
7070
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
7171
if (VALUE_OF.matches(mi) && mi.getArguments().size() == 1) {
7272
Expression argument = mi.getArguments().get(0);
73-
if (TypeUtils.isString(argument.getType()) || removeValueOfForStringConcatenation(argument)) {
73+
if ((TypeUtils.isString(argument.getType()) && !(argument instanceof J.MethodInvocation)) || removeValueOfForStringConcatenation(argument)) {
7474
return maybeParenthesize(argument.withPrefix(mi.getPrefix()), updateCursor(mi));
7575
}
7676
}

src/test/java/org/openrewrite/staticanalysis/NoValueOfOnStringTypeTest.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -249,24 +249,22 @@ void valueOfOnMethodInvocation() {
249249
java(
250250
"""
251251
class Test {
252-
static void method1() {
252+
static void unnecessary() {
253253
String a = String.valueOf(method2());
254254
}
255255
256-
static String method2() {
257-
return "";
258-
}
259-
}
260-
""",
261-
"""
262-
class Test {
263-
static void method1() {
264-
String a = method2();
256+
static void necessary() {
257+
// `"null"` with `String.valueOf` vs `null` without
258+
String b = String.valueOf(method3());
265259
}
266260
267261
static String method2() {
268262
return "";
269263
}
264+
265+
static String method3() {
266+
return null;
267+
}
270268
}
271269
"""
272270
)
@@ -296,4 +294,21 @@ static void method(int i) {
296294
)
297295
);
298296
}
297+
298+
@Test
299+
void doNotRemoveValueOfForNullableStrings() {
300+
rewriteRun(
301+
//language=java
302+
java(
303+
"""
304+
class Test {
305+
306+
String method(Object some) {
307+
return String.valueOf(some.toString());
308+
}
309+
}
310+
"""
311+
)
312+
);
313+
}
299314
}

src/test/java/org/openrewrite/staticanalysis/SimplifyBooleanExpressionTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,4 +417,28 @@ boolean notOne(A a) {
417417
)
418418
);
419419
}
420+
421+
@Test
422+
void correctlySimplifyNegatedTernaryEqualsNull() {
423+
rewriteRun(
424+
java(
425+
"""
426+
class A {
427+
void doSome(String o1, String o2) {
428+
if (!(o1 == null ? o2 == null : o1.equals(o2))) {
429+
}
430+
}
431+
}
432+
""",
433+
"""
434+
class A {
435+
void doSome(String o1, String o2) {
436+
if (o1 == null ? o2 != null : !o1.equals(o2)) {
437+
}
438+
}
439+
}
440+
"""
441+
)
442+
);
443+
}
420444
}

0 commit comments

Comments
 (0)