[java] InsufficientStringBufferDeclaration: Fix CCE for Character (#5332)

This commit is contained in:
Juan Martín Sotuyo Dodero 2024-11-14 13:57:49 -06:00 committed by GitHub
commit f803aa36dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 38 additions and 1 deletions

View File

@ -7866,6 +7866,15 @@
"code" "code"
] ]
}, },
{
"login": "chenguangqi",
"name": "天热吃西瓜",
"avatar_url": "https://avatars.githubusercontent.com/u/6231010?v=4",
"profile": "http://chenguangqi.github.io/",
"contributions": [
"bug"
]
},
{ {
"login": "wahajenius", "login": "wahajenius",
"name": "Willem A. Hajenius", "name": "Willem A. Hajenius",

View File

@ -1116,6 +1116,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center" valign="top" width="14.28%"><a href="https://github.com/zzzzfeng"><img src="https://avatars.githubusercontent.com/u/8851007?v=4?s=100" width="100px;" alt="zzzzfeng"/><br /><sub><b>zzzzfeng</b></sub></a><br /><a href="https://github.com/pmd/pmd/issues?q=author%3Azzzzfeng" title="Bug reports">🐛</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/zzzzfeng"><img src="https://avatars.githubusercontent.com/u/8851007?v=4?s=100" width="100px;" alt="zzzzfeng"/><br /><sub><b>zzzzfeng</b></sub></a><br /><a href="https://github.com/pmd/pmd/issues?q=author%3Azzzzfeng" title="Bug reports">🐛</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/magwas"><img src="https://avatars.githubusercontent.com/u/756838?v=4?s=100" width="100px;" alt="Árpád Magosányi"/><br /><sub><b>Árpád Magosányi</b></sub></a><br /><a href="https://github.com/pmd/pmd/issues?q=author%3Amagwas" title="Bug reports">🐛</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/magwas"><img src="https://avatars.githubusercontent.com/u/756838?v=4?s=100" width="100px;" alt="Árpád Magosányi"/><br /><sub><b>Árpád Magosányi</b></sub></a><br /><a href="https://github.com/pmd/pmd/issues?q=author%3Amagwas" title="Bug reports">🐛</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/clsaa"><img src="https://avatars.githubusercontent.com/u/32028545?v=4?s=100" width="100px;" alt="任贵杰"/><br /><sub><b>任贵杰</b></sub></a><br /><a href="https://github.com/pmd/pmd/issues?q=author%3Aclsaa" title="Bug reports">🐛</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/clsaa"><img src="https://avatars.githubusercontent.com/u/32028545?v=4?s=100" width="100px;" alt="任贵杰"/><br /><sub><b>任贵杰</b></sub></a><br /><a href="https://github.com/pmd/pmd/issues?q=author%3Aclsaa" title="Bug reports">🐛</a></td>
<td align="center" valign="top" width="14.28%"><a href="http://chenguangqi.github.io/"><img src="https://avatars.githubusercontent.com/u/6231010?v=4?s=100" width="100px;" alt="天热吃西瓜"/><br /><sub><b>天热吃西瓜</b></sub></a><br /><a href="https://github.com/pmd/pmd/issues?q=author%3Achenguangqi" title="Bug reports">🐛</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/341816041"><img src="https://avatars.githubusercontent.com/u/100549608?v=4?s=100" width="100px;" alt="茅延安"/><br /><sub><b>茅延安</b></sub></a><br /><a href="https://github.com/pmd/pmd/commits?author=341816041" title="Code">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/341816041"><img src="https://avatars.githubusercontent.com/u/100549608?v=4?s=100" width="100px;" alt="茅延安"/><br /><sub><b>茅延安</b></sub></a><br /><a href="https://github.com/pmd/pmd/commits?author=341816041" title="Code">💻</a></td>
</tr> </tr>
</tbody> </tbody>

View File

@ -23,6 +23,8 @@ This is a {{ site.pmd.release_type }} release.
* [#5293](https://github.com/pmd/pmd/issues/5293): \[java] Deadlock when executing PMD in multiple threads * [#5293](https://github.com/pmd/pmd/issues/5293): \[java] Deadlock when executing PMD in multiple threads
* [#5324](https://github.com/pmd/pmd/issues/5324): \[java] Issue with type inference of nested lambdas * [#5324](https://github.com/pmd/pmd/issues/5324): \[java] Issue with type inference of nested lambdas
* [#5329](https://github.com/pmd/pmd/issues/5329): \[java] Type inference issue with unknown method ref in call chain * [#5329](https://github.com/pmd/pmd/issues/5329): \[java] Type inference issue with unknown method ref in call chain
* java-performance
* [#5314](https://github.com/pmd/pmd/issues/5314): \[java] InsufficientStringBufferDeclarationRule: Lack of handling for char type parameters
### 🚨 API Changes ### 🚨 API Changes

View File

@ -240,6 +240,12 @@ public class InsufficientStringBufferDeclarationRule extends AbstractJavaRulecha
private int calculateExpression(ASTExpression expression) { private int calculateExpression(ASTExpression expression) {
Object value = expression.getConstValue(); Object value = expression.getConstValue();
return value == null ? State.UNKNOWN_CAPACITY : (Integer) value; if (value == null) {
return State.UNKNOWN_CAPACITY;
}
if (value instanceof Character) {
return (Character) value;
}
return (Integer) value;
} }
} }

View File

@ -1416,6 +1416,25 @@ public class LiteralExpression {
sb.append("this string is longer than 16 characters"); sb.append("this string is longer than 16 characters");
return sb.toString(); return sb.toString();
} }
}
]]></code>
</test-code>
<test-code>
<description>#5314 [java] InsufficientStringBufferDeclarationRule: Lack of handling for char type parameters</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class StringBufferInstantiationWithCharTest {
public void example01() {
// misleading instantiation, these buffers
// are actually sized to 99 characters long
StringBuffer sb1 = new StringBuffer('c');
StringBuilder sb2 = new StringBuilder('c');
// in these forms, just single characters are allocated
StringBuffer sb3 = new StringBuffer("c");
StringBuilder sb4 = new StringBuilder("c");
}
} }
]]></code> ]]></code>
</test-code> </test-code>