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 d22b28faf..e512fe83f 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,12 @@ 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 +91,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 +125,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 +159,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 +179,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 +244,13 @@ 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 +268,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 +292,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,7 +316,7 @@ 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);