[java] InvalidLogMessageFormat more String.format detection

Fixes #3165
This commit is contained in:
Andreas Dangel committed 2021-03-26 09:00:00 +01:00
1 parent d08d1d10e6
commit d11f1a087a
3 files changed
+26

No files matched your search

+1
View File
@@ -31,6 +31,7 @@ the parser. More information can be found in the [language specific documentatio
* [#3160](https://github.com/pmd/pmd/issues/3160): \[java] MethodReturnsInternalArray does not consider static final fields and fields initialized with empty array
* java-errorprone
* [#3146](https://github.com/pmd/pmd/issues/3146): \[java] InvalidLogMessageFormat detection failing when String.format used
* [#3165](https://github.com/pmd/pmd/issues/3165): \[java] InvalidLogMessageFormat detection failing when String.format used in a variable
* java-performance
* [#2427](https://github.com/pmd/pmd/issues/2427): \[java] ConsecutiveLiteralAppend false-positive with builder inside lambda
* [#3152](https://github.com/pmd/pmd/issues/3152): \[java] ConsecutiveLiteralAppends and InsufficientStringBufferDeclaration: FP with switch expressions
@@ -300,6 +300,11 @@ public class InvalidLogMessageFormatRule extends AbstractJavaRule {
}
private int countPlaceholders(final ASTExpression node) {
// ignore if String.format
if (isStringFormatCall(node)) {
return -1;
}
List<ASTLiteral> literals = getStringLiterals(node);
if (literals.isEmpty()) {
// -1 we could not analyze the message parameter
@@ -948,4 +948,24 @@ class TestInvalidLogMessageFormat {
}
]]></code>
</test-code>
<test-code>
<description>[java] InvalidLogMessageFormat detection failing when String.format used in a variable</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static java.lang.String.format;
class TestInvalidLogMessageFormat {
private static final Logger LOGGER = LoggerFactory.getLogger(TestInvalidLogMessageFormat.class);
public void testPMD() {
String message = String.format("Skipping file %s because no parser could be found", getName());
LOGGER.info(message);
}
private String getName() { return "the-name"; }
}
]]></code>
</test-code>
</test-data>