Skip to content

Commit

Permalink
Merge branch 'pr-5013'
Browse files Browse the repository at this point in the history
  • Loading branch information
jsotuyod committed May 14, 2024
2 parents 03f5f1a + 746f64f commit 3f61843
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/pages/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Since this release, PMD will also expose any getter returning a collection of an
* [#4985](https://github.com/pmd/pmd/issues/4985): \[java] UnusedPrivateMethod false-positive / method reference in combination with custom object
* java-codestyle
* [#4930](https://github.com/pmd/pmd/issues/4930): \[java] EmptyControlStatement should not allow empty try with concise resources
* java-errorprone
* [#4042](https://github.com/pmd/pmd/issues/4042): \[java] A false negative about the rule StringBufferInstantiationWithChar

### 🚨 API Changes

Expand Down
7 changes: 4 additions & 3 deletions pmd-java/src/main/resources/category/java/errorprone.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2642,7 +2642,7 @@ public class SomeEJB extends EJBObject implements EJBLocalHome {
<rule name="StringBufferInstantiationWithChar"
language="java"
since="3.9"
message="Do not instantiate a StringBuffer or StringBuilder with a char"
message="Argument to `new StringBuilder()` or `new StringBuffer()` is implicitly converted from char to int"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_errorprone.html#stringbufferinstantiationwithchar">
<description>
Expand All @@ -2668,8 +2668,9 @@ new StringBuilder("A") // 1 + 16 = 17
<property name="xpath">
<value>
<![CDATA[
//ConstructorCall[ArgumentList/CharLiteral]
[pmd-java:typeIs('java.lang.StringBuilder') or pmd-java:typeIs('java.lang.StringBuffer')]
//ConstructorCall[ArgumentList/*[pmd-java:typeIsExactly('char')]]
[pmd-java:matchesSig('java.lang.StringBuilder#new(int)')
or pmd-java:matchesSig('java.lang.StringBuffer#new(int)')]
]]>
</value>
</property>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ public class Foo {
public class Foo2 {
StringBuilder sb = new StringBuilder('c');
}
]]></code>
</test-code>
<test-code>
<description>failure case (not a literal)</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo2 {
public void foo(char ch) {
StringBuilder builder = new StringBuilder(ch); // should report a warning in this line
}
}
]]></code>
</test-code>
Expand Down

0 comments on commit 3f61843

Please sign in to comment.