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

[java] UseStringBufferForStringAppends: Wrong example in documentation #3374

Closed
ghost opened this issue Jul 1, 2021 · 3 comments · Fixed by #3780
Closed

[java] UseStringBufferForStringAppends: Wrong example in documentation #3374

ghost opened this issue Jul 1, 2021 · 3 comments · Fixed by #3780
Labels
in:documentation Affects the documentation
Milestone

Comments

@ghost
Copy link

ghost commented Jul 1, 2021

Affects PMD Version:

Rule:UseStringBufferForStringAppends

Please provide the rule name and a link to the rule documentation:
https://pmd.github.io/latest/pmd_rules_java_performance.html#usestringbufferforstringappends

Description:
Hi, I have encountered a false negative when PMD tried to detect UseStringBufferForStringAppends. Below is the test case for reproducing this false negative:

Code Sample demonstrating the issue:

public void testUseStringBufferForStringAppends() {
    String a;
    a = "foo";
    a += " bar"; // Should report a warning here
}

Expected outcome:Warning

PMD should report a violation at line 4, but doesn't. This is a false-negative.

Running PMD through: [Maven]

@ghost ghost added the a:false-negative PMD doesn't flag a problematic piece of code label Jul 1, 2021
@ghost
Copy link
Author

ghost commented Jul 1, 2021

@adangel Hi Andreas, could you please help me check this issue? Thanks!

@adangel adangel changed the title A false negative from PMD official document [java] UseStringBufferForStringAppends: Wrong example in documentation Jul 1, 2021
@adangel adangel added in:documentation Affects the documentation and removed a:false-negative PMD doesn't flag a problematic piece of code labels Jul 1, 2021
@adangel
Copy link
Member

adangel commented Jul 1, 2021

Hi @Belle-PL ,

the example we have on our documentation is too trivial. Exactly this case has been removed from detection with #1736 . If you create manually a StringBuilder to concatenate two strings, it will be slower - the java compiler optimizes this already for you. See also the comment at #1736.

Btw. the example is of course so trivial, that you would actually just write String a = "foo bar" instead of concatenating constant strings.

We probably should use a more complex example, like

String result = "";
for (int i = 0; i < 10; i++) {
    result += getStringFromSomeWhere(i);
}

This indeed should be written using a StringBuilder.

@ghost
Copy link
Author

ghost commented Jul 2, 2021

Thanks for your kind response! Hope this case will be removed soon. I think it is a little misleading.

@oowekyala oowekyala added this to the 6.43.0 milestone Feb 12, 2022
adangel added a commit to adangel/pmd that referenced this issue Feb 19, 2022
[java] Fix pmd#3374 UseStringBufferForStringAppends rule example pmd#3780
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in:documentation Affects the documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants