From 81a06de5c8e3f7c69718045ce8b844e7715d8df9 Mon Sep 17 00:00:00 2001 From: martinfrancois Date: Tue, 11 Aug 2026 20:14:32 +0200 Subject: [PATCH] ReplaceStringConcatenationWithStringValueOf: add failing tests for out-of-scope paren unwrap and untyped operand doNotChangeParenthesizedValueOfWithoutConcatenation pins that visitParentheses strips parens from a pre-existing (String.valueOf(o)) in a file where no concatenation was rewritten. doNotChangeWhenOperandTypeIsMissing pins that an operand without type attribution is still rewritten, which changes the rendered value when it is really a char[]. Both are marked @ExpectedToFail. Relates to #975. --- ...ingConcatenationWithStringValueOfTest.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/test/java/org/openrewrite/staticanalysis/ReplaceStringConcatenationWithStringValueOfTest.java b/src/test/java/org/openrewrite/staticanalysis/ReplaceStringConcatenationWithStringValueOfTest.java index 9a341a68e..0b510d0c7 100644 --- a/src/test/java/org/openrewrite/staticanalysis/ReplaceStringConcatenationWithStringValueOfTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/ReplaceStringConcatenationWithStringValueOfTest.java @@ -17,9 +17,11 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import org.junitpioneer.jupiter.ExpectedToFail; import org.openrewrite.DocumentExample; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; +import org.openrewrite.test.TypeValidation; import static org.openrewrite.java.Assertions.java; @@ -323,5 +325,40 @@ void method() { ) ); } + + @ExpectedToFail("visitParentheses unwraps pre-existing (String.valueOf(..)) even when no concatenation was rewritten") + @Test + void doNotChangeParenthesizedValueOfWithoutConcatenation() { + rewriteRun( + //language=java + java( + """ + class Test { + String method(Object o) { + return (String.valueOf(o)); + } + } + """ + ) + ); + } + + @ExpectedToFail("Operand without type attribution is still rewritten; not value-preserving if it is really a char[]") + @Test + void doNotChangeWhenOperandTypeIsMissing() { + rewriteRun( + spec -> spec.typeValidationOptions(TypeValidation.none()), + //language=java + java( + """ + class Test { + String method(Unresolved holder) { + return "" + holder.chars(); + } + } + """ + ) + ); + } } }