Skip to content

Commit

Permalink
Add parens around the replaced node & update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nakulj committed Jun 5, 2024
1 parent 66eb977 commit 27b17b2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 27b17b2

Please sign in to comment.