Skip to content

Commit

Permalink
Limit NoValueOfOnStringType binary handling to String concatenations
Browse files Browse the repository at this point in the history
Fixes #456
  • Loading branch information
timtebeek committed Feb 4, 2025
1 parent 7912c39 commit 09ea6c0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (VALUE_OF.matches(mi) && mi.getArguments().size() == 1) {
Expression argument = mi.getArguments().get(0);
if (argument instanceof J.Binary) {
if (argument instanceof J.Binary && removeValueOfForStringConcatenation(argument)) {
return JavaTemplate.builder("(#{any(java.lang.String)})").build()
.apply(updateCursor(mi), mi.getCoordinates().replace(), argument);
} else if (TypeUtils.isString(argument.getType()) || removeValueOfForStringConcatenation(argument)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,23 @@ static void count(int i) {
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/456")
void valueOfOnNonStringPrimitiveWithBinaryArgument() {
rewriteRun(
//language=java
java(
"""
class Test {
static void method(int i) {
String s = String.valueOf(41 + 1);
}
}
"""
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite/issues/1200")
void valueOfIsMethodInvocationPartOfBinary() {
Expand Down

0 comments on commit 09ea6c0

Please sign in to comment.