Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2812 #2814

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, ")")
Comment on lines +98 to +99
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's desirable to unconditionally wrap the suggested fix with parenthesis. This will result in unidiomatic code in the common case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have any suggestions on how to check if the parens are necessary?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't have any suggestions. I suspect this is fairly non-trivial.

I'm not sure it's worthwhile to fix this issue - I imagine it would significantly complicated the logic here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it seems pretty hard.
That said it seems to me that unidiomatic code is preferable to incorrect code- which is what the fixer currently suggests, especially since UnnecessaryParentheses will automatically catch and fix the unidiomatic generated code.

.replace(
tree,
Streams.concat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

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

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