openrewrite / rewrite-static-analysis

OpenRewrite recipes for identifying and fixing static analysis issues.
Apache License 2.0
27 stars 43 forks source link

FixStringFormatExpressions wrongly replaces `\\n` #260

Closed Bananeweizen closed 4 months ago

Bananeweizen commented 4 months ago

What is the smallest, simplest way to reproduce the problem?

class A {
    void foo(String bar) {
       return """
                  \\n
                  """.formatted(bar);
    }
}

What did you expect to see?

the code remaining as is.

What did you see instead?

class A {
    void foo(String bar) {
        return """
                   \%n
                   """.formatted(bar);
    }
}

That's an invalid escape sequence. Root cause of the bug is the recipe replacing \n without ever checking if that's part of \\n.

Be aware that even though the test looks like it contains the double backslash in the test code, it doesn't. The text block contains double backslash, but that only generates the Java test input, which contains a single backslash.

Are you interested in contributing a fix to OpenRewrite?

Yes. But too tired to look into that now.

timtebeek commented 4 months ago

Thanks for the report @Bananeweizen ! I've pushed a quick attempt up at

Could use a review as there's a ton of slashes involved. 😅