diff --git a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/StringBuilderConstantParameters.java b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/StringBuilderConstantParameters.java index 4a8f62ceb..461cdc656 100644 --- a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/StringBuilderConstantParameters.java +++ b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/StringBuilderConstantParameters.java @@ -95,6 +95,8 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState return buildDescription(tree) .setMessage(MESSAGE) .addFix(SuggestedFix.builder() + .prefixWith(tree, "(") + .postfixWith(tree, ")") .replace( tree, Streams.concat( diff --git a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/StringBuilderConstantParametersTests.java b/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/StringBuilderConstantParametersTests.java index d132f1a9b..d0f1a67bf 100644 --- a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/StringBuilderConstantParametersTests.java +++ b/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/StringBuilderConstantParametersTests.java @@ -54,7 +54,7 @@ public void shouldWarnOnConstantNumberOfParams_fix() { " }", "}") .addOutputLines( - "Test.java", "class Test {", " String f() {", " return \"foo\" + 1;", " }", "}") + "Test.java", "class Test {", " String f() {", " return (\"foo\" + 1);", " }", "}") .doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); } @@ -86,7 +86,7 @@ public void shouldWarnOnConstantNumberOfParams_stringCtor_fix() { "Test.java", "class Test {", " String f() {", - " return \"ctor\" + \"foo\" + 1;", + " return (\"ctor\" + \"foo\" + 1);", " }", "}") .doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); @@ -120,7 +120,7 @@ public void shouldWarnOnConstantNumberOfParams_charSequenceCtor_fix() { "Test.java", "class Test {", " String f(CharSequence charSequence) {", - " return \"\" + charSequence + \"foo\" + 1;", + " return (\"\" + charSequence + \"foo\" + 1);", " }", "}") .doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); @@ -154,7 +154,7 @@ public void shouldWarnOnConstantNumberOfNonConstantParams_fix() { "Test.java", "class Test {", " String f(long param0, double param1) {", - " return \"\" + param0 + param1;", + " return (\"\" + param0 + param1);", " }", "}") .doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); @@ -174,7 +174,7 @@ public void shouldWarnOnConstantNumberOfNonConstantParams_firstString_fix() { "Test.java", "class Test {", " String f(String param0, double param1) {", - " return param0 + param1;", + " return (param0 + param1);", " }", "}") .doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); @@ -239,7 +239,7 @@ public void shouldWarnOnNoParams_fix() { " return new StringBuilder().toString();", " }", "}") - .addOutputLines("Test.java", "class Test {", " String f() {", " return \"\";", " }", "}") + .addOutputLines("Test.java", "class Test {", " String f() {", " return (\"\");", " }", "}") .doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); } @@ -257,7 +257,7 @@ public void suggestedFixRetainsCast() { "Test.java", "class Test {", " String f(Object obj) {", - " return (String) obj + 1;", + " return ((String) obj + 1);", " }", "}") .doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); @@ -281,7 +281,7 @@ public void suggestedFixHandlesTernary() { "Test.java", "class Test {", " String f(Object obj) {", - " return \"a\" + (obj == null ? \"nil\" : obj) + \"b\";", + " return (\"a\" + (obj == null ? \"nil\" : obj) + \"b\");", " }", "}") .doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); @@ -305,12 +305,36 @@ public void suggestedFixHandlesAddition() { "Test.java", "class Test {", " String f(int param0, int param1) {", - " return \"a\" + (param0 + param1) + \"b\";", + " return (\"a\" + (param0 + param1) + \"b\");", " }", "}") .doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); } + @Test + public void suggestedFixHandlesMethodCalledOnBuilt() { + RefactoringValidator.of(StringBuilderConstantParameters.class, getClass()) + .addInputLines( + "Test.java", + "class Test {", + " String f() {", + " return new StringBuilder()", + " .append(\"foo\")", + " .append(\"bar\")", + " .toString()", + " .toLowerCase();", + " }", + "}") + .addOutputLines( + "Test.java", + "class Test {", + " String f() {", + " return (\"foo\" + \"bar\").toLowerCase();", + " }", + "}") + .doTest(); + } + @Test public void negativeDynamicStringBuilder() { compilationHelper